RE: [xsl] Select entire XML doc

Subject: RE: [xsl] Select entire XML doc
From: "Martinez, Brian" <brian.martinez@xxxxxxxx>
Date: Mon, 3 Mar 2003 17:03:04 -0700
> From: Lars Huttar [mailto:lars_huttar@xxxxxxx]
> Sent: Monday, March 03, 2003 4:05 PM
> Subject: RE: [xsl] Select entire XML doc
> 
> 
> [Brian Martinez wrote:]
> 
> > > > <xsl:template match="/">
> > > >   <textarea>
> > > >     <xsl:copy-of select="*"/>
> > > >   </textarea>
> > > > </xsl:template>
> > >
> > > From: Lars Huttar
> > > 
> > > Just out of curiosity, and because I'm learning:
> > > Wouldn't the above copy-of fail to copy any non-element 
> > > children of the
> > > root node?  E.g. comments and PI's...
> > > (He did say "the entire XML document.")
> > 
> > No.  If the comments and PIs (by that I assume you mean processing
> > instructions) are children of the root or context node, 
> > xsl:copy-of will
> > write them to the result tree.
> > 
> > cheers,
> > b.
> 
> 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.  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."  You could also do:

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

with the exact same results.  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.

b.

| brian martinez                              brian.martinez@xxxxxxxx |
| senior gui programmer                                  303.708.7248 |
| trip network, inc.                                 fax 303.790.9350 |
| 6436 s. racine cir.                             englewood, co 80111 |
| http://www.cheaptickets.com/                   http://www.trip.com/ |

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


Current Thread