Friday, February 22, 2008

xsi:noNamespaceSchemaLocation

Validating an XML file I was able to get the following error:
Expecting namespace 'null', but the target namespace of the schema document is 'null'.

'null' seems to match 'null', so what's the problem?

It turns out that the validator is Java-based and 'null' is not a literal namespace, but actually a Java null pointer. Meaning that both the XML document and the schema have no namespace.

But why should that be a problem?

It shouldn't. As the XML file was generated from XML Schema file (XSD), it contained attributes similar to the following:
xmlns:ns17='null' xsi:schemaLocation='null path.to.schema.file.xsd'
Additionally, root element (nor other elements) didn't have the ns17 prefix.

This is just plain wrong. Although it is not pretty to have schemas without namespace, it is also not pretty to generate such crappy XML from it.

A quick fix by hand would remove the namespace definition and replace schema location with:
xsi:noNamespaceSchemaLocation='path.to.schema.file.xsd'

VoilĂ !

Reference: http://www.w3.org/TR/xmlschema-1/#xsi_schemaLocation

No comments: