明显答案 - 乘以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
所以,日如果当前节点的值为负值,则上面的表达式提取一个零,以计算减号并获得我们必须附加到该数字的零的确切数目。