Create XML tree from another partial tree

Discussion in 'XML' started by Peter Reimer, Apr 10, 2010.

  1. Peter Reimer

    Peter Reimer Guest

    Hi folks,
    I've got a problem and I thought I better ask before I start writing a
    solution for this.
    Perhaps, somebody else did the same or can point me to something.

    I have a DTD, for example:
    <!ELEMENT A (B*)>
    <!ELEMENT B (C)>
    <!ELEMENT C (#PCDATA)>

    and a part of an xml document:
    <B></B>

    and I want to get:
    <A><B><C>...</C></B></A>

    So, in words: When I have a part of an xml document, all elements
    needed for the dtd are generated around and in the partial document.

    Does somebody have a tip for me?
    Thanks in advance
    Peter
     
    Peter Reimer, Apr 10, 2010
    #1
    1. Advertising

  2. Peter Reimer wrote:
    > Hi folks,
    > I've got a problem and I thought I better ask before I start writing a
    > solution for this.
    > Perhaps, somebody else did the same or can point me to something.
    >
    > I have a DTD, for example:
    > <!ELEMENT A (B*)>
    > <!ELEMENT B (C)>
    > <!ELEMENT C (#PCDATA)>
    >
    > and a part of an xml document:
    > <B></B>
    >
    > and I want to get:
    > <A><B><C>...</C></B></A>
    >
    > So, in words: When I have a part of an xml document, all elements
    > needed for the dtd are generated around and in the partial document.



    What happens if e.g. B is allowed in the DTD as a child of e.g. A1 and
    A2? I am not sure it is always possible to come up with one particular
    document, given a certain DTD.

    --

    Martin Honnen
    http://msmvps.com/blogs/martin_honnen/
     
    Martin Honnen, Apr 10, 2010
    #2
    1. Advertising

  3. Peter Reimer

    Peter Reimer Guest

    On 10 Apr., 16:03, Martin Honnen <> wrote:
    > Peter Reimer wrote:
    > > Hi folks,
    > > I've got a problem and I thought I better ask before I start writing a
    > > solution for this.
    > > Perhaps, somebody else did the same or can point me to something.

    >
    > > I have a DTD, for example:
    > > <!ELEMENT A (B*)>
    > > <!ELEMENT B (C)>
    > > <!ELEMENT C (#PCDATA)>

    >
    > > and a part of an xml document:
    > > <B></B>

    >
    > > and I want to get:
    > > <A><B><C>...</C></B></A>

    >
    > > So, in words: When I have a part of an xml document, all elements
    > > needed for the dtd are generated around and in the partial document.

    >
    > What happens if e.g. B is allowed in the DTD as a child of e.g. A1 and
    > A2?


    Conflict ;)

    > I am not sure it is always possible to come up with one particular
    > document, given a certain DTD.


    Yes, I'm sure that there isn't always a solution for this.
    A possible solution must get a partial tree that can used in the DTD
    only in one place.

    Thanks
    Peter
     
    Peter Reimer, Apr 10, 2010
    #3
  4. Peter Reimer wrote:

    > A possible solution must get a partial tree that can used in the DTD
    > only in one place.


    I am not aware of any API that allows XML sample generation from a DTD
    but I mainly familiar with the Microsoft .NET APIs and those are very
    much focused on schema support and ignore DTDs other than allowing you
    to validate against one if necessary.
    For schema based sample creation with .NET 2.0 or later, although only
    from a complete schema and then down from possible root elements, there
    is a sample and an article explaining that sample online at
    http://msdn.microsoft.com/en-us/library/aa302296.aspx




    --

    Martin Honnen
    http://msmvps.com/blogs/martin_honnen/
     
    Martin Honnen, Apr 10, 2010
    #4
  5. Peter Reimer

    Peter Flynn Guest

    Peter Reimer wrote:
    > Hi folks,
    > I've got a problem and I thought I better ask before I start writing a
    > solution for this.
    > Perhaps, somebody else did the same or can point me to something.
    >
    > I have a DTD, for example:
    > <!ELEMENT A (B*)>
    > <!ELEMENT B (C)>
    > <!ELEMENT C (#PCDATA)>
    >
    > and a part of an xml document:
    > <B></B>


    That isn't actually valid as per your DTD; it would need to be at least
    <B><C/></B> or (presumably) <B><C>some stuff</C></B>

    > and I want to get:
    > <A><B><C>...</C></B></A>
    >
    > So, in words: When I have a part of an xml document, all elements
    > needed for the dtd are generated around and in the partial document.
    >
    > Does somebody have a tip for me?


    You can do this by using SGML temporarily, which has the ability to
    infer missing start and end tags from the DTD, eg this file:

    <!doctype A [
    <!element A o o (B*)>
    <!element B - - (C)>
    <!element C - - (#pcdata)>
    ]>
    <b>
    <c>stuff</c>
    </b>

    Run this through a normaliser like sgmlnorm (part of the SP package; or
    use onsgmlnorm from the OpenSP package):

    $ onsgmlnorm test.sgml
    <A>
    <B>
    <C>stuff</C>
    </B>
    </A>

    Result: it has deduced the missing bits. Those extra parameters in the
    SGML element type declarations control the allowability or otherwise of
    missing start-tags and end-tags (this facility was removed for XML).

    This is the trick that originally enabled SGML processing for HTML, so
    that this file:

    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <title>My page</title>
    <h1>All about me</h1>
    <p>This is my first web page

    is in fact a 100% fully valid SGML document, although it looks odd if
    you've only ever seen XML before. You can prove it by running it through
    onsgmlnorm, provided your CATALOG file supplies a copy of the original
    v1.28 HTML DTD [1] and a copy of the SGML Declaration for HTML which
    came with it:

    $ onsgmlnorm /usr/local/lib/sgml/sgmlhtml.dec index.html
    <HTML>
    <HEAD>
    <TITLE>My page</TITLE>
    </HEAD>
    <BODY><H1>All about me</H1>
    <P>This is my first web page</P></BODY>
    </HTML>

    Bingo! output this to a local file and make the element type names
    lowercase and you have XHTML :)

    ///Peter
    --
    XML FAQ: http://xml.silmaril.ie/

    1. $Id: html.dtd,v 1.28 1995/06/16 18:54:22 connolly Exp $
     
    Peter Flynn, Apr 12, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Billy
    Replies:
    2
    Views:
    529
    Billy
    Feb 1, 2006
  2. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,246
  3. Thomas Heller
    Replies:
    13
    Views:
    895
    Michele Simionato
    Feb 8, 2007
  4. J. Clifford Dyer

    Re: Partial 1.0 - Partial classes for Python

    J. Clifford Dyer, Feb 8, 2007, in forum: Python
    Replies:
    0
    Views:
    540
    J. Clifford Dyer
    Feb 8, 2007
  5. Peter Mueller
    Replies:
    6
    Views:
    4,722
    Stefan Ram
    Jan 13, 2008
Loading...

Share This Page