-1

任何人都可以帮助我了解每个不同包号的序列号的以下逻辑。输入:

<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> 
4

1 回答 1

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>
于 2013-07-08T19:25:12.743 回答