Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can XML frgments be included?

0 views
Skip to first unread message

Phil Pastor

unread,
Jan 27, 2003, 8:59:32 PM1/27/03
to
In other words can I do something like
<root>
<firstchild></firstchild>
~ include XML fragment here ~
</root>

Phil Pastor

Mike Sharp

unread,
Jan 28, 2003, 1:48:07 PM1/28/03
to
Sure.

Assuming xmlDoc is the document, and oNode is an object that points to the
fragment:

xmlDoc.documentElement.appendChild(oNode)

This inserts oNode into the root element (documentElement), after any
pre-existing children.

if oNode is a nodeList object, rather than a single node (possibly with
children), then you'll need to interate over the nodeList, appending each
node one at a time.

If you need to insert oNode in a particular location, and not just at the
end of the children, there are other DOM methods for this.

There is also an XSLT based way to do it that's pretty cool as well. I use
it to add records to XML documents.

Regards,
Mike Sharp


"Phil Pastor" <ppa...@zoominternet.net> wrote in message
news:umnsYDnxCHA.1900@TK2MSFTNGP10...

Phil Pastor

unread,
Jan 28, 2003, 2:28:38 PM1/28/03
to
Mike,

Thanks for the response, but I am not really looking for a DOM solution. I
am seeking something like the <xsl:include href="child.xml"> syntax or <!--
#include file="child.xml" -->

The reason is... I have an XML fragment that gets repeated in multiple XML
documents, and I would like to have it in just one place.

Seems like I should be able to do this. No?


"Mike Sharp" <rdc...@qwest.net> wrote in message
news:HfAZ9.123$VH5....@news.uswest.net...

Han

unread,
Jan 28, 2003, 3:42:18 PM1/28/03
to

Hi Phil

Sounds like document() function. Check it in xml4.0 sdk.

--
Have a nice day.
Han Pohwan, Microsoft MVP, Korea

"Phil Pastor" <pas...@brulant.com> wrote in message
news:#TnsfOwxCHA.2532@TK2MSFTNGP10...

Phil Pastor

unread,
Jan 28, 2003, 3:42:09 PM1/28/03
to

OK, I did find a solution, but I don't know that it is the best. Your input
is appreciated.

I have a fragment like:
<SecondChild>Childs Name</SecondChild>

...and I save it as "child.xml"

Then in my main XML document I can write...
<!DOCTYPE root [
<!ENTITY child SYSTEM "child.xml" >
]>
<root>
<FirstChild>My Name</FirstChild>
&child;
</root>
...this results in...

<root>
<FirstChild>My Name</FirstChild>
<SecondChild>Childs Name</SecondChild>
</root>

...as I said, this works, but I prefer not to use DTD syntax if I can avoid
it. Any other options here?


"Phil Pastor" <pas...@brulant.com> wrote in message
news:#TnsfOwxCHA.2532@TK2MSFTNGP10...

Mike Sharp

unread,
Jan 28, 2003, 4:01:59 PM1/28/03
to
The document() XPath method supports this. Here's the documentation from
MSDN. I hope it renders ok in the post.

Regards,
Mike Sharp

document()
Provides a way to retrieve other XML resources from within the XSL
Transformations (XSLT) style sheet beyond the initial data provided by the
input stream.

node-set document(object, node-set?)Remarks
The document() function is versatile. Its effects vary, depending on the
type and number of arguments that are used.

a.. If only one argument is provided and that argument is a string, then
document() treats the string as a URL and retrieves the document as a set of
nodes.
b.. If only one argument is provided and that argument is a node set, then
each node in that node set is treated as a URL and the function returns the
union of all of the documents referenced.
c.. If there are two arguments, the first argument can be either a string
or a node set while the second argument must be a node set. The second
argument, when supplied, serves to indicate the base URL to which the
contents of the first argument are relative.
d.. If an empty string is passed to the document() function, the result is
the source XML of the XSLT document itself, unless the second argument is
given (and is not null). In the latter case, the URL of the document is the
base URL of the node contained in the second element.
Example
For the given XML document:

<employeeRefs>
<employeeDoc href="http://www.microsoft.com/employees/employeeList.xml"/>
<employeeDoc href="localEmployees1.xml"/>
<employeeDoc href="localEmployees2.xml"/>
</employeeRefs>The following style sheet generates a document containing the
employee nodes in all of the referenced documents.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<employees>
<xsl:apply-templates select="//employeeDoc"/>
</employees>
</xsl:template>

<xsl:template match="employeeDoc">
<xsl:copy-of select="document(@href)//employee"/>
</xsl:template>
</xsl:stylesheet>

"Phil Pastor" <pas...@brulant.com> wrote in message
news:#TnsfOwxCHA.2532@TK2MSFTNGP10...

Dennis Redfield

unread,
Jan 30, 2003, 9:45:51 AM1/30/03
to
Mike,

>> if oNode is a nodeList object, rather than a single node (possibly
with
children), then you'll need to interate over the nodeList, appending each
node one at a time.

This has always seemed like an implementation flaw to me. Since one has an
interface to the node list it seems like an appendchildren method would be a
natural (or a simple polymorphic version of appendchild). Why must we
iterate if we want them all?


dlr

"Mike Sharp" <rdc...@qwest.net> wrote in message
news:HfAZ9.123$VH5....@news.uswest.net...

Mike Sharp

unread,
Jan 30, 2003, 1:46:28 PM1/30/03
to

"Dennis Redfield" <dennis....@acadia-ins.com> wrote in message
news:#w30D0GyCHA.1120@TK2MSFTNGP11...

> Mike,
> >> if oNode is a nodeList object, rather than a single node (possibly
> with
> children), then you'll need to interate over the nodeList, appending each
> node one at a time.
>
> This has always seemed like an implementation flaw to me. Since one has
an
> interface to the node list it seems like an appendchildren method would be
a
> natural (or a simple polymorphic version of appendchild). Why must we
> iterate if we want them all?
>
Unfortunately, I think it's in the specification that way, and the
implementation follows the spec.

I think the idea is that if you are going to append a bunch of nodes to a
particular node, you build the subtrees using the DocumentFragment object.
Then when you append it, the children are appended rather than the
DocumentFragment itself. But that still doesn't make it possible to use the
nodelist interface in this manner.

Since nodelist doesn't inherit from node, it might be better to have a
documentFragment property that points to a documentFragment object (in a
manner similar to the ownerDocument property), rather than an appendChildren
method.

Maybe someone else out there has an easier way of appending a nodelist
object to a node.

here's hoping,
Mike Sharp


0 new messages