You're on the right track, but you're confusing yourself in the
terminology. (You're not the first.)
In XSLT we frequently refer to the "document element" and the "root
node" (two different things) in order to avoid having to talk about the
"root element", which is what the "document element" is also called.
(But the root node in XPath is not an element at all, much less a
document element. In fact, it's a document node. Um, sorry.)
So your stylesheet to insert an element directly inside the document
element (as the OP requested, in an arbitrary XML instance) is pretty
nearly there -- but rather than matching the first element child of the
document element and inserting something ahead of it, I'd match the
document element itself and make my insertion ahead of its contents:
... plus the identity template.
This works even if the document element has no element children.
In passing, I note that XSLT is able to process even documents with more
than one element child of the root (node). The rule that only one such
child is permitted is an XML well-formedness constraint, not an XSLT rule.
In such a case, the transform above would get the insertion with every
such child ... so match "*" to avoid that.
On 1/7/2011 4:27 PM, Graydon wrote:
Sorry, brain leak -- You can't have two element children of the root
node. Thought about that much too mechanically.
The root node is _defined_ as having zero or one element children.
If you meant the document node -- the single element child of the root node --
then see the amended version below.
If you meant the root node, you can't do that.
If that means that what you want is to stick everything else inside your
known node a, or vice-versa, that's a bit different.
On Fri, Jan 07, 2011 at 04:16:16PM -0500, Graydon Saunders scripsit:
On Fri, Jan 07, 2011 at 04:07:43PM -0500, William C Thompson scripsit:
I would love to know how to (using XSL) insert a pre-determined node a
(with children b and c) as a child of the root node of an arbitrary XML. I
know how to apply the XSL once I've got it. I just need help with the XSL.
All the examples I'm finding online assume a pre-determined XML, which I
cannot in this case.
[snip illustrative abstract example]
Most importantly, please note that any solutions with hard-coded references
to any node names (other than those I want to insert) are not usable for
me. I have no control over the schema of the XML into which I'm sticking
these other nodes. I know only that it's a valid XML file and the root has
at least one child. The above example is a purely made up example designed
to illustrate the question.
You can do this with two templates, at least if I understand the question:
<!-- Our old friend the identity transform -->
<!-- match the first element child of the document node -->
You can't have two element children of the root node; the root node is _defined_
as having only one element child.
<element> Your fixed content goes here as literal result
<!-- but don't lose the former first element child of the root node -->
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
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>