Re: [xsl] position() oddity?

Subject: Re: [xsl] position() oddity?
From: "Steve Muench" <Steve.Muench@xxxxxxxxxx>
Date: Wed, 28 Feb 2001 17:27:27 -0800
| Given test.xml
| <?xml version="1.0"?>
| <?cocoon-process type="xslt"?>
| <?xml-stylesheet href="test.xsl" type="text/xsl"?>
| <headers>
|   <title>First</title>
|   <title>Second</title>
|   <title>Third</title>
|   <title>Fourth</title>
|   <title>Fifth</title>
| </headers>
| what would you expect position() in template match "headers/title" to
| return? Currently it returns 2 4 6 8 10 using test.xsl (appended) when
| run through xt and cocoon. It's not quite what I expected (1 2 3 4 5 :-)

Peter, this is expected behavior. Here's why:

Quoting the XSLT 1.0 spec:

    In the absence of a 'select' attribute, 
    the xsl:apply-templates instruction processes
    all of the children of the current node, 
    including text nodes. 

So, in your stylesheet, your <xsl:apply-templates/>
in your match="headers" template causes the XSLT processor
to process all of the children of <headers> *including text nodes*.

This includes text nodes that you cannot see, like
the text nodes comprised of whitespace that I've
made visible below using "#" to represent carriage return
and "*" to represent space:


So, the list of children of <headers> viewed linearly 
looks like this:

   #*** (text)
   #*** (text)
   #*** (text)
   #*** (text)
As the processor processes each child node, it will
match the default rule for the text nodes, and simply
output their value to the result tree, and it will
"engage" your match="headers/title" template for each
<title> element. In the current node list visualized
above, these <title> elements are at positions 2, 4, 6, 8

You can avoid this extra whitespace by using:

  <xsl:strip-space elements="*"/>

to strip all text-nodes consisting of only whitespace
from the input for all element, or the above in
combination with:

  <xsl:preserve-space elements="some special cases"/>

to strip white space from all *but* a space-separated
list of elements.

Steve Muench, Lead XML Evangelist & Consulting Product Manager
BC4J & XSQL Servlet Development Teams, Oracle Rep to XSL WG
Author "Building Oracle XML Applications", O'Reilly

 XSL-List info and archive:

Current Thread