Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between * and node() in XSLT

What's the difference between these two templates?

<xsl:template match="node()">  <xsl:template match="*"> 
like image 568
Svish Avatar asked Aug 22 '12 10:08

Svish


People also ask

What is node-set in XSLT?

exsl:node-set() XSLT/XPath Reference: XSLT elements, EXSLT functions, XPath functions, XPath axes. exsl:node-set() returns a node-set from a result tree fragment, which is what you get when you look at the xsl:variable instead of its select attribute to fetch a variable's value.

Why do we use the select @| node () in the xsl apply templates /> element in an XSLT?

The short answer is: because of the XSLT processing model. matches any element, text-node, comment or processing-instruction. The document- (root)-node is also matched by node() .

What is current () XSLT?

XSLT current() Function The current() function returns a node-set that contains only the current node. Usually the current node and the context node are the same.

What is text () in XSLT?

XSLT <xsl:text> The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.


2 Answers

<xsl:template match="node()"> 

is an abbreviation for:

<xsl:template match="child::node()"> 

This matches any node type that can be selected via the child:: axis:

  • element

  • text-node

  • processing-instruction (PI) node

  • comment node.

On the other side:

<xsl:template match="*"> 

is an abbreviation for:

<xsl:template match="child::*"> 

This matches any element.

The XPath expression: someAxis::* matches any node of the primary node-type for the given axis.

For the child:: axis the primary node-type is element.

like image 59
Dimitre Novatchev Avatar answered Sep 22 '22 18:09

Dimitre Novatchev


Just to illustrate one of the differences, viz that * doesn't match text:

Given xml:

<A>     Text1     <B/>     Text2 </A> 

Matching on node()

<xsl:stylesheet     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     version="1.0">     <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>      <!--Suppress unmatched text-->     <xsl:template match="text()" />      <xsl:template match="/">         <root>             <xsl:apply-templates />         </root>     </xsl:template>      <xsl:template match="node()">         <node>             <xsl:copy />         </node>         <xsl:apply-templates />     </xsl:template> </xsl:stylesheet> 

Gives:

<root>     <node>         <A />     </node>     <node>         Text1     </node>     <node>         <B />     </node>     <node>         Text2     </node> </root> 

Whereas matching on *:

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

Doesn't match the text nodes.

<root>   <star>     <A />   </star>   <star>     <B />   </star> </root> 
like image 41
StuartLC Avatar answered Sep 19 '22 18:09

StuartLC