George2
George2

Reputation: 45801

XSLT template matching issue

I am confused about XSLT apply-template statement. For example, here in w3school.

http://www.w3schools.com/xsl/xsl_apply_templates.asp

It is mentioned -- "The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes.", my question is whether it is applied to current element or to child nodes or both? The word "or" makes me confused about its definite behavior.

EDIT 1: here is the code snippet I am confused, I am confused when xslt processor finds <xsl:apply-templates/>, it will match all child nodes of "current node". Here "current node" means catalog or another virtual abstract XML root node? and why?

<xsl:template match="/">
  <html>
  <body>
  <h2>My CD Collection</h2>
  <xsl:apply-templates/>
  </body>
  </html>
</xsl:template>

thanks in advance, George

Upvotes: 2

Views: 1106

Answers (4)

Alohci
Alohci

Reputation: 83071

The w3schools documentation is not all it's cracked up to be, and I agree, it's quite misleading in this case.

The spec says:

In the absence of a select attribute, the xsl:apply-templates instruction processes all of the children of the current node, including text nodes.

"Children" in XML always means direct children. Children's children etc are called "descendents".

The "current node" means exactly that. It's determined by the context in which the apply-templates instruction appears.

So initially you might have:

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

Here the current node is the document node, and the apply-templates will act on the children to that, i.e. the top level element of the XML.

In this sample:

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

the current node will be a node somewhere in the XML called "cd" and the apply-templates will act on the direct children of that.

Note that this need not apply to every element called "cd", nor in fact need it apply to any element called "cd", that will depend on how the other templates in the XSLT process the input XML. All it says is that whenever that template is matched, the current node will be a "cd" node.

Upvotes: 2

James Goodwin
James Goodwin

Reputation: 7406

my question is whether it is applied to current element or to child nodes or both?

That depends on whether there is a select attribute in the apply-templates element.

If it's just <xsl:apply-templates/> then the template(s) that match the current element's child nodes will get applied. In the case from w3Schools this means that cd, title and artist all get applied.

However if you was to do something like <xsl:apply-templates select="/catalog/cd/artist"/> instead then only that element would get the template applied to it.

Upvotes: 1

Wilfred Springer
Wilfred Springer

Reputation: 10927

In case you want to apply templates to the current element, use:

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

Upvotes: 2

Rashmi Pandit
Rashmi Pandit

Reputation: 23848

<xsl:apply-templates/> matches all the child nodes of the current node.

In the e.g.

<xsl:template match="/">
  <html>
  <body>
  <h2>My CD Collection</h2>  
  <xsl:apply-templates/>  
  </body>
  </html>
</xsl:template>

the current node (/) is catalog (root node). Hence will apply the templates that match all the child nodes (cd, title, artist, country, ...) if they exist.

Best way for you to understand would be to change the xslt in the example and observe the various outputs you get.

One way would be remove all the other 3 templates (cd, artist & title) and run the xslt again.

Upvotes: 1

Related Questions