Please go easy on me , this is my first time using XPath 2.0 🙂
Given the following code :
import net.sf.saxon.expr.Expression;
import net.sf.saxon.expr.StaticContext;
import net.sf.saxon.functions.CompileTimeFunction;
import net.sf.saxon.sort.Reverser;
import net.sf.saxon.trans.XPathException;
public class MyReverse extends CompileTimeFunction {
public Expression simplify(StaticContext env) throws XPathException {
Reverser a = new Reverser(argument[0]);
return a.simplify(env);
}
}
I want to reverse the query :
String query = "inventory/book/chapter[3]/preceding-sibling::chapter//title";
Object myQuery= xpath.evaluate(query,doc,XPathConstants.NODESET);
for my XML file (I can attach the XML file if you wish ,so please say so if it’s indeed required!)
Now from my understanding if I do this :
MyReverse rev = new MyReverse();
then if I try to use the method evaluateAsString like this : rev.evaluateAsString(arg0)
then arg0 should be a XPathContext object .
How can I use my query with the above method ?
Regards
EDIT 1:
In the example that dear Mr. @Dimitre Novatchev wrote , what is needed is all the nodes from node X , to the upper side of the document.
However , if one of X's siblings , has children , then I need to present the sibling and only then his (the sibling’s) children (and after that move on to the next sibling , and the same all over again) , and not the child of the sibling & only then – the sibling .
I’m sorry for not mentioning & explaining this earlier
thanks again 🙂
Just evaluate this XPath 2.0 expression:
Here is an XSLT -based verification:
When this transformation is applied on the following XML document (taken from your previous question):
the above XPath expression is evaluated and the resulting sequence of nodes is copied to the output:
As we can see, the sequence contains the wanted elements and in reverse-document order — exactly as requested.
As for how to evaluate an XPath expression with Saxon 9.x, read the appropriate documentation: http://www.saxonica.com/documentation/xpath-api/s9api-xpath.xml which has a pointer to a fully working code example.
UPDATE:
In a comment the OP has indicated that he needs the
chapterelements in reverse document order, but their title elements in document order.To avhieve this, use: