在XSLT 1.0中增加计数器

时间:2013-05-12 06:15:31

标签: xslt

这是输入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 个答案:

答案 0 :(得分: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>

答案 1 :(得分:3)

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

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

答案 2 :(得分:1)

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

所有这一切只是复制PackNumber仅来自之前没有匹配Item的{​​{1}}元素。

计算的次数比前一个没有匹配的前一个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>

答案 3 :(得分:-1)

不是每次都使用计数器变量并尝试递增其值(由于xsl中的变量实际上是常量而不会发生这种情况),请尝试使用position(),它可以作为唯一的递增标识符。以下是使用position()

的语法示例
<xsl:variable name="counterAlias" select="position()"/>