Write Simple DTD

Pēdējais mainījis superadmin 2018-01-12 21:33

Write Simple DTD

Public source: http://java-eim.googlecode.com/svn/trunk/java-eim-demo-xmlsamples Technologies: Java (J2SDK 1.6), Ant and Maven2 build tools, JDeveloper IDE, Xerces XML library, Sun MSV Estimated time: 15 minutes

Use Ant to validate XML and correct validation errors. Compare behavior of the default validator (e.g. "validate" task in Ant, which relies on Xerces) versus fail-fast validator (e.g. Sun Multi-schema validator). Define "PUBLIC" DTD locations locally (rather than getting them from the Internet, which may slow down the parsing process considerably). 

General Description and Scope

Given a simple XML structure, write a DTD grammar for it, so that the structure is reflected by your grammar (and anything else, which does not show up in the sample document, is forbidden). 

<title>Simple questions about XML&lt;/title>
<question id="q01">
<content>Attributes in an XML file are delimited by these characters:&lt;/content>
<var>Single quotes - '\''&lt;/var>
<var>Double quotes - '"'&lt;/var>
<var correct="1">Either single or double quotes&lt;/var>
<question id="q01">
<content>How many root elements may an XML document have?&lt;/content>
<var correct="1">There is exactly one root element&lt;/var>
<var>There can be any number of root elements&lt;/var>
<var>XML data in general does not have a root (it is a general graph, not a tree)&lt;/var>


  1. The above file can be found in the directory java-eim-demo-xmlsamples/src/test/resources/validation/module.xml
  2. Create another file - module.dtd in the same directory and fill it wiht appropriate content. It should have declarations for all 4 element-types found in the given XML, and also two attribute declarations after their respective elements. Notice that your DTD can capture the basic structure (i.e. which element is included under another one; and where do the attributes belong). But one cannot introduce some reasonable limitations, e.g. requirement, that only one answer variant may be correct. 
  3. Add a new target to the Ant script (java-eim-demo-xmlsamples/build.xml), which would validate the file module.xml. (Simply uncomment one target at the end of the build.xml file). The target looks like this: 
<target name="failfast-validate-for-module"
   description="Check files against module.dtd">
 <java classpathref="msv.classpath" fork="true" dir="."
   <arg value="src/test/resources/validation/module.dtd" />
   <arg value="src/test/resources/validation/module.xml" />
  1. If you declared "id" to be CDATA field, i.e. 
<!ATTLIST question

 then change it to ID field: 

<!ATTLIST question

 Notice that there is now a validation error, since two "id" attribute values are equal. 

Izveidojis Kalvis Apsītis 2007-10-16 09:59
This wiki is licensed under a Creative Commons 2.0 license
XWiki Enterprise 6.4 - Documentation