To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98
CHAPTER 16 XML DTD and Document Validation
- In this chapter
- Checking Your DTD Using DXP 400
- Checking Your DTD Using XML for Java 408
- Checking the Validity of XML Files with DXP 409
- Checking the Validity of XML Files with XML for Java 412
- Other XML Parsers 413
By now, youre aware that an XML document must adhere to the very specific syntax put forward in its Document Type Definition (DTD). As you design an XML document, you are essentially designing its DTD, which resides in a separate file that you must also write. Given an XML document and its DTD, you will find yourself asking two important questions:
- How can I check that my DTD is correct?
- How can I check that my documents comply with the rules defined in the DTD?
The answer to both questions is to use a program called a validating parser. In this chapter you will learn how to check your DTD using programs called DXF and XML for Java. Additionally, youll also see how to verify that your XML files comply with the rules of your DTD using these programs.
Checking Your DTD Using DXP
DXP stands for Datachannel XML Parser and is based on NXP (Norbert Mikulas XML Parser), one of the first XML parsers. DXP is written in Java, which is the most popular language for the development of XML parsers. You can use this parser to make sure your XML documents are both well formed and valid.
How to Install DXP
You can download DXP from http://www.datachannel.com/. You need to click through several pages to get to the actual download page. Once there, you should look for the file DXP.zip (about 535KB in size). In addition to the necessary Java class files, youll find a fair amount of documentation and examples.
To install DXP, follow these steps:
- 1. Unzip the DXP.zip file.
- 2. Make sure you have a Java Virtual Machine version 1.x running. You can use Suns JDK (Java Development Kit) (http://www.javasoft.com/products/jdk/), Suns JRE (Java Runtime Environment) (http://www.javasoft.com/products/), or Microsofts SDK for Java (http://www.microsoft.com/java/).
- 3. Make sure that your Java Virtual Machine can find the DXP classes by adding the packages directory of the DXP parser to your classpath environment variable, such as c:\datachannel\dxp\classes or, in the case of JRE, setting the -cp parameter to the path on the command line, such as jre -cp c:\datachannel\dxp\classes.
Running DXP
Once youve completed the steps above, youre ready to run DXP. To do this, type the following from a DOS command prompt:
jre cp .;c:\datachannel\dxp\classes dxpcl s v c:\xmlex\file.xml
Heres a breakdown of what you just typed:
- jre invokes the Java Runtime Engine.
- -cp sets the classpath (where to find the classes used). In this case we specified two paths, separated by ;the first (.) refers to the current working directory, and the second is c:\datachannel\dxp\classes.
- dxpcl is the name of the Java program (class).
- -s stands for silent mode.
- -v stands for validation on.
- c:\xmlex\file.xml is the file to be checked.
NOTE: Because youve asked for silent mode, the program will only output the error messages.
Here the markup declarations exist in an external file (a special kind of external entity). The document type declaration in the XML file needs to point to this external file. For example:
<!DOCTYPE helptopic SYSTEM <http://www.server.com/help.dtd> []>
Use the DTD (external subset) shown in Listing 16.1. When invoking the parser, assume that this DTD is in the file dtdv.dtd.
Listing 16.1 DTD for Sample Document to Parse with DXP
<!ENTITY % admonitions (tip | warning | note) >
<!ENTITY % paracontent (#PCDATA | icon | menu | xref | iconbmp)* >
<!ELEMENT helptopic (title, rule, procedure, rule?, %admonitions;) >
<!ATTLIST helptopic id ID #IMPLIED>
<!ELEMENT title (#PCDATA) >
<!ATTLIST title keyword CDATA>
<!ELEMENT procedure (step+)>
<!ELEMENT step (action, (%admonitions;)*) >
<!ELEMENT action %paracontent; >
<!ELEMENT tip %paracontent; >
<!ATTLIST tip targetgroup (beginners | specialists) beginners >
<!ELEMENT warning %paracontent; >
<!ELEMENT note %paracontent; >
<!ELEMENT icon (#PCDATA) >
<!ELEMENT menu (#PCDATA|shortcut)+>
<!ELEMENT xref (#PCDATA) >
<!ATTLIST xref linkend idref #REQUIRED>
<!ELEMENT shortcut (#PCDATA)>
<!ELEMENT tip (#PCDATA) >
<!ELEMENT iconbmp EMPTY>
<!ATTLIST iconbmp src ENTITY #REQUIRED
type NOTATION (bmp | gif) gif>
To check the preceding declarations, you need an XML file referring to this file as its DTD:
<?xml version=1.0 ?>
<!DOCTYPE helptopic SYSTEM dtdv.dtd [
]>
The preceding code starts the XML file with a document type declaration that refers to the external subset in the file dtdv.dtd.
NOTE: For the purposes of this example, assume that the XML document and the DTD file are in the same directory. If they are not, you need to account for directory paths in your declaration.
When you run DXP with these files, youll see several errors. The first is as follows:
FATAL ERROR: encountered >. Was expecting one of: <EOF> , <S>
Location: file:///c:/xmlex/dtdv.dtd:8:30
Found errors/warnings: 1 fatal error(s), 0 error(s) and 0 warning(s)
|