如何使用xslt在xml的现有值中连接一些值

时间:2016-07-25 06:33:25

标签: java xml xslt

如果重复,请道歉。我想对xml元素的值执行concat操作。

I have xml say Input.xml

<collection>
<one>
   <part>1</part>
</one>
<two>
   <part>2</part>
</two>
<three>
   <part>3</part>
</three>
</collection>

我希望输出如下:

<collection>
<one>
   <part>001</part>
</one>
<two>
   <part>002</part>
</two>
<three>
   <part>003</part>
</three>
</collection>

如何为它编写xslt

2 个答案:

答案 0 :(得分:0)

快速hacky XSLT,在线测试它似乎工作。

{{1}}

答案 1 :(得分:0)

您可能的解决方案可能是:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml"/>

    <xsl:template match="part">
        <xsl:value-of select="format-number(., '000')"/>
    </xsl:template>

    <!-- identity copy -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

part中的文字格式化为3位数字格式