Search code examples
xsltpattern-matchingdefault

xslt default template confusion for root node


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.


Solution

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