Re: [xsl] How to insert a set nodes under the root of an arbitrary XML using XSL?

Subject: Re: [xsl] How to insert a set nodes under the root of an arbitrary XML using XSL?
From: Graydon <graydon@xxxxxxxxx>
Date: Fri, 7 Jan 2011 16:49:13 -0500
On Fri, Jan 07, 2011 at 04:39:18PM -0500, Beck, Jeff (NIH/NLM/NCBI) [E] scripsit:
> On 1/7/11 4:27 PM, "Graydon" <graydon@xxxxxxxxx> wrote:
[I make a dumb mistake answering a question]
> There is only one child of the root node, but you still can match it. 

Well, sure, but if you produce output with two element children of the
root node -- two document nodes -- that's not well formed, so it's not
XML anymore.  Which is what my first reply did.

> This modification of your original (along with the identity template)
> works nicely for me with XSL1 in xalan. The advantage of matching the
> root element is that there might not be any children.

He did specify that it there was guaranteed to be at least one child,
but yes.

The first match I gave is a match on the document node; / is the root
node, /* is the document node, at least per XPath 2 terminology.  XPath
1 is of course using different terminology. :)

> I think you'll need to write your fixed content in the copy - and don't
> forget to process the attributes before you write any children
> 
> <!-- match the first element child of the root node -->
> <xsl:template match="/*[1]">
>     <!-- but don't lose the former first element child of the root node -->
>     <xsl:copy>
>         <xsl:apply-templates select="@*"/>
>         <element> Your fixed content goes here as literal result elements
> </element>
>         <xsl:apply-templates select="node()"/>
>     </xsl:copy>
> 
> </xsl:template>

Wheras that ought to work, because it's making sure everything stays in
the document node.

-- Graydon

Current Thread