xslt
在本文中,可以使用XSLT函数将具有所选字符的元素的值右移至一定长度。 没有火箭科学,但是这可能会再次变得方便,因此通过将其放到这里,我不必稍后再进行发明。 该函数本身看起来像:
<xsl:stylesheet version="2.0" xmlns:functx="http://my/functions"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:function name="functx:pad-string-to-length" as="xsd:string">
<xsl:param name="stringToPad" as="xsd:string?"/>
<xsl:param name="padChar" as="xsd:string"/>
<xsl:param name="length" as="xsd:integer"/>
<xsl:sequence select="
substring(
string-join (
($stringToPad, for $i in (1 to $length) return $padChar)
,'')
,1,$length)
"/>
</xsl:function>
</xsl:stylesheet>
有了此功能,您可以像以下方式使用它:
<xsl:template match="/">
<xsl:value-of select="functx:pad-string-to-length(//short_value, '0', 12)" />
</xsl:template>
输入的XML如:
<test>
<short_value>123</short_value>
</test>
结果将在这种情况下给出:
123000000000
顺便说一下,该功能仅适用于XSLT2!
翻译自: https://www.javacodegeeks.com/2014/10/right-pad-values-with-xslt.html
xslt