早上好。 似乎在XSLT中使用Sibling属性时遇到了问题 我的XML看起来像这样:
<Move-Afile>
<Afile>
<Item>
<PackNumber>C</PackNumber>
</Item>
<Item>
<PackNumber>S</PackNumber>
</Item>
<Item>
<PackNumber>C</PackNumber>
</Item>
<Item>
<PackNumber>D</PackNumber>
</Item>
<Item>
<PackNumber>D</PackNumber>
</Item>
<Item>
<PackNumber>D</PackNumber>
</Item>
<Item>
<PackNumber>C</PackNumber>
</Item>
<Item>
<PackNumber>F</PackNumber>
</Item>
<Item>
<PackNumber>C</PackNumber>
</Item>
</Afile>
</Move-Afile>
我的XSLT看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<!-- <xsl:template match="text()" /> -->
<xsl:template match="PackNumber" mode="count">
<xsl:choose>
<xsl:when test="not(current()/. = preceding::PackNumber) ">
<xsl:text>1</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="count(preceding::PackNumber
[not(preceding::PackNumber= .)and not( . = current()/. ) ]) +1 "/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="/">
<A>
<target>
<xsl:for-each select="//Item/PackNumber">
<PNumber>
<xsl:value-of select="."/>
</PNumber>
<xsl:variable name="count">
<xsl:apply-templates select="." mode="count"/>
</xsl:variable>
<counter>
<xsl:value-of select="$count"/>
</counter>
</xsl:for-each>
</target>
</A>
</xsl:template>
</xsl:stylesheet>
运行时,问题从第二个D元素开始。我无法确定什么会导致它不增加计数器。任何帮助/建议表示赞赏
答案 0 :(得分:0)
假设我正确地猜测你要在这里完成什么,不能简单地说:
<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:template match="/Move-Afile">
<A>
<target>
<xsl:for-each select="Afile/Item">
<PNumber>
<xsl:value-of select="PackNumber"/>
</PNumber>
<counter>
<xsl:value-of select="count(preceding-sibling::Item[PackNumber=current()/PackNumber]) + 1"/>
</counter>
</xsl:for-each>
</target>
</A>
</xsl:template>
</xsl:stylesheet>
导致:
<?xml version="1.0" encoding="UTF-8"?>
<A>
<target>
<PNumber>C</PNumber>
<counter>1</counter>
<PNumber>S</PNumber>
<counter>1</counter>
<PNumber>C</PNumber>
<counter>2</counter>
<PNumber>D</PNumber>
<counter>1</counter>
<PNumber>D</PNumber>
<counter>2</counter>
<PNumber>D</PNumber>
<counter>3</counter>
<PNumber>C</PNumber>
<counter>3</counter>
<PNumber>F</PNumber>
<counter>1</counter>
<PNumber>C</PNumber>
<counter>4</counter>
</target>
</A>
(我无法充分渗透您的代码以指出错误。)