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

Re: XSL - Switching Order Elements

Subject: Re: XSL - Switching Order Elements
From: JBryant@xxxxxxxxx
Date: Wed, 18 May 2005 09:21:19 -0500
xsl order
While I can't address the function and button part of it, you could do the 
XSLT part of it with a stylesheet parameter, thus:

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

<xsl:param name="order" select="descending"/>

<xsl:template match="/">
<xsl:apply-templates select="//Item">
        <xsl:sort select="name" order="{$order}"/>
</xsl:apply-templates>
</xsl:template>


<xsl:template match="Item">
        <xsl:value-of select="name"/>
</xsl:template>

</xsl:stylesheet>

The select="descending" attribute on the param instruction gives you a 
default value in case no parameter is passed. Otherwise, whatever is 
passed to the stylesheet ends up in the order attribute of the sort 
instruction. So, you'd best be sure that it's always either "ascending" or 
"descending". Here's a bit safer way to do it:

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

<xsl:param name="order"/>

<xsl:template match="/">
  <xsl:choose>
    <xsl:when test="$order='ascending'">
      <xsl:apply-templates select="//Item">
        <xsl:sort select="name" order="ascending"/>
      </xsl:apply-templates>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="//Item">
        <xsl:sort select="name" order="descending"/>
      </xsl:apply-templates>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<xsl:template match="Item">
        <xsl:value-of select="name"/>
</xsl:template>

</xsl:stylesheet>

The choose instruction protects you from people passing in garbage for the 
parameter and still gives you a default behavior. However, it makes the 
stylesheet much more verbose (which some folks seem to hate).

HTH

Jay Bryant
Bryant Communication Services
(presently consulting at Synergistic Solution Technologies)





"craig webber" <craigwebber@xxxxxxxxxxx> 
05/18/2005 08:38 AM
Please respond to
xsl-list@xxxxxxxxxxxxxxxxxxxxxx


To
xsl-list@xxxxxxxxxxxxxxxxxxxxxx
cc

Subject
 XSL - Switching Order Elements






I need to attach a function to a button that would change the "order" in 
this stylesheet to "ascending". Does anybody have any ideas?

Thanks, Craig.

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

<xsl:template match="/">
<xsl:apply-templates select="//Item">
        <xsl:sort select="name" order="descending"/>
</xsl:apply-templates>
</xsl:template>


<xsl:template match="Item">
        <xsl:value-of select="name"/>
</xsl:template>

</xsl:stylesheet>

_________________________________________________________________
Find a job, book a flight, search for a car - visit MSN South Africa! 
http://www.msn.co.za/

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.