[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] Dynamically define number of xsl:sort stmts using parameters


Subject: RE: [xsl] Dynamically define number of xsl:sort stmts using parameters
From: "Angela Williams" <Angela.Williams@xxxxxxxxxxxxxxxxxx>
Date: Wed, 28 Mar 2007 11:03:02 -0500

Aha!

My esteemed colleague came up with a better idea - gosh, I hate it when
I forget the power of xpath!

<?xml version="1.0" encoding="UTF-8"?>
<employees>
  <employee hireDate="04/23/1999">
    <last>Hill</last>
    <first>Phil</first>
    <salary>100000</salary>
  </employee>

  <employee hireDate="09/01/1998">
    <last>Herbert</last>
    <first>Johnny</first>
    <salary>95000</salary>
  </employee>

  <employee hireDate="08/20/2000">
    <last>Hill</last>
    <first>Graham</first>
    <salary>89000</salary>
  </employee>

  <sort order="1">last</sort>
  <sort order="2">first</sort>
</employees>

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0" xmlns:saxon="http://saxon.sf.net/">

  <xsl:output method="text" />

  <xsl:template match="employees">
    <xsl:variable name="sort1" select="sort[@order='1']" />
    <xsl:variable name="sort2" select="sort[@order='2']" />
    <xsl:variable name="sort3" select="sort[@order='3']" />
    <xsl:variable name="sort4" select="sort[@order='4']" />

    <xsl:for-each select="employee">
      <xsl:sort
          select="if ($sort1) then saxon:evaluate($sort1) else 'foo'" />
      <xsl:sort
          select="if ($sort2) then saxon:evaluate($sort2) else 'foo'" />
      <xsl:sort
          select="if ($sort3) then saxon:evaluate($sort3) else 'foo'" />
      <xsl:sort
          select="if ($sort4) then saxon:evaluate($sort4) else 'foo'" />


      Last: <xsl:apply-templates select="last" />
      First: <xsl:apply-templates select="first" />
      Salary: <xsl:apply-templates select="salary" />
      Hire Date: <xsl:apply-templates select="@hireDate" />
      <xsl:text>
      </xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

-----Original Message-----
From: Angela Williams
Sent: Tuesday, March 27, 2007 12:59 PM
To: 'xsl-list@xxxxxxxxxxxxxxxxxxxxxx'
Subject: RE: [xsl] Dynamically define number of xsl:sort stmts using
parameters

Thanks for both of your quick responses.  You've confirmed I am on the
right path.  There is a lot to think about.

Charles Knell wrote:
>use a stylesheet to create a stylesheet that does the transformation.

I think this would be possible, if complicated.  The user is not limited
on the number of tables they can insert, so there could be several
templates generated. It seems I would still end up with a variation of
3, as I would want to consolidate the number of templates to a minimum,
since the user could specify multiple tables with the same number of
sort statements. This would also mean I would have to output the master
stylesheet on the fly, too, to get the list of includes correct.

> (Do your users check off the columns they want to sort by in some user
>interface?

Yes.

Abel Braaksma wrote:
> I looked up an old template of myself where I faced a similar problem.

>The easy part is the number of sort statements. Just limit the amount
>to, say, 10, and create 10 sort statements.

This would certainly be the most expedient solution in the short term.

Again,
Thanks!


Current Thread