How to include XML in XML file?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Thread: How to include XML in XML file?

Hybrid View

  1. #1
    Join Date
    May 2006
    Posts
    36

    How to include XML in XML file?

    Hi there

    I guess this question has been asked to death - so here I bring it up again.....

    I have this XML page and right now there is an <about> tag that contains information the page name and copyright information.

    However the copyright information contains something like 1998-2006 and so instead of going through each XML page to update the year, I would like to extract this whole <about> tag into another file, which each XML can then include. However the <about> tag also contains the module id, e.g. mEmployeeDetail.

    So how should I do this then? I am new to XML and worse yet I'm maintaining someone's XML code.

    The <about> is pretty straightforward so far, such as

    Code:
    <about 
             style="width:100%;
                      font-size:10pt;font-family:Verdana,Helvetica,Arial,Sans Serif;
                      font-weight:bold;
                      font-style:normal;display:'none';" >
        <span id="aboutlabel"
               title="Copyright 1995-2006 Something Ltd."
               style="position:absolute;top:165px;left:125px;
                         font-style:normal;
                         font-family:Verdana,Helvetica,Arial;
                         font-weight:normal;">
       Copyright 1995 - 2006 Something Ltd.
      </span>
    
      <span id="aboutlabel"
                title="Copyright 1995-2006 Something Ltd.""
                style="position:absolute;top:225px;left:125px;
                         font-style:normal;
                         font-family:Verdana,Helvetica,Arial;
                         font-weight:normal;z-index:1;">
       Module ID: mEmployeeDetails
      </span>
     </about>
    And I hope to copy that into a separate file (xml) and pass in the ModuleID.

    How do I even do that? BTW I have tried googling and I think my XML at work does not support things like xi:include (gosh, I don't even know what these are)

    Thanks a lot
    Calgarychinese

  2. #2
    Join Date
    Aug 2006
    Posts
    2

    use entities in dtd

    Hello,

    To do this, you could create a DTD with an entity definition, like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <!ELEMENT about (span+)>
    <!ATTLIST about
    style CDATA #REQUIRED
    >
    <!ELEMENT span (#PCDATA)>
    <!ATTLIST span
    id CDATA #REQUIRED
    title CDATA #REQUIRED
    style CDATA #REQUIRED
    >
    <!ENTITY copy '1999 - 2006'>

    To refer to the DTD and use the entities change your xml to:
    <!DOCTYPE about SYSTEM "copy.dtd">
    <about style="width:100%;
    font-size:10pt;font-family:Verdana,Helvetica,Arial,Sans Serif;
    font-weight:bold;
    font-style:normal;display:'none';">
    <span id="aboutlabel" title="Copyright &copy; Something Ltd." style="position:absolute;top:165px;left:125px;
    font-style:normal;
    font-family:Verdana,Helvetica,Arial;
    font-weight:normal;">
    Copyright &copy; Something Ltd.
    </span>
    <span id="aboutlabel" title="Copyright &copy; Something Ltd." style="position:absolute;top:225px;left:125px;
    font-style:normal;
    font-family:Verdana,Helvetica,Arial;
    font-weight:normal;z-index:1;">
    Module ID: mEmployeeDetails
    </span>
    </about>


    Have fun

  3. #3
    Join Date
    May 2006
    Posts
    36
    Hi marknauta

    Thanks for the reply!!

    Although I'm having a bit of a problem here.

    My <about> is not at the top of the page, so if I put that right before my <about> tag I got an error:

    Cannot have a DOCTYPE declaration outside of a prolog.

    So I moved <!DOCTYPE> to the top, and then I got another message

    The name of the top most element must match the name of the DOCTYPE declaration.

    Hope you can continue helping me out.

    Thanks
    Last edited by calgarychinese; 08-21-2006 at 02:59 PM.

  4. #4
    Join Date
    Aug 2006
    Posts
    5

    XML Entities

    There are all kinds of "entities" in HTML/XHTML "nbsp" is one of them. You should be able to see that &nbsp; calls this entity and inserts it into a document. With any XML document, you can define your own entities. If the top level element is called <root>, then you can make a simple Doctype declaration without DTD or schema as follows

    <xml version="1.0">

    <!DOCTYPE root [
    <!ENTITY about SYSTEM "path/to/about.xml">
    <!ENTITY other SYSTEM "path/to/other.xml">
    ]>

    <root>
    &about;
    &other;
    </root>


    adding schema or dtd is also quite simple but what you want to google for is called XML Entities

  5. #5
    Join Date
    May 2006
    Posts
    36
    Hi there, thanks a lot for your reply.

    So if my original XML pages looks like this:

    Code:
    <page>
    <part1 id="pt1">
    <part2 id="pt2">
    </part2>
    </part1>
    <part3 id="pt3"/>
    <part4 id="pt4"/>
    <about />
    </page>
    Then will my DOCTYPE look like this:

    Code:
    <xml version="1.0">
    <!DOCTYPE page [
    <!ELEMENT page (#PCDATA)>
    <!ENTITY part1>
    <!ENTITY part2>
    <!ENTITY part3>
    <!ENTITY part4>
    <!ENTITY about SYSTEM "./copyright.dtd">
    ]>
    and do I need to put in each of part1, part3, part4, etc. inside this DOCTYPE tag?

    Thanks
    Last edited by calgarychinese; 08-21-2006 at 04:18 PM.

  6. #6
    Join Date
    Aug 2006
    Posts
    5
    Hello,

    Not quite, its easier than that. You just make the file you want to include an entity and then place it using &entityname; So:

    <!DOCTYPE page [
    <!ENTITY about SYSTEM "about.xml">
    ]>

    <page>
    <part1>
    <part2>
    </part2>
    </part1>
    <part3 />
    <part4 />
    &about;
    </page>

    then when the document is parsed, &about; is replaced with the contents of about.xml
    Notice that I haven't used DTD at all here, it is not (really)relevant to your question. I have replaced copyright.dtd with the file to be included (see red). If you want the document to be valid, then you need a schema or dtd for "page" and the included about must also comply with that schema seeing as you are including it in the "page" document. Try without validation first and see the include in effect, then tackle the second problem of validation.

    When you get to step 2, your doctype will look similar to this XHTML example:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" [
    <!ENTITY xhtmlInclude SYSTEM "includedFile.html">
    ]>
    hope this helps

  7. #7
    Join Date
    May 2006
    Posts
    36
    Hi domlogic

    Thanks for your speedy response. i'm still getting Cannot have multiple DOCTYPE declarations error. I'll look more into it.

    However, I guess the biggest concern I have re: your approach is actually the source of my problem: if I include the about.xml, does it mean the whole <about> tag will be embedded into this xml file?

    Each xml that includes this file has its specific moduleID and this string needs to be displayed in the copyright information too. If you make this a separate about.xml then isn't it now too generic, and I can't change this string from page to page anymore, isn't it?

    Thanks, as I said, I'll spend more time into this - I guess this is the learning curve of XML that I have to conquer to really understand all the relationships among entity, doctype, etc. etc.

    Thanks

  8. #8
    Join Date
    May 2006
    Posts
    36
    Hi there, I have somewhat made some more progress.

    Now I have my about.xml as suggested earlier. In the module Name section there, I replace that string "mEmployeeDetails" with "&moduleID;"

    and at the top of my xml page that includes this about.xml I have:

    <!DOCTYPE page [
    <!ENTITY moduleID "MyModuleName">
    <!ENTITY about SYSTEM "about.xml">
    ]>

    And my XML looks something like this, as said before:

    <page>
    <part1>
    <part2>
    </part2>
    </part1>
    <part3/>
    &about;
    </page>

    THe XML actually looks fine when I open it under the browser, such as

    http://localhost/myTest/employee.xml

    I can see that the about section is what I have expected, and the moduleID is now saying MyModuleName, all good.

    But in my VB script code that does the transformation (from xml to html), I have these two lines:

    Code:
      set xdEmp = server.createobject("msxml2.FreeThreadedDOMDocument.3.0")
      xdEmp.load(Server.MapPath(xmlpage & ".xml"))
    where xmlpage = "employee" in this case, and I got this error:

    xdEmp.ParseError.Reason = ""The element 'page' is used but not declared in the DTD/Schema."


    So does it mean I still have to provide the DTD for this employee.xml in detail?

    Thanks

  9. #9
    Join Date
    Aug 2006
    Posts
    5
    Hi ya


    I am glad you have your includes working.

    Well, I transform my xml using an xslt which can either be run in the browser or prior to serving, using an xslt processor like xalan or ant task (in my world). Unfortunately while I can help you with including external entities in your DOM tree, I cannot offer you support on the msxml parser or vb script as I don't use them.

    It does seem that your parser wants a valid xml file which leaves you with two options.

    1. Elect to use a parser which checks for well formedness but not validity (this may be an option with your parser), or

    2. Write a schema and declare your document to adhere to it.

    Eventually you will have to write a schema or dtd to make your document valid.

    I reccomend tackling lots of small problems rather than one big problem. So I would start by writing a schema which has very limited restrictions to appease the parser and then refining your schema to meet the requirements. Other people will reccomend different approaches based on different philosophies.

    If you become schema savvy, maybe you can help me with my xsd:key issue which I posted to this forum

    Stick with it and good luck
    Last edited by domlogic; 08-21-2006 at 06:38 PM. Reason: spaces are good

  10. #10
    Join Date
    May 2006
    Posts
    36
    Hi domlogic, thanks for your advice.

    I just feel completely defeated. I am encountering some related issues, not sure if you could help too, just cos I think they may not have to do with which parser I use.

    So I have this about.xml. I use this code to combine the contents into the resulting HTML file.

    dim copyNode
    for each node in AboutNode.childNodes
    set copyNode = node.Clone()
    parentNode.appendchild(copyNode)
    next

    It is fine except in the resulting html, my entity is not replaced. Remeber I told you there's a place in the <about> that I need to have an entity referencing the module ID?

    So in the original XML file I have this:

    <!DOCTYPE page [
    <!ENTITY about SYSTEM "about.xml">
    <!ENTITY moduleID "EmployeeName">
    ]>

    and inside my about.xml I have something like this:
    <span>&moduleID;</span>

    but as a result, it gets blanked. There is nothing. I used the debugger and I see something like NodeTypedValue which contains the string "EmployeeName", but then after append the string is gone.

    AM I making sense?

    Any idea what went wrong? Been over a week since I've been struggling this now... and I thought it would be easy.....

    Thanks
    Last edited by calgarychinese; 08-22-2006 at 02:57 PM.

  11. #11
    Join Date
    Aug 2006
    Posts
    5
    This is starting to look like a problem for xslt or some other programmatic method but this thread topic is about including xml from one file into another. The simple answer to your latest post is that you don't need to attach nodes or do anything with VB except load the "bigger" xml/xhtml file and parse it. So yes, it is already included with those ENTITY statements.

    The more complicated answer is that you need some way of changing the content in that file you are including, so this is not the best method. You probably want to make the html file and convert it into an xslt. Then you have an xml file which has the moduleID and other variable data.

    about.xml:
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="page.xsl"?>
    <about>
    <copyrightInfo>1996 to 2000</copyrightInfo>
    <moduleID>Employee</moduleID>
    </about>

    page.xsl:
    <?xml version"1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <html>
    <head>
    <title>This is the page for <xsl:value-of select="about/moduleID" /></title>
    </head>
    <body>
    <p>This is stuff on each page</p>
    <p>Copyright: <xsl:value-of select="about/copyrightInfo" /></p>
    <p>ModuleID: <xsl:value-of select="about/moduleID" /></p>
    </body>
    </xsl:template>

    I just hand wrote that off the top of my head but if you save the text into xml files named as per red text above, then open the about.xml in a modern web browser, you should get the picture.

    I hope that this helps rather than complicates matters for you

  12. #12
    Join Date
    May 2006
    Posts
    36
    Hi domlogic, thanks a lot for your help. The battle is still on, but I do really need to say thanks to you again and again, cos you have helped me all along.

    CC

Similar Threads

  1. updating xml file from VB6
    By smithg22 in forum VB Classic
    Replies: 9
    Last Post: 04-12-2005, 07:23 PM
  2. Help on Formating XML file ??
    By serge calderara in forum .NET
    Replies: 3
    Last Post: 04-12-2005, 02:10 PM
  3. How do i search within a xml file?
    By Kelvin Teh in forum XML
    Replies: 3
    Last Post: 02-16-2001, 11:09 AM
  4. Display xml file
    By rana in forum XML
    Replies: 5
    Last Post: 12-10-2000, 12:50 AM
  5. Problem Converting ADO Recordset to XML
    By CHRISTOS STAVRINOU in forum VB Classic
    Replies: 0
    Last Post: 11-16-2000, 05:58 PM

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