Select single node like /bookstore/book[last()]
Added by Mario Mueller about 6 years ago
Hi,
how do I select a single node. Do I always have to use a List and get the first segment
List matchedLines = (List>)xPathExpression.evaluate(doc, XPathConstants.NODESET);
Regards Mario
Replies (5)
Please register to reply
RE: Select single node like /bookstore/book[last()]
-
Added by Michael Kay about 6 years ago
You appear to be using the JAXP XPath interface. This was designed for XPath 1.0 which has only four datatypes: number, string, boolean, and node-set. So yes, with this API if you want to select a single node then it has to be handled as a singleton node-set.
I would strongly recommend moving to the Saxon s9api interface, which handles the whole XPath 2.0/3.1 data model with much better type safety than JAXP.
(This might also explain why you were unable to use my suggestion in another message. JAXP can't properly handle an XPath expression that returns a sequence of strings.)
RE: Select single node like /bookstore/book[last()]
-
Added by Mario Mueller about 6 years ago
Mi Michael,
I am a little bit confused. I am using the following statements in my coding:
// Following is specific to Saxon: should be in a properties file
System.setProperty("javax.xml.xpath.XPathFactory:" + NamespaceConstant.OBJECT_MODEL_SAXON,
"net.sf.saxon.xpath.XPathFactoryImpl");
xpf = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON);
xpe = xpf.newXPath();
System.err.println("Loaded XPath Provider " + xpe.getClass().getName());
In the console I get:
Loaded XPath Provider net.sf.saxon.xpath.XPathEvaluator
It seems, that I am using the Saxon s9api interface ? Or am I wrong?
Regards Mario
RE: Select single node like /bookstore/book[last()]
-
Added by Michael Kay about 6 years ago
You are using Saxon's implementation of JAXP.
Please see http://www.saxonica.com/documentation/#!xpath-api for details.
RE: Select single node like /bookstore/book[last()]
-
Added by Mario Mueller about 6 years ago
Hello Michael,
thanks. But s9api requires the EE-edition? Am I right?
BTW: I ordered you book (XSLT 2.0 and XPATH 2.0) yesterday :-)
Thanks, Regards Mario
RE: Select single node like /bookstore/book[last()]
-
Added by Michael Kay about 6 years ago
No, the s9api interface is available in Saxon-HE.
Please register to reply