Re: [xsl] Convert XML to Excel using XSLT Question II.

Subject: Re: [xsl] Convert XML to Excel using XSLT Question II.
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 21 Jun 2006 12:09:41 -0400
Karen,

At 03:42 PM 6/20/2006, you wrote:
As you said, if the XSLT will go through each node and find the match,
and apply-template when no "select =" would apply to the current
node's children, then in my following example, if I comment out the
<xsl:apply-template/>, I would assume other nodes other than the root
would still get worked on the rules, because I have other code
paragraphs/templates to match the other nodes. Then it doesn't seem to
be necessary to put a <xsl:apply-template> here. But when I commented
it out, all the rest nodes are ignored. Only <workbook ....>
<worksheet></worksheet></workbook>. Can you please explain this?

Simply, your assumption is incorrect.


Consider this template:

<xsl:template match="foo">
  <br/>
</xsl:template>

If this template allows the child nodes of the foo element to be processed, should the results of their processing be placed before the br element, or after?

In other words, a model such as you described would be underspecified with respect to a very important aspect of the building of the result tree -- the order of the elements that appear in it.

If, in contrast, we have

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

we know exactly where the br should appear relative to the results of applying templates to the children of the matched node.

Having the instruction be explicit also allows us to:

1. Do it more than once
2. Apply templates to any nodes we like (not just the children)

The presence of other templates to match other nodes has no effect on whether those nodes are processed or not. It's perfectly fine, even normal, to have templates to match nodes that do not even appear in the source document (because they might appear in some other source document).

In order to understand the XSLT processing model properly, you need to understand that:

1. Applying templates does *not* happen just because a template is present
2. Nevertheless, since the built-in default templates (for elements and the root) contain apply-templates instructions, the tree will be traversed completely by default in any case.
3. Yet, this default behavior is easy to override, at any level of the tree, simply by writing a template that does not contain an apply-templates instruction. This is an important, and very useful, feature of the language.


I hope this helps.

Cheers,
Wendell

Current Thread