You can register implementations of the XCollectionResolver interface with the XDynamicContext.
The XCollectionResolver implementation registered with the XDynamicContext is used at execution time to retrieve the collection of nodes associated with the URI provided in calls to the fn:collection method. If no collection resolver is registered with the XDynamicContext then calls to fn:collection will result in a recoverable error and the empty sequence is used for the collection.
Note that the collection resolver and the fn:collection function are not meant to resolve document URIs. The source resolver and fn:doc function should be used for this purpose.
XFactory factory = XFactory.newInstance(); // Prepare the XPath expression XPathExecutable executable = factory.prepareXPath("count(collection('typeA-typeB'))" // Register the collection resolver with the dynamic context XCollectionResolver collectionResolver = new ACollectionResolver(factory); XDynamicContext dynamicContext = factory.newDynamicContext(); dynamicContext.setCollectionResolver(collectionResolver); // Execute the XPath expression XSequenceCursor cursor = executable.execute(dynamicContext);
public class ACollectionResolver implements XCollectionResolver { private XFactory m_factory; public ACollectionResolver(XFactory factory) { m_factory = factory; } public XSequenceCursor getCollection(String uri, String base) { // Get the default collection if (uri.equals("")) { return getCollection("default", base); } // Get the requested collection ArrayList<XItemView> list = new ArrayList<XItemView>(); StringTokenizer tokenizer = new StringTokenizer(uri, "-"); XSequenceCursor cursor = null; while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); XSequenceCursor temp = getNodes(new StreamSource("collections.xml"), "/doc/" + token); if (cursor == null) { cursor = temp; } else { cursor = cursor.append(temp); } } return cursor; } private XSequenceCursor getNodes(Source source, String expression) { XPathExecutable executable = m_factory.prepareXPath(expression); XSequenceCursor cursor = executable.execute(source); return cursor; } }
In this information ...Related information
| IBM Redbooks, demos, education, and more(Index) |