Registering a schema resolver

The XSchemaResolver interface can be implemented and the implementation registered with the XFactory to override the default schema resolution behavior. This includes resolution of imports for schemas registered with XFactory using the registerSchema method and resolving schemas imported in XSLT stylesheets using the xsl:import-schema declaration.

About this task

The default behavior for resolving imports within a schema is to use the base URI of the schema to resolve the imported schema's location. The default behavior for XSLT schema imports is to use the base URI of the xsl:import-schema declaration to resolve the location specified in the declaration.

Procedure

Use the setSchemaResolver method on the XFactory class to register a schema resolver.

The getSchema method returns an instance of the java.util.List interface. This is because the definitions of the schema components for a particular namespace can be split across several distinct schema documents. You can use the getSchema method to return all the schema documents for the particular target namespace associated with all the specified location hints.

Example

The following is a basic example of using a schema resolver.
XFactory factory = XFactory.newInstance();

// Set validating to true.
factory.setValidating(true);

// Create the schema resolver and register it with the factory.
factory.setSchemaResolver(new ASchemaResolver(replacementBase));

// Prepare the stylesheet.
XSLTExecutable executable = factory.prepareXSLT(new StreamSource(stylesheetFile));

// Execute the transformation.
Source source = new StreamSource(inputFile);
Result result = new StreamResult(System.out);
executable.execute(source, result);
The following is a basic example of an XSchemaResolver implementation.
class ASchemaResolver implements XSchemaResolver
{
    String _replacementBase;
    
    public ASchemaResolver(String replacementBase)
    {
        _replacementBase=replacementBase;
    }
    
    // Resolve URI, returning the Source that URI represents.
    // Implements the "rebase:" pseudo-scheme.
    public List<? extends Source> getSchema(String namespace, List<String> locations, String baseURI) {
        String rebasePrefix="rebase:";

        List<StreamSource> list = new ArrayList<StreamSource>();
        for (int i = 0; i < locations.size(); i++) {
            String href = locations.get(i);
            String base = baseURI;
            if(href.startsWith(rebasePrefix)) {
                href=href.substring(rebasePrefix.length());
                base=_replacementBase;
            }
        
            java.net.URI uri;
            StreamSource source=null;
            try {
                // Get base URI object
                uri = new java.net.URI(base);
                // Resolved relative reference against base URI
                URI resolvedURI = uri.resolve(href);
                // Try to read...
                source = new StreamSource(resolvedURI.toString());
            } catch (java.net.URISyntaxException use) {
                throw new RuntimeException(use);
            }

            list.add(source);
        }
        return list;
    }
}
Task topic Task topic    

Terms and conditions for information centers | Feedback

Last updatedLast updated: Feb 6, 2014 8:11:25 PM CST
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=matt&product=was-nd-mp&topic=txml_resolvers_schema
File name: txml_resolvers_schema.html