Re: [xsl] not simple (or simple? :-) xpath matching

Subject: Re: [xsl] not simple (or simple? :-) xpath matching
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Thu, 7 Jun 2001 18:15:02 +0100
Hi Daniel,

> and now i would like to show a chapter depends on parameters in a
> url eg. '?part=c&chapter=4', but also to give the closest one, if
> given is not applicable (to give part=c, chapter=3 in this example,
> or to give part=c, chapter=10 if asked was chapter=12) with simple
> information, that is not exact match. i also need to provide default
> values if parameters are empty (for now i have <xsl:choose>
> construction).

You can set up the parameters with xsl:param elements; if you use the
select attribute or the content of the xsl:param then you can give a
default value for the parameter.  So for example, you could use:

<xsl:param name="part" select="'a'" />
<xsl:param name="chapter" select="1" />

This sets the $part parameter to 'a' by default, and the $chapter
parameter to 1 by default.

As you're using Cocoon, you can set these parameters using the URL you
use to access the XML, as you've indicated.

Then you only want to apply templates to the relevant chapter.  So
within the document-matching template, do:

<xsl:template match="document">
         select="part[@id = $part]/chapter[@id = $chapter]" />

Your example is made a little bit more complicated by the fact that
you want the 'nearest' chapter if there isn't an exact match. For the
nearest before, you want the last chapter whose @id is less than or
equal to the $chapter parameter, which makes the select expression:

  part[@id = $part]/chapter[@id &lt;= $chapter][last()]

For the nearest after, you want the first chapter whose @id is greater
than or equal to the value of the $chapter parameter, so the select
expression is:

  part[@id = $part]/chapter[@id >= $chapter][1]

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread