Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What this stands for in xsl? match="@*|node()"

Tags:

xml

xslt-1.0

Can anyone explain what this means in xsl? Exactly what does each expression stands for

<xsl:template match="@*|node()">
like image 914
sajay Avatar asked Jun 20 '13 09:06

sajay


People also ask

What is match in xsl?

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).

What does node () mean in XSLT?

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.

What does xsl apply templates select * /> mean?

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.

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() .


1 Answers

@* 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>
like image 72
Ian Roberts Avatar answered Nov 16 '22 02:11

Ian Roberts