[xsl] max() of three xsl:number results

Subject: [xsl] max() of three xsl:number results
From: "Trevor Nicholls" <trevor@xxxxxxxxxxxxxxxxxx>
Date: Fri, 23 Dec 2005 18:05:15 +1300

I have a <document> which consists of arbitrarily nested <section>s of mixed
content. Scattered throughout my document I have <target> elements. I am
trying to generate a listing which maps each <target> to its nearest
a) if a target immediately precedes a section then I want that section's
location, otherwise
b) I want the closest predecessor section, which may be
 1) the section containing the target, or
 2) the nearest section which precedes it.

In the following simple example I have given each section a title which
reflects its location in the structure, and given each target a label which
does the same.

<!-- ===== XML ===== -->
 <section title="1">
  <section title="1.1">
   <section title="1.1.1">
    <section title="">
    <target label="" />
   <target label="" />
  <target label="1.2" />
  <section title="1.2">
  <section title="1.3">
   <target label="1.3" />
  <para />
 <section title="2">
  <target label="2" />
  <para />
 <section title="3">
 <target label="3" />
<!-- ========== -->

The following stylesheet arrives at the numbers I want, viz:

<!-- ===== XSL 2.0 ===== -->
<xsl:stylesheet version="2.0"
<xsl:output method="text" />

<xsl:template match="/">
<xsl:apply-templates />

<xsl:template match="target">
 <xsl:variable name="X">
  <xsl:number count="section" level="multiple" />
 <xsl:variable name="Y">
  <xsl:number select="preceding::section[1]"
     count="section" level="multiple" />
 <xsl:variable name="Z">
   <xsl:when test="following::*[1][self::section]">
    <xsl:number select="following::*[1][self::section]"
       count="section" level="multiple" />
    <xsl:value-of select="0" />

 <xsl:value-of select="@label" />
 <xsl:text> X</xsl:text><xsl:value-of select="$X">
 <xsl:text> Y</xsl:text><xsl:value-of select="$Y">
 <xsl:text> Z</xsl:text><xsl:value-of select="$Z">
<!-- ========== -->

When I apply this to the example XML above I get:  X1.1.1  Y1.1.1.1  Z0  X1.1    Y1.1.1.1  Z0
1.2       X1      Y1.1.1.1  Z1.2
1.3       X1.3    Y1.2      Z0
2         X2      Y1.3      Z0
3         X       Y3        Z0

Clearly what I want is the (lexically) greatest value out of X, Y and Z.
But when I replace the target template's output with the following lines:

<xsl:value-of select="@label" />
<xsl:text> max </xsl:text>
<xsl:value-of select="max(($X,$Y,$Z))" />

I get an error:

Error in XPath 2.0 expression Invalid lexical value - '1.1.1'

What am I doing wrong?


Current Thread