I try to get just a part of an attribute (could also be a part of an element), but no luck so far. The exception I get is "Invalid XPath expression: //@att/tokenize(., ' ')[.][position() = 1] Expected node-type". I'm using dom4j 1.6.1.
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.XPath;
import org.dom4j.xpath.DefaultXPath;
public class TestXPathString {
/**
* @param args
* @throws DocumentException
*/
public static void main(String[] args) throws DocumentException {
Document doc = DocumentHelper
.parseText("<x><y att='I just need the first word' /></x>");
XPath xpath = new DefaultXPath("//@att/tokenize(., ' ')[.][position() = 1]");
Object result = xpath.evaluate(doc);
System.out.printf("Type: %s, Value: %s\n", result.getClass()
.getSimpleName(), result);
}
}
It doesn't work with
doc.selectObject("//@att/substring-before(., ' ')");
either.
Any ideas?
Applying functions in axis steps is an XPath 2.0 feature; Java (and dom4j) only support XPath 1.0. You cannot do this.
If you only expect a single result, use substring-before(//@att, ' ')
. Otherwise, pass the whole attribute value to Java and do the string manipulation outside XPath.
You might consider plugging a more powerful XPath/XQuery engine, like Saxon, BaseX or other native XML databases with Java APIs available.