Search code examples
xslttemplate-matching

Basic question regarding XSL template match


I've just begun to tinker with XML, and I have a question.

XML File:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<bucket version="Root Version 1A2B3C">
</bucket>

XSL FILE

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:template match="bucket"> 
    <html>
       <body>
        <h3>
          <xsl:value-of select="@version"/>
        </h3>
      </body>
    </html>
  </xsl:template>
 </xsl:stylesheet>

I have questions regarding the third line of the XSL. If I use

<xsl:template match="bucket"> - Root Version 1A2B3C is printed

<xsl:template match="/"> -

nothing is printed - I thought "/" means the root. My understanding is that it should either print "1.0" (<?xml version) or "Root Version 1A2B3C" (bucket version)

Please let me know why it is not working.

Thanks


Solution

  • / denotes the document-node() -- that is the whole document.

    In the provided XML the bucket element is the top element of the document. It isn't the root node.

    The top element bucket can still have siblings, such as processing instructions or comment nodes. The top element together with its siblings all have a single parent and this is / -- the root node of the document.