12

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

<xsl:template match="@*|node()">
sajay
  • 295
  • 1
  • 2
  • 11

1 Answers1

21

@* 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>
Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • 2
    As an aside, the reason `node()` doesn't include attributes is that attributes in the XPath data model are on a different _axis_ from other node types and are not considered children of their containing element. The fully expanded longhand equivalent of `@*|node()` would be `attribute::*|child::*|child::text()|child::processing-instruction()|child::comment()`. – Ian Roberts Jun 20 '13 at 10:10