Go Back   Velocity Reviews > Newsgroups > XML
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

XML - Learning XML.

 
Thread Tools Search this Thread
Old 09-11-2006, 02:29 AM   #1
Default Learning XML.


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!



justyb11@gmail.com
  Reply With Quote
Old 09-11-2006, 04:55 AM   #2
Joe Kesselman
 
Posts: n/a
Default Re: Learning XML.

wrote:
> 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.



--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
  Reply With Quote
Old 09-12-2006, 12:22 AM   #3
Peter Flynn
 
Posts: n/a
Default Re: Learning XML.

wrote:
> 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
--
XML FAQ: http://xml.silmaril.ie/
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump