[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: baffled by <xsl:template match="/">

Subject: Re: baffled by <xsl:template match="/">
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 11 May 2004 13:12:45 -0400
xsl template match root
Kenny,

At 01:07 PM 5/11/2004, you wrote:
In Michael Kay's book, XSLT Programmer's Reference - 2nd Edition, the top of
page 75 says that the pattern "/" matches the root node.  I have always been
baffled by this, because I have never seen "/" match the root node when
using <xsl:template match="/">.

For instance, just using this XLM:

<root>
</root>

You've been caught by one of the few terminological gotchas of XSLT/XPath.


XPath posits a "root node" to be the parent of the root element of the document, which we try to call the "document element" in order to alleviate exactly this confusion. So the "root node" is not what you think it is (that element you've named "root").

The XPath model finds it convenient to have this root node for several reasons:

* Since as children it can have not only the document element, but also comments and PIs that are siblings of that element, the latter have a place in the model. (If the root were that element, we'd need a way to have siblings without a parent.)

* Since the document element is now the child of something, it can be matched by the expressions "*" (short for child::*, that is any child element) or "root" (for child::root -- this likewise has to be someone's child for it to match).

... and perhaps others that elude me at the moment.

In any case, this is the kind of thing you can learn in a formal exposition of XPath.

So while match="/" matches the root node, match="/*" is short for match="/child::*", which matches the document element (the only element child of the root), no matter what it's named. The actual root is referred to by the XPath "/", but has no name as such (which accounts for the behavior of your stylesheet).

Cheers,
Wendell


and the following XSL

----------------------------------------
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
        Match = "/" -
        <xsl:value-of select="name()"/>
</xsl:template>
</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="UTF-16"?>
        Match = "/" -
----------------------------------------

and

----------------------------------------
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
        Match = "/*"
        <xsl:value-of select="name()"/>
</xsl:template>
</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="UTF-16"?>
        Match = "/*"
        root
----------------------------------------

This would appear to me that an implicit root node of
<?xml version="1.0" encoding="UTF-16"?> is actually the root.


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.