2012-05-29 32 views
1

我收到 緯度座標形式在XML文件中的數據:3876570 經度:-9013376XSL:添加一個額外的數字來的座標數據

我使用XSL到經度/轉換LAT有8位而不是7(如上所述),所以我需要在上述座標末尾追加一個零。即我需要 緯度:38765700 經度:-90133760

我想使用format-number()函數,但不知道如果我正確使用它。我試圖

<xsl:value-of select='format-number(longitude, "########")'/> 

<xsl:value-of select='format-number(longitude, "#######0")'/> 

我最終得到了7位數字本身。請幫忙!

回答

3

您致電format-number不能給你你想要的結果,因爲它不能改變它所代表的數字的值。

您可以乘十的值(有沒有需要一個format-number通話,只要你使用XSLT 1.0)

<xsl:value-of select="longitude * 10" /> 

或追加零

<xsl:value-of select="concat(longitude, '0')" /> 
-1

明顯答案 - 乘以10或連接'0'已被提議。

這裏是一個更通用的解決方案

<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=" 
*[self::latitude or self::longitude 
and 
    not(string-length() >= 8) 
or 
    (starts-with(., '-') and not(string-length() >= 9)) 
    ]"> 

    <xsl:copy> 
    <xsl:value-of select= 
    "concat(., 
      substring('00000000', 
         1, 
         8 + starts-with(., '-') - string-length()) 
      ) 
    "/> 
    </xsl:copy> 
</xsl:template> 
</xsl:stylesheet> 

這種轉變在latitudelongitude結束爲任何值與string-length()小於8將必要的零的確切數目當應用於此XML文檔時

<coordinates> 
<latitude>3876570</latitude> 
<longitude>-9013376</longitude> 
</coordinates> 

有用,正確的結果產生:

<coordinates> 
    <latitude>38765700</latitude> 
    <longitude>-90133760</longitude> 
</coordinates> 

當此XML文檔上施加:

<coordinates> 
<latitude>123</latitude> 
<longitude>-99</longitude> 
</coordinates> 

再次有用,正確的結果產生:

<coordinates> 
    <latitude>12300000</latitude> 
    <longitude>-99000000</longitude> 
</coordinates> 

請注意

在表達:

substring('00000000', 
      1, 
      8 + starts-with(., '-') - string-length()) 

我們使用的事實,每當一個布爾值是一個參數的算術運算符,它是使用規則轉換爲數字是:

number(true()) = 1 

number(false()) = 0 

所以,日如果當前節點的值爲負值,則上面的表達式提取一個零,以計算減號並獲得我們必須附加到該數字的零的確切數目。

相關問題