another (?) xml include question


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: another (?) xml include question

  1. #1
    Join Date
    Sep 2010
    Posts
    8

    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:
    Code:
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="scripts\blockTemplate.xsl"?>
    <rootElement>
        <item>
            ... bunch of xml tags
        </item>
    </rootElement>
    If I want to manually combine a list of items it looks like this:
    Code:
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="scripts\blockTemplate.xsl"?>
    <rootElement>
        <item>
            ... tags for item 1
        </item>
        <item>
            ... tags for item 2
        </item>
    </rootElement>
    My XSL starts off like this
    Code:
    <?xml version="1.0" ?>
    <xsl:stylesheet    version="1.0"
                            xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        ...(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):
    Code:
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="scripts\blockTemplate.xsl"?>
    <!DOCTYPE rootElement [
    <!ENTITY item1 SYSTEM "item1.xml">
    ]>
    
    
    <rootElement>
        &item1;
    </rootElement>
    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
    Location
    United Kingdom
    Posts
    49
    This is a very useful reference for the subject:
    http://www.xml.com/pub/a/2002/07/31/xinclude.html

  3. #3
    Join Date
    Sep 2010
    Posts
    8
    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
    Location
    United Kingdom
    Posts
    49
    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:
    http://www.w3schools.com/Xsl/func_document.asp

    ...and possibly:
    http://www.xml.com/pub/a/2003/07/16/nodeset.html

  5. #5
    Join Date
    Sep 2010
    Posts
    8
    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
    Location
    United Kingdom
    Posts
    49
    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.

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xi="http://www.w3.org/2001/XInclude"
      xmlns:exslt="http://exslt.org/common"
      xmlns:xalan="http://xml.apache.org/xalan"
      xmlns:msxslt="urn:schemas-microsoft-com:xslt"
      extension-element-prefixes="exslt"
      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" />
        </xsl:variable>
        
        <!-- Process the full node set -->
        <xsl:choose>
          <xsl:when test="function-available('msxslt:node-set')">
            <xsl:apply-templates select="msxslt:node-set($document)" mode="final" />
          </xsl:when>
          <xsl:when test="function-available('exslt:node-set')">
            <xsl:apply-templates select="exslt:node-set($document)" mode="final" />
          </xsl:when>
          <xsl:when test="function-available('xalan:nodeset')">
            <xsl:apply-templates select="xalan:nodeset($document)" mode="final" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="$document" mode="final" />
          </xsl:otherwise>
        </xsl:choose>
    
      </xsl:template>
      
      <!-- Identity Template used to copy everything... -->
      <xsl:template match="@* | node()" mode="get-includes">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()" mode="get-includes" />
        </xsl:copy>
      </xsl:template>
    
      <!-- 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" />
      </xsl:template>
      
      <!-- FINAL -->
      <xsl:template match="/" mode="final">
        <html>
          <head>
          </head>
          <body>
            <xsl:apply-templates mode="final" />
          </body>
        </html>
      </xsl:template>
      
      <xsl:template match="item" mode="final">
        <p>
          <xsl:value-of select="@name" />
        </p>
      </xsl:template>
      
    </xsl:stylesheet>

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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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