Powered by SmartDoc

The java.xml.namespace option

The java.xml.namespace option enables Relaxer objects to handle namespace-aware XML documents.

Parameters

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

true
Enables a namespace support.
false
Disables a namespace support.

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

Artifacts

The Relaxer object generated with the java.xml.namespace option has a structure to handle namespace:

The Relaxer object generated with the java.xml.namespace option has a structure which is generated by the java.pattern.composite option. In other words, using the java.xml.namespace implies that you are using the java.pattern.composite option as well.

The Relaxer object uses RNSContext to handle namespace. The RNSContext object manages namespace context for the Relaxer object.

The java.xml.namespace adds following methods to Relaxer objects:

The rGetRNSContext and the rSetRNSContext methods are a setter and a getter of the RNSContext object. Generally, a client need not use these methods immediately.

The rGetParentRNode, the rSetParentRNode, and the rGetRNodes methods are methods for manipulation compsite structures, and are specified by the java.pattern.composite option. The Relaxer object uses these methods internally to handle namespace. Namely, a client which is intented to operate on namespace need not use these methods directly.

Example

List 7.7.3.1[javaXmlNamespace.rng] is a sample schema for the java.xml.namespace option.

javaXmlNamespace.rng
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
         datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"
         ns="http://example.com/account">
  <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 command with the java.xml.namespace option is as follows:

$ relaxer -java -java.xml.namespace javaXmlNamespace.rng

Because the Java generator is a default generator, execution of Relaxer as shown below has the same effect:

$ relaxer -java.xml.namespace javaXmlNamespace.rng

As a result, Relaxer generates 10 files:

Account is a Relaxer object. URelaxer and the RStack is fundamental utility classes. URelaxer2 is a utility class for namespace operation. IRNSContainer and RNSContext are helper interface and object for namespace operation. IRNode is a helper interface for composite operation which relates to the java.pattern.composite option.

List 7.7.3.1.1[JavaXmlNamespace.java] is a sample program for the java.xml.namespace option.

JavaXmlNamespace.java
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import java.util.Map;
import java.util.Set;
import java.util.Iterator;

public class JavaXmlNamespace {
    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 JavaXmlNamespace is shown here:

$ javac JavaXmlNamespace.java

Execution

List 7.7.3.2.1[JavaXmlNamespace.xml] is an XML document for testing. A default namespace is defined as 'http://example.com/account'.

JavaXmlNamespace.xml
<account xmlns="http://example.com/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 JavaXmlNamespace class is shown here:

$ java JavaXmlNamespace javaXmlNamespace.xml
*** makeAccount ***
*** printAccount ***
AccountNo:12345
Balance:102030
Owner:XML Taro
Address: [213] Yokohama
Phone: [123] 456-7890
Phone: [090] 123-4567
*** showAccount ***
XML:<account xmlns="http://example.com/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/prefix

List 7.7.3.3.1[JavaXmlNamespacePrefix.xml] is am XML document using a prefix of a namespace. In the document, a prefix 'z' is allocated for a namespace 'http://example.com/account'.

JavaXmlNamespacePrefix.xml
<z:account xmlns:z="http://example.com/account" accountNo="12345">
  <z:balance>102030</z:balance>
  <z:owner>XML Taro</z:owner>
  <z:address zip="213">Yokohama</z:address>
  <z:phone area="123">456-7890</z:phone>
  <z:phone area="090">123-4567</z:phone>
</z:account>

Execution of the JavaXmlNamespace class is shown here:

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

In the output XML, the namespace declaration and the prefix are displayed as an original document do.