我需要根据属性值合并某些xml节点,在合并的节点上更改该属性值并相加另一个属性。需要根据属性值合并xml节点
我能够改变属性的值,但我无法弄清楚如何总结(@count)并将其分配给@count上生成的XML
源XML
<xml>
<books category="X" count="2">
<book name="bookx1"/>
<book name="bookx2"/>
</books>
<books category="Y" count="3">
<book name="booky1"/>
<book name="booky2"/>
<book name="booky3"/>
</books>
<books category="Z" count="2">
<book name="bookz1"/>
<book name="bookz2"/>
</books></xml>
XSLT转换后,它需要是这样
<xml>
<books category="A" count="5">
<book name="bookx1"/>
<book name="bookx2"/>
<book name="booky1"/>
<book name="booky2"/>
<book name="booky3"/>
</books>
<books category="Z" count="2">
<book name="bookz1"/>
<book name="bookz2"/>
</books></xml>
这是我的部分XSLT
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="@category">
<xsl:attribute name="category">
<xsl:choose>
<xsl:when test=".='X'">
<xsl:text>A</xsl:text>
</xsl:when>
<xsl:when test=".='Y'">
<xsl:text>A</xsl:text>
</xsl:when>
<xsl:when test=".='Z'">
<xsl:text>B</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
<xsl:template match="books[@category='X']"/>
<xsl:template match="books[@category='Y']"/></xsl:transform>
确保缩进整个代码块,而不仅仅是第一行。 – 2010-07-30 00:19:27
好问题(+1)。看到我的答案是一个简短而简单的解决方案。 :) – 2010-07-30 03:57:26