在XSL中将地理坐标从度 - 小时 - 分钟转换为十进制

时间:2011-12-23 18:10:07

标签: xslt coordinate-systems geography

我有一个XML文件,其中一个元素是地理坐标(纬度/经度)。此字段为度 - 小时 - 分钟(例如:43:06:35.4779)格式,我需要将其转换为十进制格式。任何人都可以帮助在XSL中进行此转换或指向某些材料。提前谢谢!

编辑:任何人都可以帮我截断转换后得到的纬度/经度值。例如

<latitude>43.051643999999996</latitude>
<longitude>-112.62663475000001</longitude>

1 个答案:

答案 0 :(得分:1)

此转化

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="lat/text() | long/text()">
  <xsl:call-template name="DegreesToDecimal"/>
 </xsl:template>

 <xsl:template name="DegreesToDecimal">
  <xsl:param name="pDegrees" select="."/>

  <xsl:variable name="vDegrees" select=
   "number(substring-before($pDegrees, ':'))"/>

  <xsl:variable name="vMinutes" select=
   "number(
       substring-before(
           substring-after($pDegrees, ':'),
           ':'
                       )
           )"/>

  <xsl:variable name="vSeconds" select=
   "number(
       substring-after(
           substring-after($pDegrees, ':'),
           ':'
                       )
           )"/>

  <xsl:value-of select=
   "$vDegrees
  +
    $vMinutes div 60
  +
    $vSeconds div 3600
  "/>
 </xsl:template>
</xsl:stylesheet>

应用于此XML文档时

<coordinates>
 <lat>43:06:35.4779</lat>
 <long>53:22:16.7890</long>
</coordinates>

生成想要的正确结果

<coordinates>
   <lat>43.10985497222222</lat>
   <long>53.37133027777778</long>
</coordinates>

解释:正确使用substring-before()substring-after()number()函数。