0

I am upgrading from XPath 1.0 to XPath 3.1. I used the library javax.xml.xpath for XPath 1.0 and am now using the Saxon-HE library. It was fairly straightforward to migrate the code and I used the S9API interface for the evaluation of the expressions as suggested by saxon.

There is however one little part of code that I can not seem to figure out how to migrate:

public static Object getXpathValueFromContentMap(String name, Map<String, Object> content, boolean list) {
    try {
        if (list) {
            return newArrayList(JXPathContext.newContext(content).iterate(name));
        } else {
            return JXPathContext.newContext(content).getValue(name);
        }
    } catch (JXPathNotFoundException ignore) {
        return null;
    }
}

This method evalutes an xpath expression on a map. Can this be done using the saxon library and if so, how?

Tim
  • 445
  • 5
  • 19
  • What is `JXPathContext`? That doesn't seem to be part of the JAXP `java.xml.xpath` API. What kind of "XPath" expressions on maps do you use? Do you expect them to work in XPath 3.1? XPath 3.1 has XDM maps and arrays and expressions to query/select from them but these are not XPath 1.0 path expressions, rather use a new, XPath 3.1 specific syntax like `?foo` to lookup the property `foo` in the context map and returns its value. – Martin Honnen Feb 01 '23 at 17:32
  • So a context item for s9api can be an XdmMap you can build with e.g. https://www.saxonica.com/html/documentation11/javadoc/net/sf/saxon/s9api/JsonBuilder.html#parseJson-java.io.Reader- – Martin Honnen Feb 01 '23 at 17:36
  • And there is https://www.saxonica.com/html/documentation11/javadoc/net/sf/saxon/s9api/XdmMap.html#makeMap-java.util.Map- to construct an XdmMap from a Java Map. – Martin Honnen Feb 01 '23 at 17:37

1 Answers1

1

JXPath (which I haven't come across before) appears to be an implementation of XPath 1.0 over (an XML node view of) Java data structures such as maps and arrays.

Saxon doesn't have anything similar. With XPath 3.1 it would be much more appropriate to interpret Java maps and arrays as XPath 3.1 maps and arrays, rather than going via an XML node view. However, the XPath expressions for doing that will be significantly different.

It's theoretically possible to provide an XML mapping in Saxon over an external data structure but it's a significant amount of work and that doesn't seem the right approach. But it rather depends on how much XPath code you've got that needs converting.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164