基于XSLT 1.0中条件的增量计数器

时间:2020-10-12 10:52:32

标签: xml xpath xslt-1.0 xalan

如何根据条件递增计数器,并使用该计数器测试另一个条件并呈现某些语句? 这是我的输入XML-

<order>
            <orderId>Order1</orderId>
            <OrderObject>
                <OrderType>30</OrderType>
                <ObjectID>obj1</ObjectID>
            </OrderObject>
            <OrderObject>
                <OrderType>30</OrderType>
                <ObjectID>obj2</ObjectID>
            </OrderObject>
            <OrderObject>
                <OrderType>40</OrderType>
                <ObjectID>obj3</ObjectID>
            </OrderObject>
</order>

这是我正在尝试的XSLT-

<xsl:variable name="counter" select="'1'"/>
        <xsl:variable name="length" select="'5'"/>
        <xsl:for-each select="order/OrderObject">
             <xsl:choose>
                                <xsl:when test="OrderType='30">
                                    {
                                     ObjectId : <xsl:value-of select="ObjectID"/>
                                    }
                                    counter = $counter+1;
                                    <xsl:choose>
                                    <xsl:when test="$counter < $length">
                                      <xsl:text>;</xsl:text>
                                    
                                </xsl:when>
                            </xsl:choose>
                                    
                                </xsl:when>
                            </xsl:choose>
        </xsl:for-each>

预期输出-

                            {
                            
                                    "ObjectId": "obj1"   
                            };
                            {
                            
                                    "ObjectId": "obj2"
                            }

1 个答案:

答案 0 :(得分:1)

只是过程,例如<xsl:for-each select="order/OrderObject[OrderType = 30]">,然后在其中检查position() &lt; 5

另一种方法不是在最后一项后缀;,而是在最后一项后加上前缀:

  <xsl:for-each select="order/OrderObject[OrderType = 30]">
     <xsl:if test="position() > 1">;</xsl:if>
     ...
  </xsl:for-each>