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(_at_)mulberrytech(_dot_)com
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
======================================================================