Powered By Blogger

Saturday, February 2, 2013

schemaLocation vs xsi:schemaLocation attributes usage

In this post let's talk about schemaLocation and xsi:schemaLocation attributes usage and their significance which are being used by XML schema.
Remember that both attributes are having same name but coming from different namespace so they are used for different purpose
There are three circumstances for using this attributes

Scenario1:(xsi:schemaLocation ) in an XML instance document

This attribute is hint to XML processor from the document author regarding the access location of schema documents.These schema documents are used to checking the validity of the document content, on a namespace by namespace basis. For example, we can indicate the location of the Order schema to a processor of the Order XML document 

In the below example processor [may] contact given location to download the schema and use it
xmlns=http://www.oracle.com/Order
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation="http://www.oracle.com/Order
http://www.oracle.com/Order.xsd">


schemaLocation attribute value contains of one or more pairs of URI references, separated by white space. The first part of each pair is a namespace name, and the second part of the pair is a an URI describing where to find an appropriate schema document for that namespace. The presence of these hints does not mean that the processor has to obtain or use the cited schema documents, and the processor is free to use other schemas obtained by any suitable means like loading from classpath jars, or to use no schema at all.

There is an interesting error I have seen when URI in this attribute is not reachable

Error

[name of xml file being processed] is invalid; nested exception is oracle.xml.parser.schema.XSDException: Network is unreachable
at

at oracle.xml.parser.v2.XMLError.flushErrorHandler(XMLError.java:425)at oracle.xml.parser.v2.XMLError.flushErrors1(XMLError.java:287)at oracle.xml.parser.v2.NonValidatingParser.parseDocument(NonValidatingParser.java:376)

at oracle.xml.parser.v2.XMLParser.parse(XMLParser.java:226)at oracle.xml.jaxp.JXDocumentBuilder.parse(JXDocumentBuilder.java:155)at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)

at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)

... 71 more

Caused by: oracle.xml.parser.schema.XSDException: Network is unreachable
at oracle.xml.parser.schema.XSDBuilder.build(XSDBuilder.java:652)
at oracle.xml.parser.schema.XSDValidator.processSchemaLocation(XSDValidator.java:1003)

at oracle.xml.parser.schema.XSDValidator.startElement(XSDValidator.java:604)
at oracle.xml.parser.v2.NonValidatingParser.parseElement(NonValidatingParser.java:1524)

at oracle.xml.parser.v2.NonValidatingParser.parseRootElement(NonValidatingParser.java:409)

at oracle.xml.parser.v2.NonValidatingParser.parseDocument(NonValidatingParser.java:355)

... 75 more

:oracle.xml.parser.schema.XSDException:Network is unreachable

at oracle.xml.parser.schema.XSDBuilder.build(XSDBuilder.java:652)

at oracle.xml.parser.schema.XSDValidator.processSchemaLocation(XSDValidator.java:1003)

at oracle.xml.parser.schema.XSDValidator.startElement(XSDValidator.java:604)

at oracle.xml.parser.v2.NonValidatingParser.parseElement(NonValidatingParser.java:1524)

at oracle.xml.parser.v2.NonValidatingParser.parseRootElement(NonValidatingParser.java:409)

at oracle.xml.parser.v2.NonValidatingParser.parseDocument(NonValidatingParser.java:355)

at oracle.xml.parser.v2.XMLParser.parse(XMLParser.java:226)

at oracle.xml.jaxp.JXDocumentBuilder.parse(JXDocumentBuilder.java:155)

at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)


Solution for this issue is to remove xsi:schemaLocation from xml instance before processor starts processing it.

Scenario2:(schemaLocation in include) in  xsd


In a schema, the include  element has a required schemaLocation attribute, and it contains a URI reference which must resolve to accessible schema document. The effect is to compose a final effective schema by merging the declarations and definitions of the including and the included schemas.
Unlike xsi:schemaLocation this is not a hint  but it is directive to processor and failure to connect to given URI causes error

Scenario3:(schemaLocation in import ) in  xsd

import element  in schema also uses an optional namespace and schemaLocation attributes.

The difference between include and import is, include is used to load the xml elements/type definitions from same target namespace as including document
import is used to load xml elements/types from different namespace than importing document


3 comments: