Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

What is the order when querying "preceding-sibling" axis

843834Aug 25 2008
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 22 2008
Added on Aug 25 2008
0 comments
297 views