XML Catalog
Encyclopedia
XML
XML
Extensible Markup Language is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards....

 documents typically refer to external entities, for example the public and/or system ID for the Document Type Definition
Document Type Definition
Document Type Definition is a set of markup declarations that define a document type for SGML-family markup languages...

. These external relationships are expressed using URIs, typically as URLs.

However, if they are absolute URLs, they only work when your network can reach them. Relying on remote resources makes XML processing susceptible to both planned and unplanned network downtime.

Conversely, if they are relative URLs, they're only useful in the context where they were initially created. For example, the URL "../../xml/dtd/docbookx.xml" will usually only be useful in very limited circumstances.

One way to avoid these problems is to use an entity resolver (a standard part of SAX
Simple API for XML
SAX is an event-based sequential access parser API developed by the XML-DEV mailing list for XML documents. SAX provides a mechanism for reading data from an XML document that is an alternative to that provided by the Document Object Model...

) or a URI Resolver (a standard part of JAXP). A resolver can examine the URIs of the resources being requested and determine how best to satisfy those requests. The XML catalog is a document describing a mapping between external entity references and locally-cached equivalents.

Example Catalog.xml

The following simple catalog shows how one might provide locally-cached DTDs for an XHTML page validation tool, for example.


PUBLIC "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN"
"http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd">

prefer="public">

uri="dtd/xhtml1/xhtml1-strict.dtd"/>

uri="dtd/xhtml1/xhtml1-transitional.dtd"/>

uri="dtd/xhtml11/xhtml11-flat.dtd"/>




This catalog makes it possible to resolve -//W3C//DTD XHTML 1.0 Strict//EN to the local URI dtd/xhtml1/xhtml1-strict.dtd. Similarly, it provides local URIs for two other public IDs.

Note that the document above includes a DOCTYPE - this may cause the parser to attempt to access the system ID URL for the DOCTYPE (i.e. http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd) before the catalog resolver is fully functioning, which is probably undesirable. To prevent this, simply remove the DOCTYPE declaration.

The following example shows this, and also shows the equivalent <system/> declarations as an alternative to <public/> declarations.





uri="dtd/xhtml1/xhtml1-strict.dtd"/>

uri="dtd/xhtml1/xhtml1-transitional.dtd"/>

uri="dtd/xhtml11/xhtml11-flat.dtd"/>



Using a Catalog - Java SAX Example

Catalog resolvers are available for various programming languages. The following example shows how, in Java, a SAX
Simple API for XML
SAX is an event-based sequential access parser API developed by the XML-DEV mailing list for XML documents. SAX provides a mechanism for reading data from an XML document that is an alternative to that provided by the Document Object Model...

 parser may be created to parse some input source in which the org.apache.xml.resolver.tools.CatalogResolver is used to resolve external entities to locally-cached instances. This resolver originates from Apache
Apache Software Foundation
The Apache Software Foundation is a non-profit corporation to support Apache software projects, including the Apache HTTP Server. The ASF was formed from the Apache Group and incorporated in Delaware, U.S., in June 1999.The Apache Software Foundation is a decentralized community of developers...

 Xerces
Xerces
Xerces is a collection of software libraries for parsing, validating, serializing and manipulating XML. The library implements a number of standard APIs for XML parsing, including DOM, SAX and SAX2. The implementation is available in Java, C++ and Perl programming languages.-External...

but is now included with the Sun Java runtime.

Simply create a SAXParser in the normal way, using factories. Obtain the XML reader and set the entity resolver to the standard one (CatalogResolver) or another of your own.


final SAXParser saxParser = SAXParserFactory.newInstance.newSAXParser;
final XMLReader reader = saxParser.getXMLReader;

final ContentHandler handler = ...;
final InputSource input = ...;

reader.setEntityResolver( new CatalogResolver );
reader.setContentHandler( handler );
reader.parse( input );


It is important to call the parse method on the reader, not on the SAX parser.

See also

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK