xsl-list
[Top] [All Lists]

Re: XSL - Switching Order Elements

2005-05-18 07:21:19
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(_at_)hotmail(_dot_)com> 
05/18/2005 08:38 AM
Please respond to
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com


To
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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/


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>