Create XML tree from another partial tree

P

Peter Reimer

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
 
M

Martin Honnen

Peter said:
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.
 
P

Peter Reimer

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
 
M

Martin Honnen

Peter said:
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
 
P

Peter Flynn

Peter said:
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
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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top