Skip to Content
0

Reorder the XML node from last to first

Oct 25, 2017 at 06:27 PM

47

avatar image
Former Member

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

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Evgeniy Kolmakov Oct 26, 2017 at 10:45 AM
2

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.

Share
10 |10000 characters needed characters left characters exceeded
Mark Dihiansan Oct 26, 2017 at 08:12 AM
1

Hi Ahmed,

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

F1

F2

Test

Regards,

Mark


item.png (5.8 kB)
f1.png (14.2 kB)
f2.png (14.4 kB)
test.png (19.0 kB)
Share
10 |10000 characters needed characters left characters exceeded
Raghuraman S Oct 26, 2017 at 08:27 AM
0

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.


capture.png (5.8 kB)
capture1.png (22.1 kB)
Share
10 |10000 characters needed characters left characters exceeded