相等的节点应该在单个计数器元素下。输入:
<Move-Afile>
<Afile>
<Item>
<PACK050>
<PackNumber>1234</PackNumber>
</PACK050>
</Item>
<Item>
<PACK050>
<PackNumber>567</PackNumber>
</PACK050>
</Item>
<Item>
<PACK050>
<PackNumber>567</PackNumber>
</PACK050>
</Item>
<Item>
<PACK050>
<PackNumber>126</PackNumber>
</PACK050>
</Item>
<Item>
<PACK050>
<PackNumber>567</PackNumber>
</PACK050>
</Item>
</Afile>
</Move-Afile>
对于每个包号,我们需要增加计数器变量,但是这里存在一个条件,例如如果先前等于当前,我们必须忽略计数器(无需增加),如下面的输出。所有相等的节点都在一个之下计数器如下输出。
XSLT 模板应包含<for-each>
如下结构。
<xsl:template match="/">
<A>
<target>
<xsl:for-each select="/Move-Afile/Afile/Item/PACK050/PackNumber">
<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>
输出:
<A>
<target>
<Item>
<PACK050>
<counter>1</counter><!-- if previous <PackNumber> is not equal to current <PackNumber> increment the count-->
<PNumber>1234</PNumber>
</PACK050>
</Item>
<Item
<PACK050>
<counter>2</counter><!-- if previous <PackNumber> is not equal to current <PackNumber> increment the count-->
<PNumber>567</PNumber>
</PACK050>
</Item>
<Item><!-- if previous <PackNumber> is equal to current <PackNumber> ignore the counter -->
<PACK050>
<PNumber>567</PNumber>
</PACK050>
</Item>
<Item><!-- if previous <PackNumber> is equal to current <PackNumber> ignore the counter -->
<PACK050>
<PNumber>567</PNumber>
</PACK050>
</Item>
<Item>
<PACK050>
<counter>3</counter><!-- if previous <PackNumber> is not equal to current <PackNumber> increment the count-->
<PNumber>126</PNumber>
</PACK050>
</Item>
</target>
</A>