Hi,
I've created a few classes using and mainly inspired by Xalan's XPath implementation, adding a new functionality to the low-level XPath API, which I think might be of some interest for the community. Thus I would be happy to share the code if

- Given the description below, is this functionality eligible for a contribution?
- If yes, how can I submit the source code?

The functionality is similar to the org.apache.xpath.CachedXPathAPI class, providing equivalent cache capability, but allowing to dynamically extend the namespace context in order to perform XPath queries using EXSLT-implemented functions as well as custom functions out of an XSLT transformation, but still in a "context-aware" mode. By default, when using the Xalan low-level XPath API, custom functions having an org.apache.xalan.extensions.ExpressionContext as their first argument will always get a null reference of it when called. For example, EXSLT's dyn:evaluate will throw an exception if used in the low-level API. Of course this function has no reasonable utility in this situation, but I wanted to implement a "sort" function working in a similar way to the xsl:sort element, allowing to compare nodes in a node set according to an XPath sort expression:

public static NodeList sort(ExpressionContext context, NodeList nodelist, String sortExpression, String dataType, String order) throws SAXNotSupportedException;

The implementation requires to manipulate the given context in order to perform the given sort expression on each node of the given nodelist, and thus cannot be used with the default low-level XPath API. With this functionality, one could use a ContextualXPathAPI class, providing the same methods as CachedXPathAPI (as well as convenience methods for retrieving the 5 possible data types), but allowing to extend the namespace context in a static way:

public static void addNamespacePrefixMapping(String prefix, String namespaceURI);
   public static void removeNamespacePrefixMapping(String prefix);

Here's an usage example, assuming there's a mypackage.XPathFunctions class defining a sort method as stated above:

   ...
   Node contextNode = ... // obtain a node reference
// extensions defined in org.apache.xalan.extensions.ExtensionNamespaceContext are mapped by default ContextualXPathAPI.addNamespacePrefixMapping("xpf", "xalan://mypackage.XPathFunctions");
   ContextualXPathAPI xPathAPI = new ContextualXPathAPI();
NodeList nodeList = xPathAPI.getNodeSet(contextNode, "xpf:sort(//*, 'name()', 'text', 'ascending')"); // one can use the same xPathAPI instance as long as the requested context node doesn't change
   ...


If I missed a similar functionality while exploring Xalan distribution, could someone show me the relevant classes, or simply ignore this message, with my apologies.
--
Jerome Droz


---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscr...@xml.apache.org
For additional commands, e-mail: xalan-dev-h...@xml.apache.org

Reply via email to