1

One of the default templates in xslt is the following:

 <xsl:template match="*|/">
     <xsl:apply-templates/>
 </xsl:template>

Why does the match pattern contains the expression for the root node "/"? Doesn't the asterisk "*" capture already all nodes in the document?

I tried to leave it out and there was no difference. Following xslt

<xsl:stylesheet 
     version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xhtml" indent="yes"/>

    <xsl:template match="*">
        <xsl:value-of select="./name()"/>
        <xsl:apply-templates/>
    </xsl:template>

</xsl:stylesheet>

and following xml

<?xml version="1.0" encoding="UTF-8"?>

<a>
    <b>
    </b>
</a>

produces the output:

<?xml version="1.0" encoding="UTF-8"?>a
b

So the root node a gets captured.

Äxel
  • 350
  • 1
  • 17
  • 2
    `/` represents the document node. But this is not `a` in this case, but the parent of `a`.It represents the whole document. (You could also have comments and processing instructions before or after `a`, and these would all be children of the document node). Also note `*` matches an element, and the document node is not an element. See https://stackoverflow.com/questions/3127108/xsl-xsltemplate-match for a longer explanation. Thanks! – Tim C Sep 13 '18 at 08:06
  • Thank you. Then i misunderstood the term document root. – Äxel Sep 13 '18 at 08:11
  • Indeed, the terminology here is confusing. DOM and XSLT 2.0 calls this top node a Document node, XSLT 1.0 calls it a root node. The outermost element (a child of the Document/root node) is called the "document element" in DOM; the XML 1.0 recommendation has the definition "There is exactly one element, called the root, or document element, ..." but then it always refers to it as the "root element", never as the "root" or as the "document element". I always use the terms "document node" and "outermost element" which I think cause least confusion. – Michael Kay Sep 13 '18 at 11:11

1 Answers1

0

As the default axis for a location path is child, *|/ is just an abbreviation for child::*|/. And child::* does not match the root node of a document, just its children.

Markus
  • 3,155
  • 2
  • 23
  • 33