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()


| OUT:
| 2AAA
| 4BBB

This is likely due to how the built-in templates are
interacting with your <xsl:template match="item"/>

Given your example document:

<itemlist>
  <item>
    <name>AAA</name>
  </item>
  <item>
    <name>BBB</name>
  </item>
</itemlist>

If you *just* use the stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="item">
    <xsl:value-of select="position()"/>
    <xsl:value-of select="name"/>
    <p/>
  </xsl:template>
</xsl:stylesheet>

Then before the XSLT processor instantiates your "item" template,
it's gone the the following basic steps:

 (1) Process the root node and find a matching template
 (2) No specific template matches the root, so built-in
     template provides a fallback and processes the
     children nodes of the root (in your case <itemlist>)
 (3) No specific template matches "itemlist" so built-in
     template provides a fallback and processes the 
     children nodes of the <itemlist> element which are:

      1. -text-node-consisting-of-whitespace-
      2. <item>
      3. -text-node-consisting-of-whitespace-
      4. <item>

So as the processor is processing this list of four child
elements, as it considers each <item> element your
<xsl:template match="item"> matches, your position()
function records the position in the context node list
being processed as numbered above.

Here are a few suggestions:

(1) Make sure you have a root template to avoid
    having the built-in templates process children
    nodes that you don't want processed.
 
    The modification of your example below produces
    the result you were expecting because it specifically
    selects the "itemlist/item" nodes for processing.

    <xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="/">
        <xsl:apply-templates select="itemlist/item"/>
      </xsl:template>
      <xsl:template match="item">
        <xsl:value-of select="position()"/>
        <xsl:value-of select="name"/>
        <p/>
      </xsl:template>
    </xsl:stylesheet>

(2) If whitespace in general is causing problems you
    can get rid of extraneous whitespace-only text node
    children in your document by using <xsl:strip-space>
    The modification of your example below also 
    produces the results you were expecting by requesting
    that the all-whitespace text nodes be removed from
    the source tree before processing:

    <xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:strip-space elements="*"/>
      <xsl:template match="item">
        <xsl:value-of select="position()"/>
        <xsl:value-of select="name"/>
        <p/>
      </xsl:template>
    </xsl:stylesheet>

See http://www.w3.org/TR/xslt#built-in-rule for more
info on the built-in templates.

______________________________________________________________
Steve Muench, Lead XML Evangelist & Consulting Product Manager
Business Components for Java & XSQL Servlet Development Teams
Oracle Rep to the W3C XSL Working Group
Author "Building Oracle XML Applications", O'Reilly, Oct 2000


 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]