[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Dynamic Sorting - Using a variable to determine th

Subject: RE: Dynamic Sorting - Using a variable to determine the sort select e xpression
From: "Diamond, Jason" <Jason.Diamond@xxxxxxx>
Date: Mon, 2 Apr 2001 13:10:58 -0500
determine age of a person
Hi, Mark.

The select attribute doesn't allow attribute value templates. But if all
you're sorting by is the contents of a dynamically specified child element
then you can use the local-name function to match against your variable.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" indent="no"/>

<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="root">
  <xsl:apply-templates select="data">
    <xsl:with-param name="sortexpression" select="sortby"/>
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="data">
  <xsl:param name="sortexpression"/>
  <xsl:apply-templates select="person">
    <xsl:sort select="*[local-name()=$sortexpression]"/>
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="person">
  <xsl:value-of select="name"/>
  <xsl:text>, </xsl:text>
  <xsl:value-of select="age"/>
  <br />
</xsl:template>

</xsl:stylesheet>

All I changed was the select attribute on the sort element.

Hope this helps,
Jason.

> -----Original Message-----
> From: Mark Swardstrom [mailto:mark@xxxxxxxxxx]
> Sent: Monday, April 02, 2001 10:59 AM
> To: 'XSL-List@xxxxxxxxxxxxxxxxxxxxxx'
> Subject:  Dynamic Sorting - Using a variable to 
> determine the sort
> select e xpression
> 
> 
> Hi - I'm trying to dynamically sort a list based on the value 
> of an element
> in my xml document.  Ideally, I'd be able to pass this value 
> to the xsl:sort
> element in the xsl document to determine the sort criteria.  
> In the example
> below, "sortby" could have the value "name" or "age".
> 
> Here's the XML:
> 
> <root>
> 	<sortby>name</sortby>
> 	<data>
> 		<person><name>Bob</name><age>21</age></person>
> 		<person><name>Jim</name><age>63</age></person>
> 		<person><name>Gus</name><age>55</age></person>
> 		<person><name>Van</name><age>18</age></person>
> 		<person><name>Sam</name><age>32</age></person>
> 		<person><name>Don</name><age>39</age></person>
> 		<person><name>Ron</name><age>33</age></person>
> 	</data>
> </root>
> 
> 
> And here's the XSL I tried (it doesn't work, but you'll see 
> what I'm trying
> to do).  I also tried curly braces in the xsl:sort select tag 
> to evaluate
> the sortexpression string as an expression, but no luck.  I understand
> that's not the intended use of the curly brackets, but gave it a shot.
> 
> 
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> 
> <xsl:output method="html" indent="no"/>
> 
> <xsl:template match="/">
> 	<xsl:apply-templates/>
> </xsl:template>
> 
> <xsl:template match="root">
> 	<xsl:apply-templates select="data">
> 		<xsl:with-param name="sortexpression" select="sortby"/>
> 	</xsl:apply-templates>
> </xsl:template>
> 
> <xsl:template match="data">
> 	<xsl:param name="sortexpression"/>
> 	<xsl:apply-templates select="person">
> 		<xsl:sort select="$sortexpression"/>
> 	</xsl:apply-templates>
> </xsl:template>
> 
> <xsl:template match="person">
> 	<xsl:value-of select="name"/>
> 	<xsl:text>, </xsl:text>
> 	<xsl:value-of select="age"/>
> 	<br />
> </xsl:template>
> 
> </xsl:stylesheet>
> 
> 
> If I can't do this, I find I have to write a bunch of xsl:if 
> clauses and
> that solution doesn't scale and results in bulky code.
> 
> Thanks ahead of time for your thoughts.
> 
> Mark Swardstrom
> Nimble Technology
> 
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.