Writing your own Collection Finder
Since Saxon 9.7, the CollectionFinder
interface replaces the
CollectionURIResolver interface in previous
releases. It has much more flexibility, in particular the ability to deliver non-XML
resources. The old
CollectionURIResolver interface has been dropped in Saxon 10.
Details of the interface can be found in the Javadoc. The basic steps are:
Write a class that implements
CollectionFinder. It takes a single method, which accepts an absolute collection URI, and returns an object that implements
ResourceCollection. Register an instance of your
CollectionFinderwith the Saxon
For example, aconfig.setCollectionFinder((context, uri) -> uri.startsWith('sql:') ? sqlCollection(uri) : config.getStandardCollectionFinder().findCollection(context, uri) )
CollectionFinderwritten to handle collection URIs using the scheme name "sql" might be supplied as:
sqlCollection(uri)returns some user-defined implementation of
ResourceCollection, perhaps one that retrieves XML documents from a relational database.
You can either reuse the existing implementations of ResourceCollection, namely
JarCollection, or you can write your own. You can also of course subclass the existing collection classes. The
ResourceCollectionobject provides two key methods that you need to implement:
getResources(), which returns a sequence of
getResourceURIs(), which returns a sequence of URIs. These are invoked by the fn:collection() and fn:uri-collection() functions respectively.
Again, you can either reuse existing implementations of Resource (such as
MetadataResource), or you can create your own, perhaps by subclassing. The key method that the
Resourceobject must provide is
getItem()which returns the resource in the form of an XDM item. It is good practice to delay any extensive work such as parsing until the
getItem()method is called: this reduces the memory footprint, and enables parallel evaluation of multiple threads (Saxon-EE only).