RE: [xsl] Select entire XML doc

Subject: RE: [xsl] Select entire XML doc
From: "Lars Huttar" <lars_huttar@xxxxxxx>
Date: Tue, 4 Mar 2003 13:57:22 -0600
> > > > > <xsl:template match="/">
> > > > >   <textarea>
> > > > >     <xsl:copy-of select="*"/>
> > > > >   </textarea>
> > > > > </xsl:template>
> > > >
...
> > What I meant was that because your <xsl:copy-of> used select="*",
> > it would only operate on element children of the root node.
> > So comments/PIs would only be copied if they were descendants
> > of the document node, not if they were children of "/".
>
> You're right and I'm right.  ;-)
>
> But I made the common mistake of confusing "document element" (or "root
> element," as it's also sometimes called) with the "root node" as it
applies
> to XPath.

OK, that clears things up a little. :-)

> The root node of a well-formed XML document can have exactly one
> document element.  In XPath, selecting the "root node" (which is often
used
> interchangeably with root or document element) means selecting the root
node
> containing the specified context node.  So when you write
>
> <xsl:template match="/">
>
> what you're saying is "select the root node of the document *and* the
> document element."

Really?  I thought <xsl:template match="/"> would match only the root
node ("/"), and if you want to do anything with the document element
you refer to it with the (implicit) child axis (e.g. "*" or "mydoc"):

  <xsl:template match="/">
    <xsl:for-each select="*">
	 <p><xsl:value-of select="name()"/></p>
    </xsl:for-each>
  </xsl:template>

which prints the name of the document element, not its children.

>  You could also do:
>
> <xsl:template match="/*">
>   <xsl:copy-of select="."/>
> </xsl:template>
>
> with the exact same results.

The same results as

 <xsl:template match="/">
   <xsl:copy-of select="*"/>
 </xsl:template>

but not the same results as

 <xsl:template match="/">
   <xsl:copy-of select="."/>
 </xsl:template>

right?

> Remember that the
> transformation works on a
> parsed XML input tree--not the raw document.  So any child of
> the root node
> that isn't an element is removed, and if there is more than one child
> element of the root node, the document is rejected by the
> parser as being
> malformed XML.

Are you sure?  Kay p. 63 seems to disagree:
[speaking of "The Tree Model", not of the raw XML text]
"For a *well-formed document* the children of the root
node will be the document element *plus any comments or processing
instructions* that come before or after the document element."
(emphasis mine)

Lars


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread