Can XMLCatalog be used to import a schema?

Can I use XMLCatalog to resolve xsds in schema import operations? If so, what is your preferred / best practice? I want to pack xsds in a bank, so use a relative scheme. Location does not work.

So far I am trying to do something like:

SchemaFactory factory = SchemaFactory .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); XMLCatalogResolver catalogResolver = new XMLCatalogResolver( new String[]{"/path/to/catalog.xml"}); factory.setResourceResolver(catalogResolver); Schema schema = factory.newSchema(new StreamSource(ClassLoader .getSystemResourceAsStream("config.xsd"))); 

Without much luck.

+3
java xml schema xmlcatalog
source share
1 answer

With a quick scan, I see two problems:

 XMLCatalogResolver catalogResolver = new XMLCatalogResolver( new String[]{"catalog.xml"}); 

If you look at the Javadoc for this method , you can read

directories - list of ordered arrays absolute URIs

which is not what you are using.

Second problem here

 Schema schema = factory.newSchema(new StreamSource(ClassLoader .getSystemResourceAsStream("config.xsd"))); 

You do not set the system identifier for the scheme, so if you have a relative location to import, this will be resolved relative to the current directory of your application instead of the directory in which you have the scheme file. You need to either call setSystemId in the source, or pass in the system identifier when creating it:

 new StreamSource(ClassLoader.getSystemResource("config.xsd").toString()) 
+5
source share

All Articles