This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: XSL Sort and Xalan


Bryce,

>I am generating a table in HTML from dynamic XML with 4 columns, any of
>which I'd like the end-user to be able to sort. I have a parameter set in my
>stylesheet  <xsl:param name="item"/> that I'm trying to use to set the
>column that should be sorted, but it is not working.  No errors, just not
>working.
>
>This is how I am passing to the sort function:
>
><xsl:sort order="ascending" select="$item"/>
>
>My question is - am I doing this incorrectly?  or, can you not pass
>parameters to the xsl:sort element in Xalan?

The way xsl:sort works is to create a value based on interpreting the
'select' attribute in the context of each of those nodes, and then giving
you those nodes in the order of those values (ascending or descending).

So, say I was looking to sort the cells:

<row id="r1">
  <cell col="a">1</cell>
  <cell col="b">5</cell>
  <cell col="c">10</cell>
</row>
<row id="r2">
  <cell col="a">10</cell>
  <cell col="b">1</cell>
  <cell col="c">5</cell>
</row>
<row id="r3">
  <cell col="a">5</cell>
  <cell col="b">10</cell>
  <cell col="c">1</cell>
</row>

If you've set $item to be a the name of the column (say 'b'), then the
following:

  <xsl:for-each select="row">
    <xsl:sort order="ascending" select="$item" />
    ...
  </xsl:for-each>

would generate:

node    select value     resulting order
r1      b                1
r2      b                2
r3      b                3

The value given by the 'select' attribute is always the same, so the nodes
are just sorted in the order they were given in.  What you want to do is
assign to each row a value based on the value of the cell in the
interesting column, and then use that to do the sort.  The value of the
cell in the column of interest is selected (in this example) using:

  cell[col=$item]

If your xsl:for-each looked like:

  <xsl:for-each select="row">
    <xsl:sort order="ascending" select="cell[col=$item]" />
    ...
  </xsl:for-each>

it would generate:

node    select value     resulting order
r1      5                2
r2      1                1
r3      10               3

The precise select expression that you need obviously depends on the format
of your source XML, but hopefully this will get you looking in the right
direction.

Cheers,

Jeni

Dr Jeni Tennison
Epistemics Ltd * Strelley Hall * Nottingham * NG8 6PE
tel: 0115 906 1301 * fax: 0115 906 1304 * email: jeni.tennison@epistemics.co.uk


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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]