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: variables numbers and apply templates


Gerrit Kuilder wrote:
> <xsl:template match="sect1|sect2|sect3|sect4|sect5" mode="createtoc">
>   <xsl:param name = "TocLevel" />
>   <xsl:variable name="CurrLevel">toclevel<xsl:value-of select="$TocLevel></xsl:variable>
>   <xsl:variable name="NextElement" select="concat('sect',$TocLevel+1)"/>
>  <xsl:element name="{$CurrLevel}">
> <xsl:if test="$NextElement">
> <xsl:apply-templates select="*[name()=$NextElement]" mode="createtoc">
>         <xsl:with-param name = "TocLevel"  select="$TocLevel+1"/>
>     </xsl:apply-templates>
>   </xsl:if>
>   </xsl:element>
> 
> Two things I cant'do
> a) <xsl:if test="count($NextElement)>0">

$NextElement is a string as returned by the concat().
You can't count the number of nodes in it because it is
not a node-set. Try count(*[name()=$NextElement]) if you
are trying to count nodes. Actually, for this particular
test, you can omit the count() altogether, as an empty
node-set will test false.
<xsl:if test="*[name()=$NextElement]">

> b) <xsl:apply-templates select="$NextElement" mode="createtoc">

Same thing; concat() creates a string, whereas you must
have a node-set as the value of the select.

Whenever you use a variable reference, you should be aware of what
type of object the variable is. 

In XPath you deal with 4 types of objects:
  number   (IEEE 754 value)
  string   (Unicode character array)
  boolean  (true/false)
  node-set (unordered nodes from 1 or more source trees)

In XSLT you get 2 more:

  result tree fragment (XSLT 1.0 only; it's a node-set that is
   constructed at run-time and can only be copied in its entirety
   or processed as a string)

  external object (formally defined in the XSLT 1.1 WD, but
   exists in XSLT 1.0 as a possible result from an extension function)

An XPath/XSLT expression always evaluates to one of these 6 types.
The XPath spec goes into detail about how the first 4 types interact
when evaluating comparisons and executing function calls, and the
XSLT spec explains this for the other 2 types. The main thing to
remember is that while XPath essentially does implicit type
conversions in most cases, there is one case where you do have to
be careful: no type can be converted to a node-set. If the XSLT
instruction you are using expects to operate on a node-set, you
must give it a node-set, not a string.

   - Mike
____________________________________________________________________________
  mike j. brown, fourthought.com  |  xml/xslt: http://skew.org/xml/
  denver/boulder, colorado, usa   |  personal: http://hyperreal.org/~mike/

 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]