我从XML文件中提取数字,并希望将它们添加到新元素中。 我试图首先添加元素,然后添加新的信息。
XML的文件:
<products>
<product>
<OrderingInfo>
<item name="Part No.">12345 (text)</item>
<item name="Part No.">12345IP (text)</item>
</OrderingInfo>
</product>
<product>
<OrderingInfo>
<item name="Part No.">001 (text)</item>
<item name="Part No.">002 (text)</item>
</OrderingInfo>
</product>
期望的输出:
<products>
<product>
<OrderingInfo>
<item name="Part No.">12345 (text)</item>
<item name="Part No.">12345IP (text)</item>
</OrderingInfo>
<varitems>
</varitems>
</product>
<product>
<OrderingInfo>
<item name="Part No.">001</item>
<item name="Part No.">002</item>
</OrderingInfo>
<varitems>
</varitems>
</product>
但我不知道如何添加元素然后使用它? 这是我用
尝试的代码<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="OrderingInfo">
<xsl:copy-of select="."/>
<varitems></varitems>
</xsl:template>
<xsl:template match="varitems">
<xsl:copy>
<xsl:for-each select="ancestor::product/OrderingInformation/item">
<item>
<partno><xsl:value-of select="substring-before(../item[1], ' ')"/></partno>
<varno><xsl:value-of select="normalize-space(substring-before(current(),' '))"/></varno>
<vartitle><xsl:value-of select="normalize-space(translate(substring-after(current(),' '),'()',''))"/></vartitle>
</item>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我正在寻找的主要部分是第一个(添加varitems)并在之后使用它。 (其余的都很好)
答案 0 :(得分:0)
由于varitems
元素是所需输出中product
的子元素,因此您只需通过制作product
模板即可直接生成它。
<xsl:template match="product">
<xsl:copy>
<xsl:apply-templates/>
<varitems>
</varitems>
</xsl:copy>
</xsl:template>