Nodes with the same name in one XML doc

E

engwar

How do you created a DTD that will allow for node with the same name
at different places in the XML document?

Here's my XML

<parentNode>
<someNode>
<myNode/>
</someNode>
</parentNode>
<parentNode>
<someOtherNode>
<myNode/>
</someNode>
</parentNode>

Here's my dtd

<!ELEMENT parentNode (someNode|someOtherNode)
<!ELEMENT someNode (myNode)>
<!ELEMENT myNode (#PCDATA)>
<!ELEMENT someOtherNode (myNode)>
<!ELEMENT myNode (#PCDATA)>

The problem is that if both of these nodes can have a myNode child
node then the application I'm using to validate my XML (Oxygen XML
editor) says that my Element type "myNode" must not be declared more
than once.

I'm new to XML so this is probably easy. Is my dtd really incorrect
here?

Thanks.
 
J

Joseph Kesselman

engwar said:
How do you created a DTD that will allow for node with the same name
at different places in the XML document?

It looks like you're misunderstanding the structure of DTDs. DTDs are
*NOT* hierarchical; each element declaration's content model says what
that element can contain, and each declared element can occur wherever
some other element's content model says it's permitted/required. Just
remove the redundant definition of myNode, and the misleading indentation.

There are lots of examples of DTDs on the web; it might be a good idea
to look at some of them. It also might be a good idea to read a few XML
introductory documents rather than trying to guess your way through this
-- hereiwth, standard pointer to http://www.ibm.com/xml for both basic
tutorials and more advanced material.
 
P

Peter Flynn

How do you created a DTD that will allow for node with the same name at
different places in the XML document?

Here's my XML

<parentNode>
<someNode>
<myNode/>
</someNode>
</parentNode>
<parentNode>
<someOtherNode>
<myNode/>
</someNode>
</parentNode>

Here's my dtd

<!ELEMENT parentNode (someNode|someOtherNode)
<!ELEMENT someNode (myNode)>
<!ELEMENT myNode (#PCDATA)>
<!ELEMENT someOtherNode (myNode)>
<!ELEMENT myNode (#PCDATA)>

The problem is that if both of these nodes can have a myNode child node
then the application I'm using to validate my XML (Oxygen XML editor)
says that my Element type "myNode" must not be declared more than once.

You don't declare element types more than once. Once is enough. Just
remove the second declaration. And remove the TABs: as Joe pointed out,
DTDs are not hierarchical (in fact the element type declarations can
occur in any order so long as they satisfy all the necessary constraints
by the end of the DTD :)

You'll also need a root element type to enclose the whole thing, if you
want your parentNode to occur more than once.

///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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top