Re: [xsl] XSL - Switching Order Elements

Subject: Re: [xsl] XSL - Switching Order Elements
From: JBryant@xxxxxxxxx
Date: Wed, 18 May 2005 09:21:19 -0500
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] 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