cancel
Showing results for 
Search instead for 
Did you mean: 

Reorder the XML node from last to first

FarooqAhmed
Participant
0 Kudos

HiExperts,

I have below xml structure

<head><item><f1>a</f1><f2>b</f2></item><item><f1>c</f1><f2>d</f2></item></head>

I want the "item" nodes to be reversed,i.e.,from last to first.Please help me with the relevant for the same.

ThankYou.

Ahmed

Accepted Solutions (0)

Answers (3)

Answers (3)

former_member190293
Active Contributor

Hi Ahmed!

This can be easily done using XSL transformation:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <head>
            <xsl:apply-templates select="/head/item">
                <xsl:sort select="position()" data-type="number" order="descending"/>
            </xsl:apply-templates>
        </head>
    </xsl:template>
    <xsl:template match="item">
        <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>

Result (according to your test data provided above):

<?xml version="1.0" encoding="utf-8"?>
<head>
    <item>
        <f1>c</f1>
        <f2>d</f2>
    </item>
    <item>
        <f1>a</f1>
        <f2>b</f2>
    </item>
</head>

Regards, Evgeniy.

markangelo_dihiansan
Active Contributor

Hi Ahmed,

The use of index (Do Not Reset to Initial Values) and sortByKey (descending) should solve your issue.

F1

F2

Test

Regards,

Mark

former_member186851
Active Contributor
0 Kudos

Hello Farooq,

If my understanding is not wrong you just to wish to reverse ITEMS Note.Try the below approach

UDF(All Values of Queue):

for(int i=var1.length-1;i>=0;i--)
{
result.addValue(var1[i]);
}

Map it to F1 and F2 Nodes

Sample Input and Output:

If this is not the requirement please post the sample input and output XMLs.