Hi,
I've got a question about the order in which the reverse axis are evaluated.
Take a simple XML File for exapmle
<root>
<a/>
<b/>
<c/>
<d/>
<e/>
<f/>
<g/>
</root>
If I'm on the <f/>-Node and query the preceding-siblings.
I expect the
first preceding-sibling to be the <e/>-Node! It's
not <a/>-Node because reverse axis doesn't be evaluated in document-order. The <a/>-Node would rather be the last node in this preceding-sibling axis.
This is at least the way how XSLT is handling the reverse axis.
So what about DOM in Java?
I wrote a simple main method which suprised me with an different "interpretation" of this axis (see the comment in the code).
public static void main(String[] args) throws Exception {
XPath xPath = XPathFactory.newInstance().newXPath();
String xmlString = "<root><a/><b/><c/><d/><e/><f/><g/></root>";
StringReader r = new StringReader(xmlString);
Document doc = DocumentBuilderFactoryImpl.newInstance().newDocumentBuilder().parse(new InputSource(r));
Node n = (Node) xPath.evaluate("/root/f", doc, XPathConstants.NODE);
NodeList nodes = (NodeList) xPath.evaluate("preceding-sibling::node()", n, XPathConstants.NODESET);
for(int i=0; i<nodes.getLength(); ++i) {
System.out.print(nodes.item(i).getNodeName() + ", ");
}
// Output: a, b, c, d, e,
}
Did I made a mistake somwherre or is it a different way of evaluating the reverse axis?
btw.: I've had the same results when testing with preceding axis