I read some XSLT examples and found that code:
<xsl:apply-template select="@*|node()"/>
What does that mean?
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.
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 elements that matches the value of the attribute.
XSLT apply-templates define how to find elements and help in removing unwanted text in the document. It applies a template rule to the current child nodes. It adds a select attribute along with the template to specify the order of child nodes to process the current task with the help of the XSLT processor.
XSLT <xsl:template> The match attribute is used to associate the template with an XML element. The match attribute can also be used to define a template for a whole branch of the XML document (i.e. match="/" defines the whole document).
The XPath expression @* | node()
selects the union of attribute nodes (@*
) and all other types of XML nodes (node()
).
It is a shorthand for attribute::* | child::node()
.
In XSLT, XPath is relative to the context node and the default selection axis is the child
axis, so the expression
select="..."
expression, for example in <xsl:apply-templates>
)match=""
expression in <xsl:template>
) - note that there is a difference between selecting nodes and matching them: the context node only matters for selection.Imagine the following node is the context node:
<xml attr="value">[ ]<child />[ ]<!-- comment -->[ ]<child> <descendant /> </child>[ ]</xml>
the expression node()
will not only select both <child>
nodes, but also four whitespace-only text nodes (signified by [
and ]
for the sake of visibility) and the comment. The <descendant>
is not selected.
A special characteristic of XML is that attribute nodes are not children of the elements they belong to (although the parent of an attribute is the element it belongs to).
This asymmetric relationship makes it necessary to select them separately, hence the @*
.
It matches any attribute node belonging to the context node, so the attr="value"
will be selected as well.
The |
is the XPath union operator. It creates a singe node set from two separate node-sets.
<xsl:apply-templates>
then finds the appropriate <xsl:template>
for every selected node and runs it for that node. This is the template matching part I mentioned above.
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