Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 27th, 2007, 09:26 AM
Registered User
 
Join Date: Aug 2007
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need Xsl to Sort the following simple xml:

Hi

I need an xsl that will sort the following:

<combo>
    <option value='565757'>Text 1</option>
    <option value='232368'>Blah blah blah</option>
    <option value='367887'>fqwefwef fewfwef</option>
</combo>

Very simple, I know, but I cant work it out.

thanks
 
Old August 27th, 2007, 09:28 AM
Registered User
 
Join Date: Aug 2007
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've tried something like this:

but where does the sort go here?

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <combo>
      <xsl:apply-templates select="//option" />
    </combo>
  </xsl:template>
  <xsl:template match="option">
    <option>
      <xsl:value-of select="text()"/>
      <xsl:attribute name="value">
        <xsl:value-of select="@value"/>
      </xsl:attribute>
    </option>
  </xsl:template>
</xsl:stylesheet>
 
Old August 27th, 2007, 10:02 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The xsl:sort goes inside the xsl:apply-template element.

Also, the xsl:attribute must come before other content inside the option element.

Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <combo>
      <xsl:apply-templates select=".//option">
        <xsl:sort select="@value" />
      </xsl:apply-templates>
    </combo>
  </xsl:template>
  <xsl:template match="option">
    <option>
      <xsl:attribute name="value">
        <xsl:value-of select="@value"/>
      </xsl:attribute>
      <xsl:value-of select="text()"/>
    </option>
  </xsl:template>
</xsl:stylesheet>
/- Sam Judson -/
 
Old August 27th, 2007, 11:54 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You don't actually say how you want it sorted. Try

<xsl:template match="combo">
  <xsl:for-each select="option">
     <xsl:sort select="@value" datatype="number"/>
     <xsl:copy-of select="."/>
  </xsl:for-each>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference




Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple sort question garybetz XSLT 2 October 27th, 2007 12:52 PM
xsl:sort Question kwilliams XSLT 6 July 18th, 2005 08:39 AM
Unable to sort using xsl sort command sly_jimmy_boy XSLT 3 June 17th, 2005 05:15 AM
XSLT-- xsl:sort help... debo XSLT 2 December 3rd, 2004 04:25 PM
xsl:sort and IE 5.0 Issue babloo81 XSLT 1 March 17th, 2004 06:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.