[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: position() oddity?

Subject: Re: position() oddity?
From: "Steve Muench" <Steve.Muench@xxxxxxxxxx>
Date: Wed, 28 Feb 2001 17:27:27 -0800
xsl white space position
| 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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.