0

input:

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

In the current XSL i need to have a tag <number> after the <order_Key> tag, in which I have to count (from 1) and increment, of course, the number of the action tags selected in the for-each

So the desired output:

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

There are some more variables that I output in the trip tag or in the ordini tag, but not relevant for my problem. Also, please not there can be multiple tags, so i need all of them in the output

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

I've tried with position, number, count, inside the for-each but i get an empty xsl.

Please help, thank you!

user3529643
  • 105
  • 8
  • Please provide minimal but complete sample of XML and XSLT and wanted and current output to demonstrate the problem. The variable `$address` you use is not even declared. – Martin Honnen Jul 30 '18 at 11:55
  • The getAdditionalOrderINfo is not relevant, you can also ignore it, the XML is bigger (i've put a comment after the root tag, there are some more information including this one). Edited to include the addressId which i forgot to include :). Also, Edited to reference another tag [for Aniket V[ – user3529643 Jul 30 '18 at 11:58

0 Answers0