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

Subject: [xsl] Re: not simple (or simple? :-) xpath matching
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Thu, 7 Jun 2001 11:09:37 -0700 (PDT)
Jeni tennison wrote:
> 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]

Hi Daniel,

I dont find the answer above satisfactory.

Here's a better solution.

What is actually needed is:

min(abs($chapter - @id))

over the nodeset /document/part[@id = $part]/chapter

This can be found very ellegantly using a common generic template finding a "generic
minimum" node from a nodeset.

You can read more about generic templates and template references at:




You can find several ellegant Xpath (sorry Jeni, not Javascript :)) ) expressions
defining the abs() function here:


However, my favourite so far is one given by Sara ???

$num * (-($num &lt; 0) + ($num &gt; 0))

Hope this really helped.

Dimitre Novatchev.

Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

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

Current Thread