Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DTD

 
Shoba Ramachandran
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I found the following in w3schools.com
"
Elements with children (sequences)
Elements with one or more children are defined with the name of the children elements inside parentheses:
<!ELEMENT element-name
(child-element-name)>
or
<!ELEMENT element-name
(child-element-name,child-element-name,.....)>example:<!ELEMENT note (to,from,heading,body)>
<b>When children are declared in a sequence separated by commas, the children must appear in the same sequence in the document</b>.
"
But it accepts even when the child elements are not in the order specified in the dtd.
So which is correct?
Thanks
Shoba
 
Shashank Tanksali
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shobha,
By default, the validation is turned off in most parsers. So when you use the parser, you need to explicitly turn on the validation.
If you are using SAX2Count (Part of xerces distribution which you can download from the Apache web site ) to parse the XML file, then you can turn the validation on by specifying a command line like java sax.SAX2Count -v myxml.xml
In case you have your own program to read an XML document, then try
// Initialize XMLReader
myReader.setFeature("http://xml.org/sax/features/validation",true);
In your case, you appear to be using a validating parser, but have not turned the validation on. So it is currently only checking for well formedness.
Hope this helps . . .
Shashank
 
Shoba Ramachandran
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shashank,
I was using xml spy and the validation is on. It pointed out someother (validation)mistakes and didn't complaint about the order of the elements.
Thanks
Shoba
 
Hema Menon
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shoba, Ramachandran:
Shashank,
I was using xml spy and the validation is on. It pointed out someother (validation)mistakes and didn't complaint about the order of the elements.
Thanks
Shoba

XML spy would indeed complain if you change the order of the elements, if you do the validation against the DTD. Try it again.
 
Hema Menon
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if your DTD is defined as
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT message (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

and xml document is defined as
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE message SYSTEM " test.dtd">
<message>
<from>sobha</from>
<to>hema</to>
<heading>hi</heading>
<body>hello</body>
</message>
On validation, XMLSpy will complain saying that,
"Mandatory element "to" expected in place of "from"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic