4

这是输入 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>
4

4 回答 4

4
Try this:

    <?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="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>

        <xsl:template match="text()" />

        <xsl:template match="PackNumber">
            <xsl:if test="not(preceding::PackNumber =.)" >
                <!-- if previous <PackNumber> is not equal to current <PackNumber> t-->

                <counter>
                    <xsl:value-of select="count(preceding::PackNumber[not(preceding::PackNumber= .)])+1"/>
                </counter>
            </xsl:if>
            <PNumber>
                <xsl:value-of select="."/>
            </PNumber>

        </xsl:template>

    <xsl:template match="/">
        <A>
            <target>
                <xsl:apply-templates select="//Item"/>
            </target>
        </A>
    </xsl:template>
    </xsl:stylesheet>

根据更改的问题进行更新:
在此处获取计数(在每个循环中作为变量)
试试这个:

    <?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="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>

        <xsl:template match="text()" />

        <xsl:template match="PackNumber" mode="count">
                <xsl:value-of select="count(preceding::PackNumber
                          [not(preceding::PackNumber= .)and not( . = current()/. ) ])+1"/>
        </xsl:template>

        <xsl:template match="/">
            <A>
                <target>
                    <xsl:for-each select="//Item/PackNumber">
                        <xsl:variable name="count">
                            <xsl:apply-templates select="." mode="count"/>
                        </xsl:variable>
                                <counter>
                                    <xsl:value-of select="$count"/>
                                </counter>
                                <PNumber>
                                    <xsl:value-of select="."/>
                                </PNumber>
                    </xsl:for-each>
                </target>
            </A>
        </xsl:template>
    </xsl:stylesheet>

更新输出:

<?xml version="1.0"?>
<A>
  <target>
    <counter>1</counter>
    <PNumber>1234</PNumber>
    <counter>2</counter>
    <PNumber>567</PNumber>
    <counter>2</counter>
    <PNumber>567</PNumber>
    <counter>3</counter>
    <PNumber>126</PNumber>
    <counter>4</counter>
    <PNumber>876</PNumber>
  </target>
</A>
于 2013-05-12T06:46:52.983 回答
3

这是一个简短且非常有效的解决方案,使用Muenchian 分组

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kPackNByVal" match="PackNumber" use="."/>

 <xsl:template match="Afile">
     <A>
       <target>
         <xsl:apply-templates select=
         "*/*[generate-id() = generate-id(key('kPackNByVal',.)[1])]"/>
       </target>
     </A>
 </xsl:template>

 <xsl:template match="PackNumber">
   <counter><xsl:value-of select="position()"/></counter>
   <PNumber><xsl:value-of select="."/></PNumber>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 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>

产生了想要的正确结果:

<A>
   <target>
      <counter>1</counter>
      <PNumber>1234</PNumber>
      <counter>2</counter>
      <PNumber>567</PNumber>
      <counter>3</counter>
      <PNumber>126</PNumber>
      <counter>4</counter>
      <PNumber>876</PNumber>
   </target>
</A>
于 2013-05-12T16:59:02.983 回答
1

如果您需要的结果是您在第二个XML 示例中显示的结果,那么这个样式表就是您所需要的。

它所做的PackNumber只是从那些Item没有先前匹配的元素中复制PackNumber

该计数被计算为比Item没有匹配前驱的前导元素的数目多一。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:strip-space elements="*"/>
  <xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes"/>

  <xsl:template match="/">
    <A>
      <target>
        <xsl:apply-templates select="*"/>
      </target>
    </A>
  </xsl:template>

  <xsl:template match="Item">
    <xsl:if test="not(PackNumber = preceding-sibling::Item/PackNumber)">
      <counter>
        <xsl:value-of select="1+count(preceding-sibling::Item[not(PackNumber = preceding-sibling::Item/PackNumber)])"/>
      </counter>
      <xsl:copy-of select="PackNumber"/>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

输出

<A>
   <target>
      <counter>1</counter>
      <PackNumber>1234</PackNumber>
      <counter>2</counter>
      <PackNumber>567</PackNumber>
      <counter>3</counter>
      <PackNumber>126</PackNumber>
      <counter>4</counter>
      <PackNumber>876</PackNumber>
   </target>
</A>
于 2013-05-12T14:57:43.207 回答
-1

与其使用 counter 变量并尝试每次都增加其值(这无论如何都不会发生,因为 xsl 中的变量实际上是常量),而是尝试使用position(),它可以用作唯一的递增标识符。以下是使用的语法示例position()

<xsl:variable name="counterAlias" select="position()"/>
于 2015-12-30T11:27:48.070 回答