任何人都可以帮助我解决每个不同包装数量的序列号的下面逻辑。 输入:
<Move-Afile>
<Afile>
<Item>
<PackNumber>1234</PackNumber>
</Item>
<Item>
<PackNumber>1234</PackNumber>
</Item>
<Item>
<PackNumber>1234</PackNumber>
</Item>
<Item>
<PackNumber>126</PackNumber><!-- different PackNumber counter start from 1 -->
</Item>
<Item>
<PackNumber>126</PackNumber>
</Item>
<Item>
<PackNumber>135</PackNumber><!-- different PackNumber counter start from 1 -->
</Item>
<Item>
<PackNumber>135</PackNumber>
</Item>
</Afile>
</Move-Afile>
<Item>
是无界元素。对于每个Item PackNumber都存在。这里有一个类似于每个唯一<PackNumber>
的条件,计数器应该从1开始并像输出一样递增。
输出:
<?xml version="1.0"?>
<A>
<target>
<counter>1</counter>
<PNumber>1234</PNumber>
<counter>2</counter>
<PNumber>1234</PNumber>
<counter>3</counter>
<PNumber>1234</PNumber>
<counter>1</counter><!-- different PackNumber counter start from 1 -->
<PNumber>126</PNumber>
<counter>2</counter>
<PNumber>126</PNumber>
<counter>1</counter><!-- different PackNumber counter start from 1 -->
<PNumber>135</PNumber>
<counter>2</counter>
<PNumber>135</PNumber>
</target>
</A>
答案 0 :(得分:3)
最好的方法是寻找分组。例如xslt-1.0的 muenchian分组 http://www.jenitennison.com/xslt/grouping/muenchian.html。
因此你可以尝试这样的事情:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="kItem" match="Item" use="PackNumber"/>
<xsl:template match="/*">
<A>
<target>
<xsl:for-each select="Afile/Item[generate-id(.)=generate-id(key('kItem',PackNumber)[1])]">
<xsl:variable name="pnr" select="PackNumber" />
<xsl:for-each select="key('kItem', $pnr)" >
<counter>
<xsl:value-of select="position()"/>
</counter>
<PNumber>
<xsl:value-of select="$pnr"/>
</PNumber>
</xsl:for-each>
</xsl:for-each>
</target>
</A>
</xsl:template>
</xsl:stylesheet>
将生成以下输出:
<A>
<target>
<counter>1</counter>
<PNumber>1234</PNumber>
<counter>2</counter>
<PNumber>1234</PNumber>
<counter>3</counter>
<PNumber>1234</PNumber>
<counter>1</counter>
<PNumber>126</PNumber>
<counter>2</counter>
<PNumber>126</PNumber>
<counter>1</counter>
<PNumber>135</PNumber>
<counter>2</counter>
<PNumber>135</PNumber>
</target>
</A>