Bugs item #1116471, was opened at 2005-02-04 22:06 Message generated for change (Comment added) made by maartenc You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=116035&aid=1116471&group_id=16035
Category: None Group: None Status: Open Resolution: None Priority: 5 Submitted By: Steve Carter (cart33) >Assigned to: Maarten Coene (maartenc) Summary: Problem with XPath and retrieving text Initial Comment: I have a Junit test similar to the following: public void test() { fiinal String XML = "<a><b>Water T & D-46816</b></a>"; final String XPATH = "a/b/text()"; final String EXPECTED_VALUE = "Water T & D-46816"; XPath xpathObj = createXpathObject(XPATH ); Document doc = createDocument(XML ); Object node = xpathObj.selectSingleNode(doc); if (node instanceof Text) { result = ((Text) node).getText(); } assertEquals(EXPECTED_VALUE, result)); } which fails because getText() only returns: Water T interrogating the node object returned from selectSingleNode indicates that the expected result is present as 3 seperate text elements in the content (ArrayList) member variable I can retrieve the value if I tweak the approach to use: final String XPATH = "a/b"; if (node instanceof Element) { return (String) ((Element) node).getData(); } If i dont have entity references then the first approach always works. Therefore this seems to be a bug, please correct me if i am wrong. ---------------------------------------------------------------------- >Comment By: Maarten Coene (maartenc) Date: 2005-02-12 15:57 Message: Logged In: YES user_id=178745 I don't think this is a bug. The following happened: expression "a/b/text()" selects all text nodes of <b>. Because you have an entity reference in it, the SAX parser you have used did create 3 text nodes: "Water T ", "&" and " D-46816". The selectSingleNode() method returns the first node: "Water T ". So this is correct. expression "a/b" selects all <b> elements. If you apply the string function to it, you will retrieve the string-value of the <b> element. This expression should do the trick: "string(a/b[1])", as illustrated by the example below: String xml = "<a><b>Water T & D-46816</b></a>"; Document doc = DocumentHelper.parseText(xml); String result = (String) doc.selectObject("string(a/b[1])"); now, result is equal to "Water T & D-46816" Another way is to retrieve the node and ask for the string-value directly on the node: Node node = doc.selectSingleNode("a/b"); String result = node.getStringValue(); I hope this helped you out. If you still feel this is a bug, please tell me otherwise I'll close this issue. regards, Maarten ---------------------------------------------------------------------- Comment By: Nobody/Anonymous (nobody) Date: 2005-02-10 18:49 Message: Logged In: NO This problem affects other xpath query types sch as /a/b/* etc... ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=116035&aid=1116471&group_id=16035 ------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click _______________________________________________ dom4j-dev mailing list dom4j-dev@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/dom4j-dev