What's the difference between these two templates?
<xsl:template match="node()"> <xsl:template match="*">
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.
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() .
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.
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.
<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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With