Skip Headers

# Element and Attribute Form Default Qualified

Previous

Next

Example: XML Schema with Element and Attribute Form Default Qualified shows an XML schema in which a target namespace is set. It is coded with `elementFormDefault` and `attributeFormDefault` set to qualified. This means that all nodes must be namespace qualified.

XML Schema with Element and Attribute Form Default Qualified

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        elementFormDefault="qualified"
        attributeFormDefault="qualified"
        xmlns="urn:namespace-example"
        targetNamespace="urn:namespace-example">
        <xsd:element name="customer" type="customer-type"/>
        <xsd:complexType name="customer-type">
            <xsd:sequence>
                <xsd:element name="name" type="xsd:string"/>
                <xsd:element ref="date-of-birth"/>
            </xsd:sequence>
            <xsd:attribute name="id" type="xsd:integer"/>
        </xsd:complexType>
        <xsd:element name="date-of-birth" type="xsd:date"/>
    </xsd:schema>

Example: XML Document shows an XML document that conforms to this XML schema.

XML Document

    <?xml version="1.0" encoding="UTF-8"?>
    <ns:customer xmlns:ns="urn:namespace-example" ns:id="1">
        <ns:name>Jane Doe</ns:name>
        <ns:date-of-birth>1975-02-21</ns:date-of-birth>
    </ns:customer>

Example: XML Descriptors and Mappings shows the Java code for a `Customer` class `XMLDescriptor` and XML mappings for its attributes to illustrate how this schema configuration affects the XPaths you specify for default root element and mappings (for more information, see "Configuring an XML Descriptor" and Configuring an XML Mapping).

XML Descriptors and Mappings

    NamespaceResolver namespaceResolver = new NamespaceResolver();
    namespaceResolver.put("ns", "urn:namespace-example");

    XMLDescriptor customerDescriptor = new XMLDescriptor();
    customerDescriptor.setJavaClass(Customer.class);
    customerDescriptor.setDefaultRootElement("ns:customer");
    customerDescriptor.setNamespaceResolver(namespaceResolver);
                            XMLDirectMapping idMapping = new XMLDirectMapping();
    idMapping.setAttributeName("id");
    idMapping.setXPath("@ns:id");
    customerDescriptor.addMapping(idMapping);

    XMLDirectMapping nameMapping = new XMLDirectMapping();
    nameMapping.setAttributeName("name");
    nameMapping.setXPath("ns:name/text()");
    customerDescriptor.addMapping(nameMapping);

    XMLDirectMapping birthDateMapping = new XMLDirectMapping();
    birthDateMapping.setAttributeName("birthDate");
    birthDateMapping.setXPath("ns:date-of-birth/text()");
    customerDescriptor.addMapping(birthDateMapping);