Re: [xsl] Choosing different sorts

Subject: Re: [xsl] Choosing different sorts
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 11 Feb 2003 22:13:46 +0000
Hi Andrew,

> I want to create a table with three columns, with each page in its
> own row. The url in column one, "index" in the second column of the
> row only if the page ends in index.html (nothing otherwise), and the
> product of the page/issue/@weight and either 1 or 2, depending on
> whether the page is an index or not. (e.g. Row 2 would read:
> www.foo.com/index.html index 6)
>
> So far, I can do all of this, but what I can't do is sort the list
> by the third column. I want to do an <xsl:if> immediately within the
> <xsl:for-each> to create a variable which would be referenced by an
> <xsl:sort> but the sort needs to be before the <xsl:if> and when it
> is before the if it can't recognize the variable.
>
> So how can I sort by a variable that hasn't been created or by a
> value that needs to be calculated when the variable can't be global
> when the sort line comes first?

You can't solve the problem in that way. There are three solutions
that you might try.

The first is to use a fairly complex XPath expression to do the
condition for you. Try:

  <xsl:sort select="issue/@weight *
                    (1 + (substring(@url, string-length(@url) - 9) =
                          'index.html'))" />

If the url ends with 'index.html' then you get issue/@weight * 2; if
not then you get issue/@weight * 1.

The second is to write a user-defined function to work out the value
that you want for the third column. Depending on your processor, you
might have to use JavaScript to write the function or you might be
able to use <func:function> from EXSLT to write it using XSLT.

The third is to create a temporary result tree fragment that contains
a copy of the source document but with an extra attribute on the
<page> elements to give the value that you want for the third column.
You can convert this RTF into a node set using an extension node-set()
function and then process that node set to get the output; the sort is
then simple because you can just refer to the new attribute you've
added.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread