Can anyone explain what this means in xsl? Exactly what does each expression stands for
<xsl:template match="@*|node()">
The <xsl:template> Element The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document).
This is called the identity transform. The node()|@* is matching all child nodes ( node() is all text,element,processing instructions,comments) and attributes ( @* ) of the current context.
Definition and Usage The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes. If we add a select attribute to the <xsl:apply-templates> element it will process only the child element that matches the value of the attribute.
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() .
@*
matches any attribute node, and node()
matches any other kind of node (element, text node, processing instruction or comment). So a template matching @*|node()
will apply to any node that is not consumed by a more specific template.
The most common example of this is the identity template
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
</xsl:template>
which copies the input XML to the output tree verbatim. You can then override this template with more specific ones that apply to particular nodes to make small tweaks to the XML, for example, this stylesheet would create an output XML that is identical to the input except that all foo
elements have had their names changed to bar
:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
</xsl:template>
<xsl:template match="foo">
<bar><xsl:apply-templates select="@*|node()" /></bar>
</xsl:template>
</xsl:stylesheet>
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