Re: [xsl] template matching?

Subject: Re: [xsl] template matching?
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 21 Nov 2003 11:23:26 -0500
It's so nice to see someone starting with XSLT with a "push" approach!

James, that's what we call the orthodox method of applying templates to your document, as your stylesheet does it (operating in the way described by Jeni).

An alternative approach to handling the data (and solving your problem) is to use explicit select statements to "reach in" and grab the nodes you need ("pulling"), instead of letting the built-in templates traverse the tree and only intervening where necessary (as Jeni demonstrates with the fix she suggests).

But "push" is the bread-and-butter of XSLT coding, and it's a very good way of writing strong, simple, maintainable stylesheets.

With practice, you'll find yourself using both techniques; if you use XSLT a lot, however, it's important to internalize how the tree traversal works by default, pushing the nodes through a kind of filter or set of filters (your templates). (One way of thinking of it is that there's a "push" by default since the "pull" is built in.) It's what accounts for the simplicity and elegance of XSLT when dealing with the type of transformation it was designed to handle.

Cheers,
Wendell

At 09:17 AM 11/21/2003, you wrote:
Hi James,

> does anyone know why the numbers are appearing?

When you use the instruction <xsl:apply-templates> without a select
attribute, you apply templates to all the child nodes of the current
node. For example, if you have:

  <viewentryl2 position="1.1">
    <entrydata><text>1.1, </text></entrydata>
    <viewentryl3 position="1.1.1">
      <entrydata><text>1.1.1,</text></entrydata>
    </viewentryl3>
    ...
  </viewentryl2>

and, within a template matching the <viewentryl2> element, use
<xsl:apply-templates>, you apply templates to the <entrydata> element
child and the <viewentryl3> element children, as well as the
whitespace-only text nodes before, between and after them.

When you apply templates to a node and you have a template that
matches the node, then the template that matches the node gets used to
process that node. So, in your example, you have a template for the
<viewentryl3> elements, which will be used to process the
<viewentryl3> elements.

When you apply templates to a node and you *don't* have a template
that matches the node, then the built-in templates get used. The
relevant built-in templates look like:

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

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

So when you apply templates to the <entrydata> element, the built-in
template for elements get used. This template just applies templates
to the children of the element; in this case, that means applying
templates to the <text> element. Again, since you have no template for
the <text> element, the built-in template gets used, which means the
processor applies templates to the text node child of the <text>
element. The built-in template for text nodes gets used on this text
node, and outputs the value of the text node -- the number "1.1, ".

So that's where the numbers come from (and the whitespace too,
incidentally) -- the built-in template for text nodes.

If you want to get rid of the numbers, then the best idea is to tell
the XSLT processor which elements you actually want to apply templates
to by selecting them with the select attribute of
<xsl:apply-templates>: if you only want to process the <viewentryl3>
element children of the <viewentryl2> element, for example, use:

<xsl:apply-templates select="viewentryl3" />

Alternatively, you could tell the processor that when it's told to
process an <entrydata> element, it shouldn't do anything, by providing
an empty template that matches that element:

<xsl:template match="entrydata" />

Cheers,

Jeni

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


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

___&&__&_&___&_&__&&&__&_&__&__&&____&&_&___&__&_&&_____&__&__&&_____&_&&_
"Thus I make my own use of the telegraph, without consulting
the directors, like the sparrows, which I perceive use it
extensively for a perch." -- Thoreau



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



Current Thread