如何以十进制形式将坐标分割为度数分秒XSLT

时间:2015-11-06 10:37:23

标签: xml xslt coordinates

<gml:pos>42.171035 -2.07781</gml:pos>

我正在尝试将坐标从十进制形式拆分并重新计算到几分钟秒我正在使用xslt ... 我是xslt的新人,你能帮助我吗? 感谢

2 个答案:

答案 0 :(得分:1)

尝试将PHP代码从https://www.dougv.com/2012/03/converting-latitude-and-longitude-coordinates-between-decimal-and-degrees-minutes-seconds/转换为XSLT 2.0我想出了

<xsl:function name="mf:decimal-to-DMS">
  <xsl:param name="input" as="xs:decimal"/>
  <xsl:param name="type" as="xs:boolean"/>
  <xsl:variable name="direction" as="xs:string"
    select="if ($type) then (if ($input gt 0) then 'N' else 'S')
            else (if ($input gt 0) then 'E' else 'W')"/>
  <xsl:variable name="degrees" select="floor(abs($input))"/>
  <xsl:variable name="seconds" select="(abs($input) - $degrees) * 3600"/>
  <xsl:variable name="minutes" select="floor($seconds div 60)"/>
  <xsl:variable name="seconds" select="floor($seconds - ($minutes * 60))"/>
  <xsl:sequence select="$degrees, $minutes, $seconds, $direction"/>
</xsl:function>

使用示例XML

<data>42.171035 -2.07781</data>

和样式表

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:mf="http://example.com/mf"
  exclude-result-prefixes="xs mf"
  version="2.0">

<xsl:function name="mf:decimal-to-DMS">
  <xsl:param name="input" as="xs:decimal"/>
  <xsl:param name="type" as="xs:boolean"/>
  <xsl:variable name="direction" as="xs:string"
    select="if ($type) then (if ($input gt 0) then 'N' else 'S')
            else (if ($input gt 0) then 'E' else 'W')"/>
  <xsl:variable name="degrees" select="floor(abs($input))"/>
  <xsl:variable name="seconds" select="(abs($input) - $degrees) * 3600"/>
  <xsl:variable name="minutes" select="floor($seconds div 60)"/>
  <xsl:variable name="seconds" select="floor($seconds - ($minutes * 60))"/>
  <xsl:sequence select="$degrees, $minutes, $seconds, $direction"/>
</xsl:function>

<xsl:template match="data">
  <xsl:variable name="tokens" select="tokenize(., '\s+')"/>
  <xsl:variable name="latitude" select="xs:decimal($tokens[1])"/>
  <xsl:variable name="longitude" select="xs:decimal($tokens[2])"/>
  <xsl:value-of select="mf:decimal-to-DMS($latitude, true()), mf:decimal-to-DMS($longitude, false())"/>
</xsl:template>

</xsl:stylesheet>

输出为42 10 15 N 2 4 40 W

答案 1 :(得分:1)

在XSLT 1.0中,尝试类似:

xcrun xcodebuild -exportArchive -exportPath build/ -archivePath build/MyApp.xcarchive/ -exportOptionsPlist exportOptions.plist

应用于您的示例,结果应为:

<xsl:template match="gml:pos">
    <coordinates>
        <lat>
            <xsl:call-template name="decimal-degrees-to-DMS">
                <xsl:with-param name="decimal-degrees" select="substring-before(., ' ')"/>
            </xsl:call-template>
        </lat>       
        <lon>
            <xsl:call-template name="decimal-degrees-to-DMS">
                <xsl:with-param name="decimal-degrees" select="substring-after(., ' ')"/>
            </xsl:call-template>
        </lon>   
    </coordinates>
</xsl:template>


<xsl:template name="decimal-degrees-to-DMS">
    <xsl:param name="decimal-degrees"/>

    <xsl:variable name="dec" select="translate($decimal-degrees * 3600, '-', '')" />
    <xsl:variable name="deg" select="floor($dec div 3600)" />
    <xsl:variable name="min" select="floor($dec div 60) mod 60" />
    <xsl:variable name="sec" select="$dec mod 60" />

    <xsl:value-of select="$deg" />
    <xsl:text>º </xsl:text>
    <xsl:value-of select="$min" />
    <xsl:text>' </xsl:text>
    <xsl:value-of select="format-number($sec, '0.00')" />
    <xsl:text>" </xsl:text>
    <xsl:choose>
        <xsl:when test="$decimal-degrees &lt; 0">S/W</xsl:when>
        <xsl:otherwise>N/E</xsl:otherwise>
    </xsl:choose>   
</xsl:template>