tags:

views:

89

answers:

4

Hello everyone,

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

+1  A: 

<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.

Rashmi Pandit
Thanks for your help, I have updated my further confusions in EDIT1 section of my post, any further comments?
George2
+1  A: 

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

<xsl:apply-templates select="."/>
Wilfred Springer
Thanks for your help, I have updated my further confusions in EDIT1 section of my post, any further comments?
George2
+1  A: 

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.

James
Thanks for your help, I have updated my further confusions in EDIT1 section of my post, any further comments?
George2
+1  A: 

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.

Alohci
Thanks for your help, I have updated my further confusions in EDIT1 section of my post, any further comments?
George2
In your example, the current node is the Document Node. It's neither virtual nor abstract. It's the topmost node of the DOM and the parent of the catalog element. So the apply-templates instruction will select just the one and only "catalog" element.
Alohci
Thanks, question answered!
George2