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)>

    and a part of an xml document:

    and I want to get:

    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 Reimer, Apr 10, 2010
    1. Advertisements

  2. 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, Apr 10, 2010
    1. Advertisements

  3. Peter Reimer

    Peter Reimer Guest

    Conflict ;)
    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.

    Peter Reimer, Apr 10, 2010
  4. 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
    Martin Honnen, Apr 10, 2010
  5. Peter Reimer

    Peter Flynn Guest

    That isn't actually valid as per your DTD; it would need to be at least
    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)>

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

    $ onsgmlnorm test.sgml

    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:

    <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
    <TITLE>My page</TITLE>
    <BODY><H1>All about me</H1>
    <P>This is my first web page</P></BODY>

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

    Peter Flynn, Apr 12, 2010
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.