Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML order by Attribute

Tags:

xml

I have googled around and tried to find an answer to this, but none of the solutions seem to work. I have the following xml schema:

<xml>
  <race>
    <particpant time="3.25" name="joe blogs">
    <particpant time="1.15" name="john Doe">
    <particpant time="2.0" name="A random">
  </race>
</xml>

I then attempt the following transformation in xslt:

<ul>
   <xsl:for-each select="participant">
   <xsl:sort  order="ascending" select="@time"/>
     <li> <xsl:value-of select="@name"/> (<xsl:value-of select="@time"/>)</li>
   </xsl:for-each>
 </ul>

The loop works and pulls out each participant, but the sort ordering has a weird effect, it switches positions around but not necessarily ordering them into the correct order.

Can anyone point me in the right direction.

BTW The code above is a stripped back version of my code and with dummy data as the actual file is larger and more complex.

Thanks in advance

John

like image 990
user505988 Avatar asked Sep 24 '26 03:09

user505988


1 Answers

I think that this might be because it is sorting values as though they were strings. Have you tried the following?

 <ul>
   <xsl:for-each select="participant">
   <xsl:sort data-type="number" order="ascending" select="@time"/>
     <li> <xsl:value-of select="@name"/> (<xsl:value-of select="@time"/>)</li>
   </xsl:for-each>
 </ul>
like image 134
Lea Hayes Avatar answered Sep 28 '26 05:09

Lea Hayes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!