Home : Tutorials : XML Tutorial


Declaring Attributes in DTD

For a Validating Parser to Properly Validate the XML document, it make it necessry to have all declarations for both Element  and Attributes in the DTD file. 

Attributes for the Address Element can be declared as follows

<!ELEMENT Main_Author (Address *)  >
<!ELEMENT Address (#PCDATA)> 

<!ATTLIST Address 
            Street1   CDATA     #REQUIRED
            Street2   CDATA     #IMPLIED
            City        CDATA     #IMPLIED
            Zip         CDATA     "unknown"
            Country  CDATA     #REQUIRED
>

The DTD tag ATTLIST begins the series of attribute definitions. The name that follows ATTLIST specifies the element for which the attributes are being defined. In this case, the element is the Address element.

Each attribute is defined by a series of three space-separated values. Commas and other separators are not allowed, so formatting the definitions as shown above is helpful for readability. The first element in each line is the name of the attribute: Street1, Street2, City, Zip or Country. The second element indicates the type of the data: CDATA is character data -- unparsed data, once again, in which a left-angle bracket (<) will never be construed as part of an XML tag. 

The last entry in the attribute specification determines the attributes default value, if any, and tells whether or not the attribute is required. The table below shows the possible choices.

Specification
Specifies...
#REQUIRED
The attribute value must be specified in the document.
#IMPLIED
The value need not be specified in the document. If it isn't, the application will have a default value it uses.
"defaultValue"
The default value to use, if a value is not specified in the document.
#FIXED "fixedValue"
The value to use. If the document specifies any value at all, it must be the same.

Using the Validating Parser

Validating Parser would make sure that the the XML document is Valid which means all the DTD Tags are satisfied along with Well Formedness. 

Two main Requirements for using  validating parser are: 

a.The DTD is required. 
b.Since the DTD is present, the ignorableWhitespace method is invoked whenever the DTD makes that possible. 

To use the validating parser, make the changes highlighted below:

public static void main (String argv [])
{
    if (argv.length != 1) {
    ...
    }
    // Use the validating parser
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);
    try {
...

You an experiment with Error that are generated by not declaring the DTD  declaration in the XML file or not matching other requirements for the XML file to be called Valid.

Home  Previous  Next


Home : Tutorials : XML Tutorial

 

Copyright© 1998-2004 All Rights Reserved. No portion of this site may be reproduced or redistributed without prior written permission from VistaEdge Technologies

All registered trademarks appearing on this site are the property of their respective owners. Java is a trademark or registered trademark of Sun Microsystems, Inc. in the United States and other countries. This site is not connected to Sun Microsystems, Inc. and is not sponsored by Sun Microsystems, Inc.