这是输入 XML:
<Move-Afile>
<Afile>
<Item>
<PackNumber>1234</PackNumber>
</Item>
<Item>
<PackNumber>567</PackNumber>
</Item>
<Item>
<PackNumber>567</PackNumber>
</Item>
<Item>
<PackNumber>126</PackNumber>
</Item>
<Item>
<PackNumber>876</PackNumber>
</Item>
</Afile>
</Move-Afile>
<Item>
是一个包含<PackNumber>
作为子元素的无界元素。对于每个包号,我们需要增加计数器变量,但这里存在一个条件,例如如果先前<PackNumber>
等于当前<PackNumber>
,我们必须忽略计数器(无需增加),如下面的输出。
在 内部for-each
,我们可以得到像下面的 XSLT 示例那样的计数器吗?
这是我的 XSLT 模板
<xsl:template match="/">
<A>
<target>
<xsl:for-each select="/inp1:Move-Afile/inp1:Afile/inp1:Item/inp1:PalletNumber">
<xsl:variable name="count">
<!-- get the count here-->
</xsl:variable>
<counter>$count</counter>
<PNumber><xsl:value-of select="."/></PNumber>
</xsl:for-each>
</target>
</A>
</xsl:template>
这是 XML 输出:
<A>
<target>
<Item>
<counter>1</counter><!-- if previous <PackNumber> is not equal to current <PackNumber> increment the count-->
<PNumber>1234</PNumber>
</Item>
<Item>
<counter>2</counter><!-- if previous <PackNumber> is not equal to current <PackNumber> increment the count-->
<PNumber>567</PNumber>
</Item>
<Item><!-- if previous <PackNumber> is equal to current <PackNumber> ignore the count-->no need to increment
<PNumber>567</PNumber>
</Item>
<Item>
<counter>3</counter><!-- if previous <PackNumber> is not equal to current <PackNumber> increment the count-->
<PNumber>126</PNumber>
</Item>
<Item><!-- if previous <PackNumber> is equal to current <PackNumber> ignore the count-->no need to increment
<PNumber>126</PNumber>
</Item>
</target>
</A>
XML 输出 2:
<?xml version="1.0"?>
<A>
<target>
<counter>1</counter>
<PNumber>1234</PNumber>
<counter>2</counter>
<PNumber>567</PNumber>
<!-- IF PNumber is equal we have to ignore the Total loop -->
<counter>3</counter>
<PNumber>126</PNumber>
<counter>4</counter>
<PNumber>876</PNumber>
</target>
</A>