Basic question regarding XSL template match

Tag: xslt , template-matching Author: zixin100 Date: 2010-10-27

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">


<xsl:stylesheet version="1.0" xmlns:xsl="">
  <xsl:output method="html"/>
  <xsl:template match="bucket"> 
          <xsl:value-of select="@version"/>

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.


Good question, +1. See my answer for an explanation.
If you are curios then I would add another aspect. The <?xml part belongs to the so-called xml prologue. XML prologue may contain processing instructions, comments and doctype declarations.

Best Answer

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

Other Answer1


The root node is the root of the tree. A root node does not occur except as the root of the tree. The element node for the document element is a child of the root node. The root node also has as children processing instruction and comment nodes for processing instructions and comments that occur in the prolog and after the end of the document element.

Always remember that (from

XPath operates on the abstract, logical structure of an XML document, rather than its surface syntax.

So, document root / means the "logical" root of the whole document.