Learning XML.

J

justyb11

Hi everyone,

I'm learning XML and have read a couple of site on the topic, but I'm
wondering how to implement something like this in a DTD.


<root>
<header>
<name> Some data </name>
<date month="12", day="01", year="2005" />
<author name="Bob" />
<note> Bob wrote this. </note>
</header>
</root>

However, the <name> tag is required, but all the others are optional.
Also, the name tag must appear first but it does not matter what order
the other fields appear in or if they even appear at all. So another
example of the document could be...

<root>
<header>
<name> Another Example </name>
<author name="Sue" />
</header>
</root>

or...

<root>
<header>
<name> A different example. </name>
<author name="Joe" />
<note> Quick example for you to look at. </note>
<date month="1", day="1", year="2004" />
</header>
</root>

I've come up with this much so far.

<!ELEMENT root (header)>
<!ELEMENT header (name, (author | note | date)?)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT author EMPTY>
<!ELEMENT note (#PCDATA)>
<!ELEMENT date EMPTY>

<!ATTLIST author name CDATA #REQUIRED>
<!ATTLIST date month CDATA #REQUIRED>
<!ATTLIST date day CDATA #REQUIRED>
<!ATTLIST date year CDATA #REQUIRED>

I would also like to include that the author, note, and date element
can be used once or not at all. And if you use one of those elements
then you must use the the attributes listed.

I thank you all in advance for any help. Also if you could suggest
some tool that I can use in Linux to help me with XML and validation.

Thanks!
 
J

Joe Kesselman

I would also like to include that the author, note, and date element
can be used once or not at all.

In DTDs, that kind of constraint -- any order but only one instance --
is painful to express. You wind up having to enumerate all the possible
sequences. Not difficult, but verbose and a pain to maintain.

Suggestions:

1) Abandon DTDs and move to XML Schemas, which are a more powerful
constraint language.

or

2) Insist that the contents appear in a specified order. There really
isn't any strong advantage to allowing the any-order flexibility unless
the order is going to be meaningful -- which, in this case, it clearly
isn't. Programs can easily generate stuff in the right order, and so can
humans once they stop grumbling that you should have let them be lazy.
If you want to be kind to them, provide a tool (eg an XSLT stylesheet)
which will help get the documents into valid form.


Note that there are likely to be be some constraints in a serious XML
application that can't be expressed in either DTD or schema. These
formalisms are very useful and as documentation for what your program
expects, and as an initial sanity-check layer ... I like to think of it
as "higher level syntax" ... but the program itself is often going to
have to do some additional checking to enforce specific semantic
constraints.
 
P

Peter Flynn

Hi everyone,

I'm learning XML and have read a couple of site on the topic, but I'm
wondering how to implement something like this in a DTD.


<root>
<header>
<name> Some data </name>
<date month="12", day="01", year="2005" />
<author name="Bob" />
<note> Bob wrote this. </note>
</header>
</root>

However, the <name> tag is required, but all the others are optional.
Also, the name tag must appear first but it does not matter what order
the other fields appear in or if they even appear at all.

If the order is not important, then fix it:

<!ELEMENT root (header)>
<!ELEMENT header (name,author?,note?,date?)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT author EMPTY>
<!ELEMENT note (#PCDATA)>
<!ELEMENT date EMPTY>

<!ATTLIST author name CDATA #REQUIRED>
<!ATTLIST date month CDATA #REQUIRED
day CDATA #REQUIRED
year CDATA #REQUIRED>

Or perhaps simpler, <!ATTLIST date YYYY-MM-DD CDATA #REQUIRED>

The "any order but once each" facility in SGML was removed from XML
to make it simpler. Joe posted some good suggestions about Schemas.

///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,821
Messages
2,569,748
Members
45,726
Latest member
RaleighAll

Latest Threads

Top