XPathAPI selectSingleNode

Dear All,

I’m suffering from the below problem for weeks, please kindly help. I don’t know why the below code fragment doesn’t work. I print out all the xml raw string of my resultset, it’s fine, but when I try to get a node’s value, I always get the same value. Stange is that if invoke xmlObject.writeTo(somestringwriter), then use DocumentBuilder to parse an InputSource, it works! What mistake I have made???

----------------------------------------------------
source:

TQuery tQuery = TQuery.newInstance(“/article/@ID”);
TResponse response = accessorA.query( tQuery, 10 );
if( response.hasFirstXMLObject() ) {
TXMLObjectIterator itr = response.getXMLObjectIterator();
for ( count = 0 ; itr.hasNext() ; count++) {
TXMLObject xmlObject = itr.next();
Element element = (Element)xmlObject.getElement();

TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource( element );
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);

// It works if insert the belows
// StringWriter sw = new StringWriter();
// DocumentBuilder db = DocumentBuilderFactory.newDocumentBuilder();
// element = db.parse(new InputSource(new StringReader(sw.toString())));

System.out.println(org.apache.xpath.XPathAPI.selectSingleNode(element, “//article/@ID”)+“\n”);

}
}

--------------------------------------------------
output:

<?xml version="1.0" encoding="UTF-8"?>
231883

<?xml version="1.0" encoding="UTF-8"?>
231883

<?xml version="1.0" encoding="UTF-8"?>
231883

<?xml version="1.0" encoding="UTF-8"?>
231883

<?xml version="1.0" encoding="UTF-8"?>
231883

<?xml version="1.0" encoding="UTF-8"?>
231883

<?xml version="1.0" encoding="UTF-8"?>
231883

<?xml version="1.0" encoding="UTF-8"?>
231883

<?xml version="1.0" encoding="UTF-8"?>
231883

<?xml version="1.0" encoding="UTF-8"?>
231883

------------------------------------------------------


Best regards,
Lun

I think selectSingleNode(“//article”) always gives you the first ‘article’ Node in
your response document since your XPath-expression starts at the document root (“//”)
and selectSingleNode() returns the first Node matching.

selectSingleNode(“./@ID”) should work.

HTH

Hi HTH,

Thank you very very very much!!! It works!!! But why?? By the Xalan Java Doc, it state that,
contextNode - The node to start searching from.
The elements I passed into the method is just,
(the output is a while loop)
one at a time, get by xmlObject.getElement(), which can be seen at the writeTo(System.out), is not the whole document.
So, no matter always return first or last node, I suppose it is only that one and only one node, isn’t it? Am I conceptually wrong in using xmlObject.getElement()?

Thanks again and again!

Best regards,
Lun

I see what you mean but I think that with xpath like //, the value for start node is not relevant. The search is done in the entire response document simply because the xpath argument // means “anywhere after the root” (including “above this node”).