DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2010

    another (?) xml include question

    Hi. I've read a lot about including one xml file into another, including on this site, but I'm still having trouble with it.

    I have XML files that use an XSL stylesheet to show up nicely in my browser. The XSL uses a CSS file to enhance the output on my screen. All these files (XMLs, XSL, and CSS) are on my harddrive. I use Firefox and Notepad++.

    When I open one of these XML files in Firefox, I couldn't be happier with the result. However, each XML file represents 1 item. I would like to be able to create an XML file that includes a bunch of items.

    If I copy/paste a bunch of different XML files into 1 XML file, it works great. But I don't want to do that; I want to use includes. Essentially, I want to have a separate "item1.xml", "item2.xml", and "listOfItems.xml". When I edit "item1.xml", I want the changes to show up in "listOfItems.xml".

    I do not have a schema or dtd. I made up my XML structure as I went and updated my XSL file accordingly. (It's for my own hobby use so I don't have to worry about clients getting mad!)

    My XML looks like this:
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="scripts\blockTemplate.xsl"?>
            ... bunch of xml tags
    If I want to manually combine a list of items it looks like this:
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="scripts\blockTemplate.xsl"?>
            ... tags for item 1
            ... tags for item 2
    My XSL starts off like this
    <?xml version="1.0" ?>
    <xsl:stylesheet    version="1.0"
        ...(with tons of functions/templates, etc)
    I have tried lots of alternatives, but the one that seemed most promising was something like this (as an XML file):
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="scripts\blockTemplate.xsl"?>
    <!DOCTYPE rootElement [
    <!ENTITY item1 SYSTEM "item1.xml">
    I have tried many things and combinations of things. For example:
    • "item1.xml" now has no xml header and starts with <item>.
    • I have tried messing around with my "xsl:template match" line in the XSL file.
    • I have tried changing the !DOCTYPE to "item" (instead of "rootElement").
    • I've tried taking out the xml-stylesheet line from the "combo-xml" file.

    Can someone please tell me what I'm doing wrong? Like I said, I've tried a bunch of different techniques that I've found all around the internet.

    Thank you in advance!!!

  2. #2
    Join Date
    May 2009
    United Kingdom
    This is a very useful reference for the subject:

  3. #3
    Join Date
    Sep 2010
    Thank you for replying! I found this exact page during my search, in fact it's where I got the method that I posted above.

    I also tried his "xi:include" method but I couldn't get that to work either. Using all the methods I've tried, the page always comes up blank.

    In the article he puts the "<book xmlns:xi= ...>" declaration directly below the xml declaration. I had my xml-stylesheet declaration between them. Would that have caused the problem?

    I have another question too: all these files are on my hard drive, and I'm not running a Web server ... is THAT the problem? (That drove me crazy when I was trying to figure out cgi form scripts!!)

    Thanks again!!

  4. #4
    Join Date
    May 2009
    United Kingdom
    The processing section on page 2 (http://www.xml.com/pub/a/2002/07/31/...de.html?page=2) details that an XInclude aware processor would need to be used and flagged to process these.

    Alternatively, this could be handled manually if you cannot use an XInclude aware processor.

    Each xi:include element could be processed to build up a new XML file via the document method and then the entire resultant node set could be re-processed using the exslt node-set method. Not necessarily pretty but it could all be done in a single stylesheet without the need for an XInclude aware processor.

    Through the use of:

    ...and possibly:

  5. #5
    Join Date
    Sep 2010
    Thank you thank you thank you.

    I don't know why, but apparently I blanked out while reading that "processing" paragraph. The first two sentences clear everything up: "XInclusion is not part of XML 1.0 or the XML Infoset. XML parsers do not perform inclusions automatically." Personally, I think this should have been at the START of the article, but whatever.

    By "handled manually" do you mean copy/pasting the xml data between files? I've done that, but obviously that doesn't help if I want to change the original source (I'd have to manually change all manual inclusions, which defeats the very purpose of an include!).

    Also, I can't get the !DOCTYPE/!ENTITY method to work for me either. Does this method fall under the same "not really a part of XML" problem as the xi:include method? Or does there need to be a DTD to use it?

    Finally, it seems to me that "basic inclusion" (or "automatic text replacement at parsing") should be built in from day one. Is there a basic method that everyone learns in XML 101?? I'm going to try the first link you suggested when I have some time ... the second link is quite a bit over my head (at least for now!).

    Thanks again!

  6. #6
    Join Date
    May 2009
    United Kingdom
    Personally I do not like working with DTD's, therefore I am sure about how to use the alternative method.

    However, by manual, I still meant using XSLT!

    The following is an example set of XSLT which uses modes to first process the document with the XIncludes. It will resolve them into a variable named document, then reprocess these (dependant upon which processor you are using) in the mode final. The mode final is where you could use your own XSLT, it is just an example. The mode final is not required.

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0"
      exclude-result-prefixes="xsl msxslt exslt xalan">
      <xsl:output method="xml" indent="yes"/>
      <xsl:template match="/">
        <!-- Process and store into a node set -->
        <xsl:variable name="document">
          <xsl:apply-templates mode="get-includes" />
        <!-- Process the full node set -->
          <xsl:when test="function-available('msxslt:node-set')">
            <xsl:apply-templates select="msxslt:node-set($document)" mode="final" />
          <xsl:when test="function-available('exslt:node-set')">
            <xsl:apply-templates select="exslt:node-set($document)" mode="final" />
          <xsl:when test="function-available('xalan:nodeset')">
            <xsl:apply-templates select="xalan:nodeset($document)" mode="final" />
            <xsl:apply-templates select="$document" mode="final" />
      <!-- Identity Template used to copy everything... -->
      <xsl:template match="@* | node()" mode="get-includes">
          <xsl:apply-templates select="@* | node()" mode="get-includes" />
      <!-- XInclude match template used to process the referenced document -->
      <xsl:template match="xi:include" mode="get-includes">
        <xsl:apply-templates select="document(@href)" mode="get-includes" />
      <!-- FINAL -->
      <xsl:template match="/" mode="final">
            <xsl:apply-templates mode="final" />
      <xsl:template match="item" mode="final">
          <xsl:value-of select="@name" />

Similar Threads

  1. Replies: 3
    Last Post: 08-07-2001, 03:32 PM
  2. XML tutorials - fyi
    By Jeff Condon in forum XML
    Replies: 0
    Last Post: 05-29-2001, 02:25 PM
  3. Data Junction Announces XML Junction 7.51
    By Tim Frost in forum xml.announcements
    Replies: 0
    Last Post: 04-02-2001, 10:53 AM
  4. XML & ASP Question
    By zack in forum Enterprise
    Replies: 0
    Last Post: 03-13-2001, 10:44 AM
  5. XML Parsing Performance Question
    By Carlos De La Paz mexicanopelon in forum XML
    Replies: 1
    Last Post: 06-30-2000, 12:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center