XSL增量计数变量

时间:2018-07-30 11:26:01

标签: xml xslt

输入:

<root>
<!-- some info -->
<trip>
    <code>3295</code>
    <from_instant>2018-07-30T20:13:03+02:00</from_instant>
    <section>
        <id>3318ORS</id>
        <action id="596287">
            <action_kind>
                <code>P</code>
            </action_kind>
            <addressId>CAV</addressId>
            <stop_number>1</stop_number>
        </action>
        <action id="596291">
            <action_kind>
                <code>T</code>
            </action_kind>
            <addressId>002988001084</addressId>
        </action>
        <action id="596290">
            <action_kind>
                <code>S</code>
            </action_kind>
            <addressId>002988001084</addressId>
            <stop_number>2</stop_number>
        </action>
        <action id="596289">
            <action_kind>
                <code>D</code>
            </action_kind>
            <addressId>002988001082</addressId>
            <stop_number>2</stop_number>
        </action>
        <!-- multiple action tags -->
    </section>
</trip>
<!-- multiple trip tags -->
</root>

XSL:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

<xsl:template match="trip">
    <xsl:variable name="ID">
        <xsl:value-of select="code"/>
    </xsl:variable>

    <xsl:variable name="Date">
        <xsl:value-of select="substring-before(from_instant,'T')"/>
    </xsl:variable>

    <trip>
        <ID>
            <xsl:value-of select="$ID"/>
        </ID>

        <Date>
            <xsl:value-of select="$Date"/>
        </Date>

        <xsl:for-each select="section/action[action_kind/code = 'P']|section/action[action_kind/code = 'D']">

            <xsl:variable name="Order_Key">
                <xsl:value-of select="action_kind/code"/>
            </xsl:variable>
            <xsl:variable name="address">
                <xsl:value-of select="addressId"/>
            </xsl:variable>
            <xsl:if test="$address != 'CAV'">

                <ordini>


                    <order_Key>
                        <xsl:value-of select="$Order_Key"/>
                    </order_Key>

                </ordini>   
            </xsl:if>                   
        </xsl:for-each>
    </trip>
</xsl:template>
</xsl:stylesheet>

在当前的XSL中,我需要在<number>标签之后有一个标签<order_Key>,在其中我必须计数(从1开始)并增加所选择的动作标签的数量在每个人中

所以期望的输出:

<root>
<trip>
    <ID>3295</ID>
    <Date>2018-07-30</Date>
    <ordini>
        <order_Key/>
        <number>1</number>
    </ordini>
    <ordini>
        <order_Key/>
        <number>2</number>
    </ordini>
</trip>
</root>

我在旅行标签或ordini标签中输出了更多变量,但与我的问题无关。 另外,请不要有多个标签,所以我需要在输出中全部使用

<root><trip>...</trip><trip>...</trip> and so on </root>

我曾尝试在for-each内部使用位置,数字,计数,但我得到了一个空的xsl。

请帮助,谢谢!

0 个答案:

没有答案
相关问题