WAS v8.5 > Develop applications > XML applications > Use the XML API to perform operations > Use resolvers

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.

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.

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. We 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;
    }}


+

Search Tips   |   Advanced Search