Re: [xslt] position() and apply-templates creating a copy



[ xslt@gnome.org is not the best place for asking XSLT syntax and processing
  help, there is more qualified lists for this especially the XSL list
  at mulberry (pointers from http://xmlsoft.org/XSLT/bugs.html), Daniel ]

On Sun, Nov 11, 2001 at 04:43:48PM +0100, ERDI Gergo wrote:
> [please CC replies to cactus@cactus.rulez.org]
> 
> I'm having problems using position() in my style sheets, so I created some
> test cases to better understand what's going on. Both of them gave
> surprising results so I'm asking for some explanation.
> 
> #1, position()
> Given the following XML file:
> 
> <?xml version="1.0"?>
> <root>
>   <foo/>
>   <foo/>
>   <foo/>
> </root>
> 
> and the following style sheet:
> 
>   <xsl:template match="foo">
>     <xsl:message>
>       <xsl:text>This is foo #</xsl:text>
>       <xsl:value-of select="position()"/>
>     </xsl:message>   
>   </xsl:template>
> 
> I expect the result to be 
> This is foo #1
> This is foo #2
> This is foo #3
> 
> instead, the following is shown:
> This is foo #2
> This is foo #4
> This is foo #6

  foo elements are processed by the template when called by the
implicit <xsl:apply-templates/> at the root element. The current
node list for the processing are the children of the root node, i.e.:

   Text - foo - text - foo - text - foo -text

Remember that white spaces are significant in XML and have to be preserved
to conform to the standard. The index of the nodes in this list are 2, 4, 6

> #2, apply-templates operates on a copy?
> Using the same XML file and the following style sheet:
> 
>   <xsl:template match="foo">
>     <xsl:message>
>       <xsl:text>This is foo #</xsl:text>
>       <xsl:value-of select="position()"/>
>     </xsl:message>   
>     <xsl:apply-templates select="." mode="nest"/>
>   </xsl:template>
> 
>   <xsl:template match="foo" mode="nest">
>     <xsl:message>
>       <xsl:text>(nested tmpl) This is foo #</xsl:text>
>       <xsl:value-of select="position()"/>
>     </xsl:message>
>   </xsl:template>
> 
> I get the following output:
> 
> This is foo #2
> (nested tmpl) This is foo #1
> This is foo #4
> (nested tmpl) This is foo #1
> This is foo #6
> (nested tmpl) This is foo #1

  In that case the explanation before still holds for the nodes
handled with the default foo template. When calling 
   <xsl:apply-templates select="." mode="nest"/>
the nodelist then become the single foo mode being processed,
and in the called template their position() is 1.

  To obtain 1,2,3, create a template to root, calling for the
subprocessing of only the foo nodes

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


  The associated parts of the spec are:
     - the XPath context 
       http://www.w3.org/TR/xpath#section-Introduction
       6th paragraph

     - the default XSLt built-in rules
       http://www.w3.org/TR/xslt#built-in-rule

     - the definition of apply-templates
       http://www.w3.org/TR/xslt#section-Applying-Template-Rules
    
Daniel

-- 
Daniel Veillard      | Red Hat Network https://rhn.redhat.com/
veillard@redhat.com  | libxml Gnome XML XSLT toolkit  http://xmlsoft.org/
http://veillard.com/ | Rpmfind RPM search engine http://rpmfind.net/



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]