Use elementFormDefault to Hide/Expose Namespaces
* 'elementFormDefault' is a binary switch attribute in the schema to control if the origin (i.e. namespace) of each element should be
- hidden (elementFormDefault="unqualified")
- or exposed ("elementFormDefault="qualified").
* elementFormDefault defaults to "unqualified"
* All schemas must have a consistent value for 'elementFormDefault' in order to hide/expose all elements.
Hide Namespaces
* Advantages:
- Simplicity
- Flexibility to change the schema without impact of instance documents.
* Howto:
- Set elementFormDefault="unqualified" in all schemas.
- The element must not be globally declared.
Best Practices
* Make two identical copies of a schema:
- One with elementFormDefault="qualified"
- One with elementFormDefault="unqualified"
* Minimize the use of global elements and attributes.
Notes
* Global elements and attribute must always be qualified.
* Use <include> to include schema with same namespace.
* Use <import> to include schema with different namespace.
References
* Hide (Localize) Namespaces Versus Expose Namespaces or PDF version
Filed under: xml | |Comments off
Implementation
* Define an abstract base type (PublicationType) to include common elements:
<xsd:complexType name="PublicationType" abstract="true">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"
minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="Date" type="xsd:gYear"/>
</xsd:sequence>
</xsd:complexType>
* Define concrete types(BookType, MagazineType) to extend abstract type
<xsd:complexType name="BookType">
<xsd:complexContent>
<xsd:extension base="PublicationType">
<xsd:sequence>
<xsd:element name="ISBN" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="MagazineType">
<xsd:complexContent>
<xsd:restriction base="PublicationType">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"
minOccurs="0" maxOccurs="0"/>
<xsd:element name="Date" type="xsd:gYear"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
* Define container element (Catalogue) to contain an element (Publication) of the abstract type (PublicationType):
<xsd:element name="Catalogue">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Publication"
type="PublicationType"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
* Example
<?xml version="1.0"?>
<Catalogue xmlns="http://www.catalogue.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.catalogue.org
Catalogue.xsd">
<Book>
<Title>Illusions The Adventures of a Reluctant Messiah</Title>
<Author>Richard Bach</Author>
<Date>1977</Date>
<ISBN>0-440-34319-4</ISBN>
<Publisher>Dell Publishing Co.</Publisher>
</Book>
<Magazine>
<Title>Natural Health</Title>
<Date>1999</Date>
</Magazine>
<Book>
<Title>The First and Last Freedom</Title>
<Author>J. Krishnamurti</Author>
<Date>1954</Date>
<ISBN>0-06-064831-7</ISBN>
<Publisher>Harper & Row</Publisher>
</Book>
</Catalogue>
Extend the schema
* Add a new concrete type (CDType)
<include schemaLocation="Catalogue.xsd"/>
<complexType name="CDType">
<complexContent>
<extension base="PublicationType" >
<sequence>
<element name="RecordingCompany" type="string"/>
</sequence>
</extension>
</complexContent>
</complexType>
* Example
<?xml version="1.0"?>
<Catalogue xmlns="http://www.catalogue.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.catalogue.org
Catalogue.xsd">
<Book>
<Title>Illusions The Adventures of a Reluctant Messiah</Title>
<Author>Richard Bach</Author>
<Date>1977</Date>
<ISBN>0-440-34319-4</ISBN>
<Publisher>Dell Publishing Co.</Publisher>
</Book>
<Magazine>
<Title>Natural Health</Title>
<Date>1999</Date>
</Magazine>
<Book>
<Title>The First and Last Freedom</Title>
<Author>J. Krishnamurti</Author>
<Date>1954</Date>
<ISBN>0-06-064831-7</ISBN>
<Publisher>Harper & Row</Publisher>
</Book>
<CD>
<Title>Timeless Serenity</Title>
<Author>Dyveke Spino</Author>
<Date>1984</Date>
<RecordingCompany>Dyveke Spino Productions</RecordingCompany>
</CD>
</Catalogue>
Reference
* http://www.xfront.com/VariableContentContainers.pdf
Filed under: xml | |Comments off
Convert XML document to String
protected String docToString(Document doc){
String xmlStr = null;
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StreamResult streamResult = new StreamResult(new StringWriter());
DOMSource domSrc = new DOMSource(doc);
transformer.transform(domSrc,streamResult);
xmlStr = streamResult.getWriter().toString();
} catch (TransformerConfigurationException e) {
log.error(e);
} catch (TransformerFactoryConfigurationError e) {
log.error(e);
} catch (TransformerException e) {
log.error(e);
}
return xmlStr;
}
Filed under: xml | |No Comments