xsl-list
[Top] [All Lists]

Re: template matching?

2003-11-21 07:17:55
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



<Prev in Thread] Current Thread [Next in Thread>