Powered by SmartDoc

The java.jaxp.dtd option

The java.jaxp.dtd option specifies a DTD for validation by an XML parser. The parser is made aware of the DTD via org.xml.sax.EntityResolver.

Parameters

The java.jaxp.dtd option takes one of the following values as a parameter:

true
Uses a DTD in a resource for validation.
false
Does not uses a DTD in a resource for validation.

The default configuration is false. No parameter implies that the parameter is true.

Artifacts

A package name specified by the package option or by the java.package option provides the resouce name for the DTD.

If no package name is specified, the directory name for the resouce is slash (/). Otherwise, a directory name is derived from the package name.

Example

List 7.5.3.1[javaJaxpDtd.rng] is a sample schema for the java.jaxp.dtd option.

javaJaxpDtd.rng
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
         datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
  <start>
    <ref name="account"/>
  </start>
  <define name="account">
    <element name="account">
      <attribute name="accountNo">
        <data type="token"/>
      </attribute>
      <element name="balance">
        <data type="int"/>
      </element>
      <element name="owner">
        <data type="token"/>
      </element>
      <optional>
        <ref name="address"/>
      </optional>
      <zeroOrMore>
        <ref name="phone"/>
      </zeroOrMore>
    </element>
  </define>
  <define name="address">
    <element name="address">
      <attribute name="zip">
        <data type="token"/>
      </attribute>
      <text/>
    </element>
  </define>
  <define name="phone">
    <element name="phone">
      <attribute name="area">
        <data type="token"/>
      </attribute>
      <data type="token"/>
    </element>
  </define>
</grammar>

Build

Execution of Relaxer with the java.jaxp.dtd option is as follows:

$ relaxer -java -java.jaxp -java.jaxp.dtd javaJaxpDtd.rng

Because the java generator is a default generator, and the default value for the java.jaxp option is true, execution of Relaxer shown here has the same effect:

$ relaxer -java.jaxp.dtd javaJaxpDtd.rng

As a result, Relaxer generates six files:

List 7.5.3.1.1[JavaJaxpDtd.java] is a sample program for the java.jaxp.dtd option.

JavaJaxpDtd.java
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

public class JavaJaxpDtd {
    public static void main(String[] args)
	throws IOException, SAXException, ParserConfigurationException {

	String uri = args[0];
	Account account = makeAccount(uri);
	printAccount(account);
	showAccount(account);
    }

    private static Account makeAccount(String uri)
	throws IOException, SAXException, ParserConfigurationException {

	System.out.println("*** makeAccount ***");
	Account account = new Account(uri);
	return (account);
    }

    private static void printAccount(Account account) {
	System.out.println("*** printAccount ***");
	String accountNo = account.getAccountNo();
	long balance = account.getBalance();
	String owner = account.getOwner();
	System.out.println("AccountNo:" + accountNo);
	System.out.println("Balance:" + balance);
	System.out.println("Owner:" + owner);
	Address address = account.getAddress();
	if (address != null) {
	    String zip = address.getZip();
	    String place = address.getContent();
	    System.out.println("Address: [" + zip + "] " + place);
	}
	Phone[] phones = account.getPhone();
	for (int i = 0;i < phones.length;i++) {
	    Phone phone = phones[i];
	    String area = phone.getArea();
	    String number = phone.getContent();
	    System.out.println("Phone: [" + area + "] " + number);
	}
    }

    private static void showAccount(Account account) {
	System.out.println("*** showAccount ***");
	System.out.println("XML:" + account);
    }
}

Compilation of program JavaJaxpDtd.java is shown here:

$ javac JavaJaxpDtd.java

List 7.5.3.1.2[javaJaxpDtd.dtd] is a DTD for javaJaxpDtd.rng.

javaJaxpDtd.dtd
<!ELEMENT account (balance, owner, address?, phone*)>
<!ATTLIST account accountNo CDATA #REQUIRED>

<!ELEMENT balance (#PCDATA)>

<!ELEMENT owner (#PCDATA)>

<!ELEMENT address (#PCDATA)>
<!ATTLIST address zip CDATA #REQUIRED>

<!ELEMENT phone (#PCDATA)>
<!ATTLIST phone area CDATA #REQUIRED>

Execution

List 7.5.3.2.1[javaJaxpDtd.xml] is a valid XML document with regard to the DTD javaJaxpDtd.dtd.

javaJaxpDtd.xml
<!DOCTYPE account SYSTEM "javaJaxpDtd.dtd">

<account accountNo="12345">
  <balance>102030</balance>
  <owner>XML Taro</owner>
  <address zip="213">Yokohama</address>
  <phone area="123">456-7890</phone>
  <phone area="090">123-4567</phone>
</account>

Execution of the JavaJaxpDtd class for the valid document is shown here:

$ java JavaJaxpDtd javaJaxpDtd.xml
*** makeAccount ***
*** printAccount ***
AccountNo:12345
Balance:102030
Owner:XML Taro
Address: [213] Yokohama
Phone: [123] 456-7890
Phone: [090] 123-4567
*** showAccount ***
XML:<account \
    accountNo="12345"><balance>102030</balance><owner>XML \
    Taro</owner><address zip="213">Yokohama</address><phone \
    area="123">456-7890</phone><phone \
    area="090">123-4567</phone></account>

Invalid document

List 7.5.3.3.1[javaJaxpDtdBadStructure.xml] is a invalid XML document against the DTD javaJaxpDtd.dtd.

javaJaxpDtdBadStructure.xml
<!DOCTYPE account SYSTEM "javaJaxpDtd.dtd">

<account accountNo="12345">
  <balance>102030</balance>
  <owner>XML Taro</owner>
  <address zip="213">Yokohama</address>
  <phone area="123">456-7890</phone>
  <phone area="090">123-4567</phone>
  <badElement/>
</account>

Execution of the JavaJaxpDtd for the invalid document is here:

$ java JavaJaxpDtd javaJaxpDtdBadStructure.xml
*** makeAccount ***
error : Element "account" does not allow "badElement" here.
error : Element type "badElement" is not declared.
*** printAccount ***
AccountNo:12345
Balance:102030
Owner:XML Taro
Address: [213] Yokohama
Phone: [123] 456-7890
Phone: [090] 123-4567
*** showAccount ***
XML:<account \
    accountNo="12345"><balance>102030</balance><owner>XML \
    Taro</owner><address zip="213">Yokohama</address><phone \
    area="123">456-7890</phone><phone \
    area="090">123-4567</phone></account>

DOCTYPE

List 7.5.3.4.1[javaJaxpDtdNoDoctype.xml] is an XML document which has correct form against the DTD javaJaxpDtd.dtd. However it does not have a DOCTYPE delaration.

javaJaxpDtdNoDoctype.xml
<account accountNo="12345">
  <balance>102030</balance>
  <owner>XML Taro</owner>
  <address zip="213">Yokohama</address>
  <phone area="123">456-7890</phone>
  <phone area="090">123-4567</phone>
</account>

Execution of the JavaJaxpDtd for the invalid document is here:

$ java JavaJaxpDtd javaJaxpDtdNoDoctype.xml
*** makeAccount ***
warning : Valid documents must have a <!DOCTYPE declaration.
error : Element type "account" is not declared.
error : Attribute "accountNo" is not declared for element \
    "account".
error : Element type "balance" is not declared.
error : Element type "owner" is not declared.
error : Element type "address" is not declared.
error : Attribute "zip" is not declared for element "address".
error : Element type "phone" is not declared.
error : Attribute "area" is not declared for element "phone".
error : Attribute "area" is not declared for element "phone".
*** printAccount ***
AccountNo:12345
Balance:102030
Owner:XML Taro
Address: [213] Yokohama
Phone: [123] 456-7890
Phone: [090] 123-4567
*** showAccount ***
XML:<account \
    accountNo="12345"><balance>102030</balance><owner>XML \
    Taro</owner><address zip="213">Yokohama</address><phone \
    area="123">456-7890</phone><phone \
    area="090">123-4567</phone></account>

An XML document which does not have a DOCTYPE declaration is always invalid. Result of the execution of the JavaJaxpDtd shows this error.

Note that an XML document which does not have a DOCTYPE declaration can be validated using the java.jarv.verify option with JARV.

Package

The java.package option affects the outcome of use of the java.jaxp.dtd option.

Here is an execution of Relaxer without the java.package option:

$ relaxer -java -java.jaxp -java.jaxp.dtd javaJaxpDtd.rng

As a result, Relaxer generates six files:

In this case, the resource name is /javaJaxpDtd.dtd. The file javaJaxpDtd must be available in the root directory of the resouce.

Here is an execution of Relaxer with the java.package option. The package name is com.example.javaJaxpDtd.

$ relaxer -java -java.jaxp -java.jaxp.dtd \
    -java.package:com.example.javaJaxpDtd javaJaxpDtd.rng

In this case, Resource name is /com/example/javaJaxpDtd/javaJaxpDtd.dtd. The file javaJaxpDtd must be available in the /com/example/javaJaxpDtd directory of the resouce.