[jdom-interest] XPath.selectNodes(doc) via Jaxen: nodes not in document-order

Kai Wörner kai.woerner at uni-hamburg.de
Mon Aug 16 02:36:39 PDT 2004

Hi List,

as far as I've understood the documentation, a xpath.selectNodes() should
return a list including the xpath results in document order. Given the
following XML:

<?xml version="1.0" encoding="UTF-8"?>
	<s><w no="1">word1</w></s>
	<s><w no="2">word2</w></s>
	<s><q><w no="3">word3</w></q></s>
	<s><w no="4">word4</w></s>

(mind the line w/ word no. 3) and the following java-program:

	SAXBuilder parser = new SAXBuilder();
		try {
			doc = parser.build("test.xml");
			root = doc.getRootElement();
			xpath = XPath.newInstance("//w");
			List words = xpath.selectNodes(doc);
			Iterator i = words.iterator();
			while (i.hasNext()) {
System.out.print(((Element)i.next()).getAttributeValue("no")+", ");
		} catch (Exception e) {

I'm getting the result 1,2,4,3, 
But I want to get 1,2,3,4,

Am I getting something wrong or what is "document order" supposed to mean?


Kai Wörner

More information about the jdom-interest mailing list