明顯答案 - 乘以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>
:
這種轉變在latitude
和longitude
結束爲任何值與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
所以,日如果當前節點的值爲負值,則上面的表達式提取一個零,以計算減號並獲得我們必須附加到該數字的零的確切數目。