[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

Read data backwards

Hi,

I want to display the following XML using XSLT:

<root>
    <no_of_entries>4</no_of_entries>
    <data>
        <number>1</number>
    </data>
    <data>
        <number>2</number>
    </data>
    <data>
        <number>3</number>
    </data>
    <data>
        <number>4</number>
    </data>
</root>

For this I would use:

<xsl:for-each select="data">
    <xsl:value-of select="number" />
</xsl:for-each>

and have the data printed "1 2 3 4".


I want to have this data read backwards "4 3 2 1". It is not possible to change the XML.

Please advice
SirTam

0
sirtam
Asked:
sirtam
  • 3
  • 2
1 Solution
 
Geert BormansCommented:
sort per descending position
    <xsl:for-each select="data">
        <xsl:sort select="position()" order="descending" data-type="number"/>
        <xsl:value-of select="number" />
    </xsl:for-each>  
0
 
sirtamAuthor Commented:
Thank you. I will try this and get back to you when tested.

SirTam
0
 
sirtamAuthor Commented:
I noticed that all of the attributes was needed. I used the following sort:

<xsl:sort select="number" order="descending"/>

I now also see that my the data always will be numbered from 1 and up with 1 as min and 4 as max.
0
 
Geert BormansCommented:
nope, you need all attributes in this case
<xsl:sort select="number" order="descending"/>
is equal to
        <xsl:sort select="position()" order="descending" data-type="text" />
(text is the default, not number)

XSLT1 processors will cast the position() to a string and sort stringwise
If you would have more than 10 items, 10 will be sorted in between 1 and 2
and that is NOT the effect you want, please stick to the original answer
0
 
sirtamAuthor Commented:
You do have a point here.

In this case there wont be numbers up to 10, so I wouldn't have noticed it. But if the system should be changed this could have made an awkward bug,

Thanks again
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now