2

I am attempting to update some xml parsers, and have hit a small snag. We have an xsd that we need to keep compatible with older versions of the xml, and we had to make some changes to it. We made the changes in a new version of the xsd, and we would like to use the same parser (as the changes are pretty small in general, and the parser can easily handle both). We are using the XMLReader property "http://java.sun.com/xml/jaxp/properties/schemaSource" to set the schema to the previous edition, using something like the following:

xmlReader.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", 
    new InputSource(getClass().getResourceAsStream("/schema/my-xsd-1.0.xsd")));

This worked fine when we only had one version of the schema. Now we have a new version, and we want the system to use whichever version of the schema is defined in the incoming xml. Both schemas define a namespace, something like the following:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.mycompany.com/my-xsd-1.0"
    xmlns="http://www.mycompany.com/my-xsd-1.0"
    elementFormDefault="unqualified" attributeFormDefault="unqualified">

and, for the new one:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.mycompany.com/my-xsd-1.1"
    xmlns="http://www.mycompany.com/my-xsd-1.1"
    elementFormDefault="unqualified" attributeFormDefault="unqualified">

So, they have different namespaces and different schema "locations" defined. We don't want the schema to live on the 'net - we want it to be bundled with our system. Is there a way to use the setProperty mechanism to do this behavior, or is there a different way to handle this?

I tried putting both resources in an input stream in an array as the parameter, but that didn't work (I remember reading somewhere that this was a possible solution - although now I can't find the source, so it might have been wishful thinking).

2 Answers 2

2

So, it turns out what I had tried actually worked - we were accidentally using invalid xml! What works (for anyone else who is interested) is the following:

List<InputSource> inputs = new ArrayList<InputSource>();
inputs.add(new InputSource(getClass().getResourceAsStream("/schema/my-xsd-1.0.xsd")));
inputs.add(new InputSource(getClass().getResourceAsStream("/schema/my-xsd-1.1.xsd")));

xmlReader.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", 
    inputs.toArray(new InputSource[inputs.size()]));
1

Personally I think it's generally a bad idea to change the namespace when you version a schema, unless the changes are radical - but views differ on that, and you seem to have made your decision, and you may as well reap the benefits.

Since you're using two different namespaces, the schemas are presumably disjoint, so you should be able to give the processor a schema that's the union of the two - I don't know if there's a better way, but one way of achieving this is to write a little stub schema that imports both, and supply this stub as your schemaSource property. The processor will use whichever schema declarations match the namespace of the elements in the source document.

(Using version-specific namespaces makes this task - validation - easier. But it makes subsequent processing of the XML, e.g. using XPath, harder, because it's hard to write code that works with both namespaces.)

2
  • So instead of changing the namespace, you would recommend keeping the same namespace and just changing the version number? How would you pass the correct version into the validator then?
    – aperkins
    Jun 8, 2011 at 17:14
  • I should clarify too - I don't disagree with you on the namespace changing - I inherited this. I can change it so that the namespace doesn't change, I would just like to figure out how to handle the lookup of the different xsds as appropriate.
    – aperkins
    Jun 8, 2011 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.