Re: [xsl] selection on position

Subject: Re: [xsl] selection on position
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 23 Jul 2002 10:45:59 +0100
Hi,

You're encountering the effect of the built-in templates. When a
processor reads an XML document, it creates a node tree, for example:

  /  (root or document node)
  +- colors
     +- color
     |  +- text: "red"
     +- color
     |  +- text: "blue"
     +- color
        +- text: "green"

The processor then takes the root node and tries to find a template
that matches it. If you have a template that matches the root node,
such as:

<xsl:template match="/">
  <xsl:value-of select="/colors/color[position()=2]"/>
</xsl:template>

then it processes the content of that template in order to get the
result. In this case, you'd get the value of the second color element,
i.e. "blue".

If you don't have a template that matches the root node, then the
processor uses a *built-in template* that looks like:

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

The xsl:apply-templates instruction tells the processor to apply
templates to the child node -- in this case the colors element, so the
processor takes the colors element and tries to find a template that
matches it. If you have a template that matches the color element,
such as:

<xsl:template match="colors">
  <xsl:value-of select="color[position()=2]"/>
</xsl:template>

then the processor processes the content of that template in order to
get the result. In this case, you'd get the value of the second color
element, i.e. "blue".

If you don't have a template that matches an element to which you've
told the processor to apply templates, then the processor again uses a
built-in template that looks like:

<xsl:template match="*">
  <xsl:apply-templates />
</xsl:template>

This tells the processor to apply templates to the child nodes of the
element -- in this case to all the color element children of the
colors element. Again, the processor tries to find templates that
match. If you have a template that matches one of them, such as:

<xsl:template match="/colors/color[position()=2]">
  <xsl:value-of select="."/>
</xsl:template>

then the processor will use that template. However, in your case you
don't have any templates that match the other color elements, so
again, the built-in template for elements:

<xsl:template match="*">
  <xsl:apply-templates />
</xsl:template>

and uses this to process the first and third color element. This tells
the processor to apply templates to the children of the these color
elements, which are both text nodes. Again, there's a built-in
template for text nodes, which is:

<xsl:template match="text()">
  <xsl:value-of select="." />
</xsl:template>

So when the processor applies templates to the text node children of
the first and third color elements, you get their values.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread