Cristy
Cristy

Reputation: 557

What does this do? <xsl:apply-templates select="."/> and <xsl:apply-templates select="*|@*"/>

I am very new to XSL and I am confused about what the select inside the following pieces of code will select.

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

<xsl:apply-templates select="*|@*"/>

Any ideas? Thanks

Upvotes: 7

Views: 12001

Answers (2)

Rookie Programmer Aravind
Rookie Programmer Aravind

Reputation: 12154

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

processes the content of the current node! the dot . indicates content.. if the current node doesn't have childnodes but has data instead (ex: <foo>Sample Data</foo>) then the parser processes the data Sample Data

<xsl:apply-templates select="@*|*"/>

processes the attribute and child nodes or the data under the current node.. difference is .. this one takes care of all the attribute of the context node..

I use the word process instead of copy, because .. apply-template unlike copy-of and value-of evaluates other templates, for example along with above code if I have one more template like below:

  <xsl:template match="text()[.='Sample Data']"/> 

then it will drop the text from your output XML. Where as copy-of select="node_name" and value-of select="node-name" copy the data despite of being this template in our XSL file..

Upvotes: 5

Alex Bishop
Alex Bishop

Reputation: 483

Check out the Abbreviated Syntax section of XPath 2.0.

In the <xsl:apply-templates select="."/> example, . evaluates to the context item. In most cases, this is the same as the node currently being processed. So this example will select the context node.

In the <xsl:apply-templates select="*|@*"/> example, * will select all the child elements of the context node. @* will select all the attributes of the context node. | is the union operator. So this example will select all the child elements of the context node and also all the attributes of the context node.

<xsl:apply-templates select="."/> is frequently used to apply further processing to the context node.

<xsl:apply-templates select="*|@*"/> is frequently used to process all the child elements of the current node and its attributes. It’s often used when you’re done handling an element and want to hand off its child elements/attributes to any other templates that apply.

Upvotes: 14

Related Questions