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: position() oddity?


| 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:

 <headers>#
***<title>First</title>#
***<title>Second</title>#
***<title>Third</title>#
***<title>Fourth</title>#
***<title>Fifth</title>#
</headers>

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

   #*** (text)
   <title>
   #*** (text)
   <title>
   #*** (text)
   <title>
   #*** (text)
   <title>
   #
   
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
http://www.oreilly.com/catalog/orxmlapp/



 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]