DTD Question surrounding duplicate child elements

D

Doug Estep

Below is a sample of my XML…

<MetaData>
<Table name="mytable">
<PrimaryKey name="pk"><Column name="mycolumn"/></PrimaryKey>
</Table>
</MetaData>

<Sequencer>
<Table name="mytable">
<Column name="mycolumn">
<Type>Object</Type>
</Column>
</Table>
</Sequencer>

Notice that I have two elements called, MetaData and Sequencer that
both have child elements called Table. How do I define the Table
child element in a DTD? Let me continue to explain what I mean. To
define the DTD for the two parent elements, I would do the following…

<!ELEMENT MetaData (Table+)>
<!ELEMENT Sequencer(Table+)>

Now I want to define the Table elements for both the MetaData and the
Sequencer elements. How do I do that when both child elements are
named Table. This is valid XML. But I can't see how I can create a
DTD for this XML when I can't define duplicate element names in a DTD.

I'm new to XML, so this may be a dumb question. I hope that it is,
because if this cannot be done, then defining a DTD is a worthless
exercise in any serious business application, in my opinion.

Your help is greatly appreciated. Please refrain from telling me how
I can work around this by structuring my XML in another way. I know
that I can do that. I am more interested in if it is possible to have
two parent elements with child elements that have the same name and be
able to define that in a DTD.

Thanks,
Doug
 
R

Richard Tobin

Doug Estep said:
Notice that I have two elements called, MetaData and Sequencer that
both have child elements called Table. How do I define the Table
child element in a DTD?

You can't have two elements with the same name and different content
models in the same DTD. In DTDs, an element type is determined by its
name.

XML Schemas allow elements with the same name to have different types
in different context, but DTDs don't.
I'm new to XML, so this may be a dumb question. I hope that it is,
because if this cannot be done, then defining a DTD is a worthless
exercise in any serious business application, in my opinion.

I hear that DTDs have been used in thousands of serious business
applications, but I suppose it's possible that you're right and
they're all wrong.

-- Richard
 
D

Doug Estep

Thanks for your help Richard. If the purpose of a DTD is to define
the rules for an XML file and the XML file can define child elements
for a parent which may have the same name as another parents child
elements.... and the DTD cannot handle that situation.... then I do
think there is a flaw in the DTD design.

My solution is to name my child nodes uniquely with regards to the
entire XML file. Certainly that can be done.

My comment about "Serious" business applications was directed at
situations where you have very large XML files -- which "serious"
business applications are sure to have. With a very large XML file, I
think it would be challenging to put together a DTD if the XML file
can never have a child element with the same name as another child
element within another parent.

But hey...nothing personal man!
 
P

Patrick TJ McPhee

% Thanks for your help Richard. If the purpose of a DTD is to define
% the rules for an XML file and the XML file can define child elements
% for a parent which may have the same name as another parents child
% elements.... and the DTD cannot handle that situation.... then I do
% think there is a flaw in the DTD design.

You keep leaving out `but different content models' when you say `have
the same name'. If you want to go around slagging DTDs, at least make
your description of the problem complete. DTDs were not designed to
accomodate this sort of document, but that doesn't make them worthless for
any serious application. Anyway, you can write a DTD which will validate
your document, but it will also validate documents which don't conform
to your design. I don't think it's possible to create a well-formed XML
document for which you can't create a matching DTD.

You should probably look at RELAX NG or W3C Schemas and see if they are
better for your purposes.
 
O

Oliver Bonten

Thanks for your help Richard. If the purpose of a DTD is to define
the rules for an XML file and the XML file can define child elements
for a parent which may have the same name as another parents child
elements.... and the DTD cannot handle that situation.... then I do
think there is a flaw in the DTD design.

My opinion is that it is a flaw in the XML Schema Design that it *does*
allow you to use context-dependent content models. (Or, in other words,
have two different elements with the same name.) This creates a lot of
headache and has no productive use, except to underline programmer's
preference for elegant solutions over comprehensible ones.

In DTDs, things are called by their names, and if two elements have the
same name, they are the same. You can have a TABLE in MetaData and a TABLE
in Sequencer, but they have to be interchangeable. You cannot define
different rules for TABLE in MetaData and for TABLE in Sequencer. If you
want to define different rules for TABLE in MetaData and for TABLE in
Sequencer, they are actually semantically different objects and should get
different names, such as MetaDataTable and SequencerTable.

Oliver
 

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,821
Latest member
AleidaSchi

Latest Threads

Top