66

I am just learning XML and how to use XSL files. In an XSL file I found the following term:

xsl:template match="/"

What does this stand for? And what could I use instead of the /? Could I write table or any other HTML tag instead of /?

U. Windl
  • 3,480
  • 26
  • 54
Pat
  • 677
  • 1
  • 6
  • 3

3 Answers3

135

The value of the match attribute of the <xsl:template> instruction must be a match pattern.

Match patterns form a subset of the set of all possible XPath expressions. The first, natural, limitation is that a match pattern must select a set of nodes. There are also other limitations. In particular, reverse axes are not allowed in the location steps (but can be specified within the predicates). Also, no variable or parameter references are allowed in XSLT 1.0, but using these is legal in XSLT 2.x.

/ in XPath denotes the root or document node. In XPath 2.0 (and hence XSLT 2.x) this can also be written as document-node().

A match pattern can contain the // abbreviation.

Examples of match patterns:

<xsl:template match="table">

can be applied on any element named table.

<xsl:template match="x/y">

can be applied on any element named y whose parent is an element named x.

<xsl:template match="*">

can be applied to any element.

<xsl:template match="/*">

can be applied only to the top element of an XML document.

<xsl:template match="@*">

can be applied to any attribute.

<xsl:template match="text()">

can be applied to any text node.

<xsl:template match="comment()">

can be applied to any comment node.

<xsl:template match="processing-instruction()">

can be applied to any processing instruction node.

<xsl:template match="node()">

can be applied to any node: element, text, comment or processing instructon.

Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • That was helpful to know and is rarely mentioned in other sources: "A node matches a pattern if the node is a member of the result of evaluating the pattern as an expression with respect to some possible context; the possible contexts are those whose context node is the node being matched or one of its ancestors." (https://www.w3.org/TR/xslt#patterns) – Niklas Peter Oct 18 '17 at 13:00
  • @NiklasPeter, Yes, here the Spec is explaining a little bit how XPath expressions are evaluated -- so do we need to mention that any XSLT programmer is expected to know XPath? I think that everybody is quite aware of this prerequisite. – Dimitre Novatchev Oct 18 '17 at 14:03
  • The reason why I added this quote is the last part of it: "the possible contexts are those whose context node is the node being matched or one of its ancestors.". I think trying all the ancestors nodes is specific to XSLT and not a fact, which not everybody comfortable with XPath knows. – Niklas Peter Oct 18 '17 at 15:13
  • @NiklasPeter, This excerpt from the Spec is a convoluted way to say that the match pattern can start with (and/or contain) the `//` abbreviation. W3C specs are infamous for being confusing and "informative" to the end user. – Dimitre Novatchev Oct 18 '17 at 18:02
  • Or maybe I am wrong and we could ask @Michel-kay for his explanation. – Dimitre Novatchev Oct 18 '17 at 21:26
48

It's worth noting, since it's confusing for people new to XML, that the root (or document node) of an XML document is not the top-level element. It's the parent of the top-level element. This is confusing because it doesn't seem like the top-level element can have a parent. Isn't it the top level?

But look at this, a well-formed XML document:

<?xml-stylesheet href="my_transform.xsl" type="text/xsl"?>
<!-- Comments and processing instructions are XML nodes too, remember. -->
<TopLevelElement/>

The root of this document has three children: a processing instruction, a comment, and an element.

So, for example, if you wanted to write a transform that got rid of that comment, but left in any comments appearing anywhere else in the document, you'd add this to the identity transform:

<xsl:template match="/comment()"/>

Even simpler (and more commonly useful), here's an XPath pattern that matches the document's top-level element irrespective of its name: /*.

Robert Rossney
  • 94,622
  • 24
  • 146
  • 218
  • 4
    I have spent a good chunk of this morning being completely baffled by this document/root element dichotomy. I've been using XML and the DOM for years, but still trip up on things like this! – Tom Anderson Aug 28 '12 at 11:12
  • 1
    Thank you for letting me in on this subtle but elementary distinction I was oblivious to for too long ! :) – Chris Sep 23 '12 at 21:18
  • Wow, I've been working with xslt for 7 years and didn't know this. – Joshua Hedges Mar 07 '19 at 14:47
11

The match attribute indicates on which parts the template transformation is going to be applied. In that particular case the "/" means the root of the xml document. The value you have to provide into the match attribute should be a XPath expression. XPath is the language you have to use to refer specific parts of the target xml file.

To gain a meaningful understanding of what else you can put into match attribute you need to understand what Xpath is and how to use it. I suggest you look at links I've provided for you at the bottom of the answer.

Could I write table or any other html tag instead of "/" ?

Yes you can. But this depends on what exactly you are trying to do. If your target xml file contains HTML elements and you are trying to apply this xsl:template on them, it makes sense to use table, div or anything else.

Here a few links:

Cy Pangilinan
  • 562
  • 1
  • 7
  • 22
Koynov
  • 1,499
  • 1
  • 10
  • 19