Writing your own collection finder
The CollectionFinder
interface enables you to define a mapping from a collection URI to
a set of resources (which can be arbitrary XDM values: they are not restricted
to be XML documents).
The details differ between Java and .NET:
On Java, the CollectionFinder is a functional interface, so you can either write a class that implements the interface, or you can supply an implementation directly as a lambda expression. It accepts a URI as input (supplied as a string), and returns an instance of
ResourceCollection
.You can either reuse the existing implementations of ResourceCollection (such as
CatalogCollection
,DirectoryCollection
,JarCollection
, andExplicitCollection
), or you can write your own. You can also of course subclass the existing collection classes. TheResourceCollection
object provides two key methods that you need to implement:getResources()
, which returns a sequence ofResource
objects, andgetResourceURIs()
, 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
XmlResource
,JSONResource
,UnparsedTextResource
,BinaryResource
, andMetadataResource
), or you can create your own, perhaps by subclassing. The key method that theResource
object must provide isgetItem()
which returns the resource in the form of an XDM item. It is good practice to delay any extensive work such as parsing until thegetItem()
method is called: this reduces the memory footprint, and enables parallel evaluation of multiple threads (Saxon-EE only).For example, a
config.setCollectionFinder((context, uri) -> uri.startsWith('sql:') ? sqlCollection(uri) : new StandardCollectionFinder().findCollection(context, uri) )CollectionFinder
written to handle collection URIs using the scheme name "sql" might be supplied as:where
sqlCollection(uri)
returns some user-defined implementation ofResourceCollection
, perhaps one that retrieves XML documents from a relational database.-
On .NET, the CollectionFinder is a delegate so you will typically implement it directly as a lambda expression. It accepts a URI as input (supplied as a string), and returns an instance of
IEnumerable<IResource>
.You can either reuse the existing implementations of IResource, or you can write your own. The key method that the
IResource
object must provide isGetItem()
which returns the resource in the form of an XDM item. It is good practice to delay any extensive work such as parsing until theGetItem()
method is called: this reduces the memory footprint, and enables parallel evaluation of multiple threads.For example, a
processor.CollectionFinder = uri => uri.startsWith('sql:') ? sqlCollection(uri) : new StandardCollectionFinder().findCollection(context, uri) )CollectionFinder
written to handle collection URIs using the scheme name "sql" might be supplied as:where
sqlCollection(uri)
returns some user-defined implementation ofIEnumerable<IResource>
, perhaps one that retrieves XML documents from a relational database.