2011-12-23 25 views
1

我有一個XML文件,其中一個元素是地理座標(緯度/經度)。該字段的格式爲度 - 小時 - 分鐘(例如:43:06:35.4779),我需要將其轉換爲十進制格式。任何人都可以幫助在XSL中做這種轉換或指向一些材料。提前致謝!在XSL中將地理座標從度 - 小時 - 分鐘轉換爲十進制

編輯:任何人都可以幫助我截斷轉換後得到的lat/long值。例如

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

您的意思是從度數(0到+ -180)到弧度(-pi到pi)?或者從43:06:35到43.109855? –

+0

從43:06:35到43.109855 – mona

回答

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()功能。

+0

當我使用這段代碼時,我得到了 NaN NaN。什麼可能是這個問題?感謝您的回答和解釋! – mona

+0

@mona:您可能修改了XSLT代碼或XML文檔,或者您有一個錯誤的非兼容XSLT處理器。我使用9種不同的XSLT處理器,它們都產生相同的正確結果。 –

+0

謝謝迪米特雷。我不確定如何遍歷XML樹。所以搞砸了......它工作的很好! – mona

相關問題