Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > including one XML file in another XML file

Reply
Thread Tools

including one XML file in another XML file

 
 
Johan
Guest
Posts: n/a
 
      02-21-2007
How can I include one XML file into another XML file (on the client
side, in Firefox)?

I think XInclude is just what I need, but Firefox doesn't support it:
https://bugzilla.mozilla.org/show_bug.cgi?id=201754

It seems I also can use an "external entity reference", but that
depends on a DTD and I'm using XML Schema. Is it also possible with a
Schema and how can I do it?

Here is exactly what I'm trying to do:

design.xml:
<?xml .....>
<root>
<design>....</design>
<library>....</library>
</root>

Currently the 'library' element is inside the 'design.xml' file. I
want to put the library element into a separate xml file and include
it in all my design files. (I have a lot of designs and only a couple
of libraries).

The design.xml file contains a reference to an XSLT file that
visualizes the design. When you open the design.xml file in Firefox a
SVG (scalable vector graphics) file is created on the fly and shown to
you.

I also have an XML Schema file that specifies what all the valid
elements in the design.xml file are.

Can someone show me an example that accomplishes this? That would be
great.

thanks, Johan.

 
Reply With Quote
 
 
 
 
fildpauz
Guest
Posts: n/a
 
      02-21-2007
> How can I include one XML file into another XML file (on the client
> side, in Firefox)?


I'm kind of new to XML, but let me take a stab at answering this.
Depending on what you want the client to do with the document, it
seems you might be able to accomplish this at the stylesheet level
using document().

design.xml
<?xml ...>
<root>
<design> ... </design>
<library/>
</root>

library.xml
<?xml ...>
<library>
....
</library>

stylesheet.xml
<?xml version="1.0">
<xsl:stylesheet ... >
<xsl:template match="root">
<xsl:apply-templates select="design"/>
<xsl:apply-templates select="document(library.xml)/"/>
</xsl:template>
</xsl:stylesheet>

I might not have the syntax quite right, but I hope you get the idea.
When the stylesheet is applied to design.xml, the processor will
access and handle the external file as if it were part of the source
document. For further info see the XSL section on handling multiple
documents: http://www.w3.org/TR/xslt#document

Good luck!

 
Reply With Quote
 
 
 
 
p.lepin@ctncorp.com
Guest
Posts: n/a
 
      02-21-2007
On Feb 21, 11:45 am, "Johan" <(E-Mail Removed)> wrote:
> design.xml:
> <?xml .....>
> <root>
> <design>....</design>
> <library>....</library>
> </root>
>
> Currently the 'library' element is inside the
> 'design.xml' file. I want to put the library element into
> a separate xml file and include it in all my design
> files. (I have a lot of designs and only a couple of
> libraries).
>
> The design.xml file contains a reference to an XSLT file
> that visualizes the design.


I don't believe what you want is possible, but there are
other solutions to the problem; document() on the XSLT side
in particular comes to mind. Proof-of-concept:

xform1.xml:

<?xml-stylesheet type="text/xsl" href="xform.xsl"?>
<root library="1">
<design>1</design>
</root>

xform_lib_1.xml:

<library>
<design n="1">A</design>
</library>

xform.xsl:

<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable
name="lib"
select=
"
document(concat('xform_lib_',/root/@library,'.xml'))
"/>
<xsl:template match="root">
<result>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="design">
<xsl:value-of
select="$lib/library/design[@n=current()]"/>
</xsl:template>
</xsl:stylesheet>

Note that client-side transformations in general should be
avoided, unless you know very well what you're doing. So,
unless you know very well what you're doing, consider
transforming your document server-side.

--
Pavel Lepin

 
Reply With Quote
 
Johan
Guest
Posts: n/a
 
      02-21-2007
On 21 feb, 11:11, "fildpauz" <(E-Mail Removed)> wrote:
> > How can I include one XML file into another XML file (on the client
> > side, in Firefox)?

>
> I'm kind of new to XML, but let me take a stab at answering this.
> Depending on what you want the client to do with the document, it
> seems you might be able to accomplish this at the stylesheet level
> using document().
>
> design.xml
> <?xml ...>
> <root>
> <design> ... </design>
> <library/>
> </root>
>
> library.xml
> <?xml ...>
> <library>
> ...
> </library>
>
> stylesheet.xml
> <?xml version="1.0">
> <xsl:stylesheet ... >
> <xsl:template match="root">
> <xsl:apply-templates select="design"/>
> <xsl:apply-templates select="document(library.xml)/"/>
> </xsl:template>
> </xsl:stylesheet>
>
> I might not have the syntax quite right, but I hope you get the idea.
> When the stylesheet is applied to design.xml, the processor will
> access and handle the external file as if it were part of the source
> document. For further info see the XSL section on handling multiple
> documents:http://www.w3.org/TR/xslt#document


Thanks for this explanation. I only have one question remaining:
How can I perform xsl:apply-templates to the union of the design and
the library together?
(There aren't any templates that directly match elements in the
library. There template that matches the design element gets values
from the library when it needs them (using <xsl:variable name="..."
select="...">).

Johan.

 
Reply With Quote
 
fildpauz
Guest
Posts: n/a
 
      02-21-2007
> Thanks for this explanation. I only have one question remaining:
> How can I perform xsl:apply-templates to the union of the design and
> the library together?
> (There aren't any templates that directly match elements in the
> library. There template that matches the design element gets values
> from the library when it needs them (using <xsl:variable name="..."
> select="...">).


Unless you have some reason not to do so, you can put templates that
apply to the library nodes in the same stylesheet. Alternatively you
can put those templates in an independent stylesheet and include it in
the main stylesheet using xsl:include (http://www.w3.org/TR/
xslt#section-Combining-Stylesheets). However, I have a feeling that a
proper solution to this problem would involve namespaces but that is
getting beyond my (in)expertise.

Cheers,
fildpauz

 
Reply With Quote
 
Johan
Guest
Posts: n/a
 
      02-21-2007
On 21 feb, 12:41, "fildpauz" <(E-Mail Removed)> wrote:
> > Thanks for this explanation. I only have one question remaining:
> > How can I perform xsl:apply-templates to the union of the design and
> > the library together?
> > (There aren't any templates that directly match elements in the
> > library. There template that matches the design element gets values
> > from the library when it needs them (using <xsl:variable name="..."
> > select="...">).

>
> Unless you have some reason not to do so, you can put templates that
> apply to the library nodes in the same stylesheet. Alternatively you
> can put those templates in an independent stylesheet and include it in
> the main stylesheet using xsl:include (http://www.w3.org/TR/
> xslt#section-Combining-Stylesheets). However, I have a feeling that a
> proper solution to this problem would involve namespaces but that is
> getting beyond my (in)expertise.


I don't have templates that match the library nodes. I only have
templates for the design. These templates 'get' a value from the
library when they need it (with xsl:variable).
So
<xsl:apply-templates select="document(library.xml)/"/>
doesn't match anything. That's why I want to call 'apply-templates'
and 'select' both the design and the library at the same time. So I
want the union of "design" and "document(library.xml)". Is there a way
to do this?

 
Reply With Quote
 
Johan
Guest
Posts: n/a
 
      02-21-2007
On 21 feb, 11:30, (E-Mail Removed) wrote:
> On Feb 21, 11:45 am, "Johan" <(E-Mail Removed)> wrote:
>
> > design.xml:
> > <?xml .....>
> > <root>
> > <design>....</design>
> > <library>....</library>
> > </root>

>
> > Currently the 'library' element is inside the
> > 'design.xml' file. I want to put the library element into
> > a separate xml file and include it in all my design
> > files. (I have a lot of designs and only a couple of
> > libraries).

>
> > The design.xml file contains a reference to an XSLT file
> > that visualizes the design.

>
> I don't believe what you want is possible, but there are
> other solutions to the problem; document() on the XSLT side
> in particular comes to mind. Proof-of-concept:
>
> xform1.xml:
>
> <?xml-stylesheet type="text/xsl" href="xform.xsl"?>
> <root library="1">
> <design>1</design>
> </root>
>
> xform_lib_1.xml:
>
> <library>
> <design n="1">A</design>
> </library>
>
> xform.xsl:
>
> <xsl:stylesheet version="1.0"
> xmlnssl="http://www.w3.org/1999/XSL/Transform">
> <xsl:variable
> name="lib"
> select=
> "
> document(concat('xform_lib_',/root/@library,'.xml'))
> "/>
> <xsl:template match="root">
> <result>
> <xsl:apply-templates/>
> </result>
> </xsl:template>
> <xsl:template match="design">
> <xsl:value-of
> select="$lib/library/design[@n=current()]"/>
> </xsl:template>
> </xsl:stylesheet>
>
> Note that client-side transformations in general should be
> avoided, unless you know very well what you're doing. So,
> unless you know very well what you're doing, consider
> transforming your document server-side.


Thanks for your help, but there is one part that I don't understand:
<xsl:template match="design">
<xsl:value-of
select="$lib/library/design[@n=current()]"/>
</xsl:template>

I think "xsl:value-of" element selects the value of the library/
design[n] element and add it to the output. Is that right? (I don't
want to do that).

I will describe my problem in a little bit more detail:
The <library> element contains one or more <type> elements. Each type
has an "id" attribute.
The author of the design.xml file can use type's from the library by
referring to their id.

Both the design and library (with all the types) used to be in the
same file, but now I want to put the <library> with all the <type>'s
in a separate file.


 
Reply With Quote
 
Johan
Guest
Posts: n/a
 
      02-21-2007
On 21 feb, 12:06, "Johan" <(E-Mail Removed)> wrote:
> On 21 feb, 11:11, "fildpauz" <(E-Mail Removed)> wrote:
>
>
>
> > > How can I include one XML file into another XML file (on the client
> > > side, in Firefox)?

>
> > I'm kind of new to XML, but let me take a stab at answering this.
> > Depending on what you want the client to do with the document, it
> > seems you might be able to accomplish this at the stylesheet level
> > using document().

>
> > design.xml
> > <?xml ...>
> > <root>
> > <design> ... </design>
> > <library/>
> > </root>

>
> > library.xml
> > <?xml ...>
> > <library>
> > ...
> > </library>

>
> > stylesheet.xml
> > <?xml version="1.0">
> > <xsl:stylesheet ... >
> > <xsl:template match="root">
> > <xsl:apply-templates select="design"/>
> > <xsl:apply-templates select="document(library.xml)/"/>
> > </xsl:template>
> > </xsl:stylesheet>

>
> > I might not have the syntax quite right, but I hope you get the idea.
> > When the stylesheet is applied to design.xml, the processor will
> > access and handle the external file as if it were part of the source
> > document. For further info see the XSL section on handling multiple
> > documents:http://www.w3.org/TR/xslt#document

>
> Thanks for this explanation. I only have one question remaining:
> How can I perform xsl:apply-templates to the union of the design and
> the library together?
> (There aren't any templates that directly match elements in the
> library. There template that matches the design element gets values
> from the library when it needs them (using <xsl:variable name="..."
> select="...">).
>
> Johan.


For clarification:
P.S. I want to 'include' the library.xml file into the design.xml file
before the XSLT processing happens (or at the start of the XSLT
processing before processing the <design> element).

P.S. #2. I have to use client-side processing. Local users should be
able to edit the design.xml file and visualize it in Firefox. If I
could use server side processing I could use XInclude.

Johan.


 
Reply With Quote
 
p.lepin@ctncorp.com
Guest
Posts: n/a
 
      02-21-2007
On Feb 21, 2:14 pm, "Johan" <(E-Mail Removed)> wrote:
> On 21 feb, 11:30, (E-Mail Removed) wrote:
> > On Feb 21, 11:45 am, "Johan" <(E-Mail Removed)> wrote:

>
> > > design.xml:
> > > <?xml .....>
> > > <root>
> > > <design>....</design>
> > > <library>....</library>
> > > </root>

>
> > > Currently the 'library' element is inside the
> > > 'design.xml' file. I want to put the library element
> > > into a separate xml file and include it in all my
> > > design files. (I have a lot of designs and only a
> > > couple of libraries).

>
> > > The design.xml file contains a reference to an XSLT
> > > file that visualizes the design.

>
> > <xsl:stylesheet version="1.0"
> > xmlnssl="http://www.w3.org/1999/XSL/Transform">
> > <xsl:variable
> > name="lib"
> > select=
> > "
> > document(concat('xform_lib_',/root/@library,'.xml'))
> > "/>
> > <xsl:template match="root">
> > <result>
> > <xsl:apply-templates/>
> > </result>
> > </xsl:template>
> > <xsl:template match="design">
> > <xsl:value-of
> > select="$lib/library/design[@n=current()]"/>
> > </xsl:template>
> > </xsl:stylesheet>

>
> there is one part that I don't understand:
> <xsl:template match="design">
> <xsl:value-of
> select="$lib/library/design[@n=current()]"/>
> </xsl:template>
>
> I think "xsl:value-of" element selects the value of the
> library/design[n] element and add it to the output. Is
> that right?


No, it selects a design element that has the n attribute
with the value equal to the value of the current node.

> (I don't want to do that).


>From your explanations it actually sounds to me as if

that's more or less what you want to do.

> I will describe my problem in a little bit more detail:
> The <library> element contains one or more <type>
> elements. Each type has an "id" attribute. The author of
> the design.xml file can use type's from the library by
> referring to their id.


Depending on exact way the author may 'use' the types from
the library, this might look similar to the following:

<xsl:template match="refer-to-lib-type">
<xsl:apply-templates
select="$lib/library/type[@id=current()/@id]"/>
</xsl:template>

The XML files would look like this:

<root library="library-identifier">
<design>
<refer-to-lib-type id="type-id-example"/>
</design>
</root>

<library>
<type id="type-id-example">
<whatever-needs-to-be-processed/>
</type>
</library>

--
Pavel Lepin

 
Reply With Quote
 
Johan
Guest
Posts: n/a
 
      02-21-2007
On 21 feb, 13:51, (E-Mail Removed) wrote:
> On Feb 21, 2:14 pm, "Johan" <(E-Mail Removed)> wrote:
>
>
>
> > On 21 feb, 11:30, (E-Mail Removed) wrote:
> > > On Feb 21, 11:45 am, "Johan" <(E-Mail Removed)> wrote:

>
> > > > design.xml:
> > > > <?xml .....>
> > > > <root>
> > > > <design>....</design>
> > > > <library>....</library>
> > > > </root>

>
> > > > Currently the 'library' element is inside the
> > > > 'design.xml' file. I want to put the library element
> > > > into a separate xml file and include it in all my
> > > > design files. (I have a lot of designs and only a
> > > > couple of libraries).

>
> > > > The design.xml file contains a reference to an XSLT
> > > > file that visualizes the design.

>
> > > <xsl:stylesheet version="1.0"
> > > xmlnssl="http://www.w3.org/1999/XSL/Transform">
> > > <xsl:variable
> > > name="lib"
> > > select=
> > > "
> > > document(concat('xform_lib_',/root/@library,'.xml'))
> > > "/>
> > > <xsl:template match="root">
> > > <result>
> > > <xsl:apply-templates/>
> > > </result>
> > > </xsl:template>
> > > <xsl:template match="design">
> > > <xsl:value-of
> > > select="$lib/library/design[@n=current()]"/>
> > > </xsl:template>
> > > </xsl:stylesheet>

>
> > there is one part that I don't understand:
> > <xsl:template match="design">
> > <xsl:value-of
> > select="$lib/library/design[@n=current()]"/>
> > </xsl:template>

>
> > I think "xsl:value-of" element selects the value of the
> > library/design[n] element and add it to the output. Is
> > that right?

>
> No, it selects a design element that has the n attribute
> with the value equal to the value of the current node.
>
> > (I don't want to do that).
> >From your explanations it actually sounds to me as if

>
> that's more or less what you want to do.
>
> > I will describe my problem in a little bit more detail:
> > The <library> element contains one or more <type>
> > elements. Each type has an "id" attribute. The author of
> > the design.xml file can use type's from the library by
> > referring to their id.

>
> Depending on exact way the author may 'use' the types from
> the library, this might look similar to the following:
>
> <xsl:template match="refer-to-lib-type">
> <xsl:apply-templates
> select="$lib/library/type[@id=current()/@id]"/>
> </xsl:template>
>
> The XML files would look like this:
>
> <root library="library-identifier">
> <design>
> <refer-to-lib-type id="type-id-example"/>
> </design>
> </root>
>
> <library>
> <type id="type-id-example">
> <whatever-needs-to-be-processed/>
> </type>
> </library>


I don't have templates that match the library (or the types inside
it), so
<xsl:apply-templates
select="$lib/library/type[@id=current()/@id]"/>
isn't going to match anything.

I think the solution from fildpauz fits my needs the best:

stylesheet.xml
<?xml version="1.0">
<xsl:stylesheet ... >
<xsl:template match="root">
<xsl:apply-templates select="design"/>
<xsl:apply-templates select="document(library.xml)/"/>
</xsl:template>
</xsl:stylesheet>

The only question that remains is this one:
"How can I perform xsl:apply-templates to the union of the design and
the library together?
(There aren't any templates that directly match elements in the
library. There template that matches the design element gets values
from the library when it needs them (using <xsl:variable name="..."
select="...">)). "

"So I want the union of "design" and "document(library.xml)". Is there
a way
to do this?"

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Including one C source file in another - witty or wicked ? Morris Dovey C Programming 10 03-08-2008 06:10 AM
Including one external JS file directly into another Neo Geshel Javascript 36 03-18-2007 09:39 PM
Help on including one XML document within another XML document using XML Schemas Tony Prichard XML 0 12-12-2003 03:18 PM
Using One XSLT and multiple XML Problem (One is XML and another one is XBRL) loveNUNO XML 2 11-20-2003 06:47 AM
including one aspx file in another aspx file vish ASP .Net 2 07-16-2003 10:29 PM



Advertisments