我有这个XML:
<ProdOrders>
<ProdOrder>
<Id>20</Id>
<ProdOrderNo>1107939.001</ProdOrderNo>
<ChargeNumbers>
<ChargeNo>C_1_1107939.001</ChargeNo>
<ChargeNo>C_2_1107939.001</ChargeNo>
<ChargeNo>C_3_1107939.001</ChargeNo>
<ChargeNo>C_4_1107939.001</ChargeNo>
<ChargeNo>C_5_1107939.001</ChargeNo>
<ChargeNo>C_6_1107939.001</ChargeNo>
</ChargeNumbers>
</ProdOrder>
</ProdOrders>
在XSLT中有for-each循环。是否可以删除/删除刚刚在for-each循环中使用的实际最后一个节点,并继续执行下一个节点,直到删除所有这些节点?
<xsl:template match="ProdOrders">
<xsl:for-each select="ProdOrder">
<xsl:for-each select="ChargeNumbers/ChargeNo">
<!-- Do something with the ChargeNo Value -->
<!-- Now delete the Node <ChargeNo> with its value and continue for-each loop -->
</xsl:for-each>
</xsl:for-each>
因此XML将更改为:
<ProdOrders>
<ProdOrder>
<Id>20</Id>
<ProdOrderNo>1107939.001</ProdOrderNo>
<ChargeNumbers>
<ChargeNo>C_2_1107939.001</ChargeNo>
<ChargeNo>C_3_1107939.001</ChargeNo>
<ChargeNo>C_4_1107939.001</ChargeNo>
<ChargeNo>C_5_1107939.001</ChargeNo>
<ChargeNo>C_6_1107939.001</ChargeNo>
</ChargeNumbers>
</ProdOrder>
</ProdOrders>
在for-each循环中,第一个处理项目为<ChargeNo>C_2_1107939.001</ChargeNo>
我对Xsl / -t不熟悉,我甚至不知道这是可行的,也不知道我有什么替代品。
答案 0 :(得分:1)
在你摆脱它之前,我没有看到你正在做任何废弃的值。您展示的结果可以更简单地实现:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- suppress the first ChargeNo -->
<xsl:template match="ChargeNo[1]"/>
</xsl:stylesheet>
答案 1 :(得分:0)
下面的样式表:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ChargeNo">
<xsl:choose>
<!-- alternatively, you can choose position() = 1, for the first occurrence -->
<xsl:when test="position()=last()"/>
<xsl:otherwise>
<xsl:copy>
<xsl:value-of select="concat(., '-test')"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
当应用于您输入XML时,生成:
<ProdOrders>
<ProdOrder>
<Id>20</Id>
<ProdOrderNo>1107939.001</ProdOrderNo>
<ChargeNumbers>
<ChargeNo>C_1_1107939.001-test</ChargeNo>
<ChargeNo>C_2_1107939.001-test</ChargeNo>
<ChargeNo>C_3_1107939.001-test</ChargeNo>
<ChargeNo>C_4_1107939.001-test</ChargeNo>
<ChargeNo>C_5_1107939.001-test</ChargeNo>
</ChargeNumbers>
</ProdOrder>
</ProdOrders>