如何在xslt中执行条件增量

如何在xslt中执行条件增量,xslt,Xslt,在装运中,存在订单数量。对于每个订单,都有许多订单行项目。订单行项目包含项目。对于每个迭代,我们都维护层次结构级别。在orderLine Items部分中,如果上一个项目等于当前项目,则不需要增加层次结构 要点: 只有一批货 每批货的订单数量 每个订单的orderLineItems数 对于orders和orderlineItems的每次迭代,我们必须维护一个层次结构级别(增量值),但对于orderlineItems,有一个条件是如果前一个项目等于当前项目,则不需要增加层次结构级别。否则,我们应该

在装运中,存在订单数量。对于每个订单,都有许多订单行项目。订单行项目包含项目。对于每个迭代,我们都维护层次结构级别。在orderLine Items部分中,如果上一个项目等于当前项目,则不需要增加层次结构

要点:

  • 只有一批货
  • 每批货的订单数量
  • 每个订单的orderLineItems数
  • 对于orders和orderlineItems的每次迭代,我们必须维护一个层次结构级别(增量值),但对于orderlineItems,有一个条件是如果前一个项目等于当前项目,则不需要增加层次结构级别。否则,我们应该增加层次结构级别,如下面所需的输出

    以下是输入。
    输入:


  • 
    价值4
    项目1
    (条件仅适用于本节,不适用于本节)
    3.
    3.
    2.
    价值98
    项目2
    8.
    3.
    4.
    价值98
    项目2
    8.
    3.
    4.
    
    下面是我们需要的输出

    输出:

    <ns1:Shipment>
       <ns1:Shipment1>1</ns1:Shipment1>
       <ns1:ShipmentValue>S</ns1:ShipmentValue>
       <ns1:Order>
          <ns1:Orderlevel1>2</ns1:Orderlevel1>
          <ns1:Orderlevel2>1</ns1:Orderlevel2>
          <ns1:Orderlevel3>O</ns1:Orderlevel3>
          <ns1:OrderLineItems>
             <ns1:Level1>3</ns1:Level1>
             <ns1:Level2>2</ns1:Level2>
             <ns1:Level3>I</ns1:Level3>
             <ns1:Item>Item59</ns1:Item>
          </ns1:OrderLineItems>
          <ns1:OrderLineItems>
             <ns1:Item>Item59</ns1:Item>
          </ns1:OrderLineItems>
           <ns1:OrderLineItems>
            <ns1:Level1>4</ns1:Level1>
             <ns1:Level2>2</ns1:Level2>
             <ns1:Level3>I</ns1:Level3>
             <ns1:Item>Item63</ns1:Item>
          </ns1:OrderLineItems>
       </ns1:Order>
       <ns1:Order>
          <ns1:Orderlevel1>5</ns1:Orderlevel1>
          <ns1:Orderlevel2>1</ns1:Orderlevel2>
          <ns1:Orderlevel3>O</ns1:Orderlevel3>
          <ns1:OrderLineItems>
             <ns1:Level1>6</ns1:Level1>
             <ns1:Level2>5</ns1:Level2>
             <ns1:Level3>I</ns1:Level3>
             <Item>Item74</Item>
          </ns1:OrderLineItems>
           <ns1:OrderLineItems>
           <ns1:Level1>7</ns1:Level1>
             <ns1:Level2>5</ns1:Level2>
             <ns1:Level3>I</ns1:Level3>
            <Item>Item78</Item>
          </ns1:OrderLineItems>   
          <ns1:OrderLineItems>
             <ns1:Item>Item78</ns1:Item>
          </ns1:OrderLineItems>
     </ns1:Order>
    </ns1:Shipment>
    
    
    1.
    s
    2.
    1.
    O
    3.
    2.
    我
    项目59
    项目59
    4.
    2.
    我
    项目63
    5.
    1.
    O
    6.
    5.
    我
    项目74
    7.
    5.
    我
    项目78
    项目78
    


    我已经简化了你的问题(假设我理解正确,我不确定)

    无论如何,对于此XML:

    <shipment>
     <order>
      <value>value 4</value>
      <other>item1</other>
     </order>
     <order>
      <value>value 98</value>
      <other>item2</other>
     </order>
     <order>
      <value>value 98</value>
      <other>item3</other>
     </order>
     <order>
      <value>another value</value>
      <other>item4</other>
     </order>
    </shipment>
    
    
    价值4
    项目1
    价值98
    项目2
    价值98
    项目3
    另一个价值
    项目4
    
    我得到了以下输出(注意
    元素在每次
    改变时都会增加):

    
    1.
    价值4
    项目1
    2.
    价值98
    项目2
    2.
    价值98
    项目3
    3.
    另一个价值
    项目4
    
    如果这对您有用,请查看以下样式表:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
      <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
    <xsl:template match="/">
      <shipment>
        <!-- call template for first <order> only. All subsequent <order>
             elements will be called recursively by this template itself -->
        <xsl:call-template name="order">
          <xsl:with-param name="prevValue" select="string('')"/>
          <xsl:with-param name="counter" select="0"/>
          <xsl:with-param name="self" select="/shipment/order[1]"/>
        </xsl:call-template>
      </shipment>
    </xsl:template>
    
    <xsl:template name="order">
      <xsl:param name="prevValue"/>
      <xsl:param name="counter"/>
      <xsl:param name="self"/>
    
      <xsl:variable name="selfCounter">
        <xsl:choose>
          <!-- if the <value> is the same as in previous <order>, 
               do not increment <counter> -->
          <xsl:when test="$prevValue = $self/value">
            <xsl:value-of select="$counter"/>
          </xsl:when>
          <!-- if the value is different, increment <counter> by one -->
          <xsl:otherwise>
            <xsl:value-of select="$counter + 1"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
    
      <order>
        <!-- copy all the original children of <order>,
             and add <counter> -->
        <counter><xsl:value-of select="$selfCounter"/></counter>
        <xsl:copy-of select="$self/node()" />
      </order>
    
      <!-- if there is another <order>, call this template -->
      <xsl:if test="$self/following-sibling::order[1]">
            <xsl:call-template name="order">
              <xsl:with-param name="prevValue" select="$self/value"/>
              <xsl:with-param name="counter" select="$selfCounter"/>
              <xsl:with-param name="self" select="$self/following-sibling::order[1]"/>
            </xsl:call-template>
      </xsl:if>
    </xsl:template>
    
    </xsl:stylesheet>
    
    
    
    您能否澄清您的问题并使用较小的示例。甚至可以创建一个最小的示例来说明您想要实现的目标?从输入中,我们获得了装运数据。每个装运有订单数。每个订单有多个orderLineItems。订单和orderLineItems应该迭代。对于每个迭代,我们应该维护一个层次结构级别,但这里有一个条件,如果前一个项目值等于当前项目值,则无需增加层次结构级别的值。否则,我们需要增加order和orderlineItems的层次结构,这取决于它迭代的次数。为什么“Level3”元素被设置为包含“O”或“I”?在我对订单层次结构的要求中,Level3元素为“O”,而对于项目层次结构,Level3元素为“I”。这没有问题,我们可以硬编码这个“O”和“I”可能是重复的,我已经添加了输入xml,请指定此结构的计数器变量。
    <shipment>
       <order>
          <counter>1</counter>
          <value>value 4</value>
          <other>item1</other>
       </order>
       <order>
          <!-- counter incremented, because value changed
               from "value 4" to "value 98" -->
          <counter>2</counter>
          <value>value 98</value>
          <other>item2</other>
       </order>
       <order>
          <!-- counter not incremented, value still "value 98" -->
          <counter>2</counter>
          <value>value 98</value>
          <other>item3</other>
       </order>
       <order>
          <!-- counter incremented, because value changed
               from "value 98" to "another value" -->
          <counter>3</counter>
          <value>another value</value>
          <other>item4</other>
       </order>
    </shipment>
    
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
      <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
    <xsl:template match="/">
      <shipment>
        <!-- call template for first <order> only. All subsequent <order>
             elements will be called recursively by this template itself -->
        <xsl:call-template name="order">
          <xsl:with-param name="prevValue" select="string('')"/>
          <xsl:with-param name="counter" select="0"/>
          <xsl:with-param name="self" select="/shipment/order[1]"/>
        </xsl:call-template>
      </shipment>
    </xsl:template>
    
    <xsl:template name="order">
      <xsl:param name="prevValue"/>
      <xsl:param name="counter"/>
      <xsl:param name="self"/>
    
      <xsl:variable name="selfCounter">
        <xsl:choose>
          <!-- if the <value> is the same as in previous <order>, 
               do not increment <counter> -->
          <xsl:when test="$prevValue = $self/value">
            <xsl:value-of select="$counter"/>
          </xsl:when>
          <!-- if the value is different, increment <counter> by one -->
          <xsl:otherwise>
            <xsl:value-of select="$counter + 1"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
    
      <order>
        <!-- copy all the original children of <order>,
             and add <counter> -->
        <counter><xsl:value-of select="$selfCounter"/></counter>
        <xsl:copy-of select="$self/node()" />
      </order>
    
      <!-- if there is another <order>, call this template -->
      <xsl:if test="$self/following-sibling::order[1]">
            <xsl:call-template name="order">
              <xsl:with-param name="prevValue" select="$self/value"/>
              <xsl:with-param name="counter" select="$selfCounter"/>
              <xsl:with-param name="self" select="$self/following-sibling::order[1]"/>
            </xsl:call-template>
      </xsl:if>
    </xsl:template>
    
    </xsl:stylesheet>