DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT <http://nagoya.apache.org/bugzilla/show_bug.cgi?id=22311>. ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND INSERTED IN THE BUG DATABASE.
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=22311 XPath expresssion does not work against cloned node Summary: XPath expresssion does not work against cloned node Product: XalanJ2 Version: 2.5 Platform: PC OS/Version: Windows XP Status: NEW Severity: Normal Priority: Other Component: org.apache.xpath AssignedTo: [EMAIL PROTECTED] ReportedBy: [EMAIL PROTECTED] I am using XPathAPI to select nodes using a simple XPath expression. It works fine when the context node is part of a Document, but if I make a deep clone of that node and use the same XPath expression, nothing is returned. I tested this against Java 2 SDK 1.4.2, Xalan 2.5.1, and Xerces 2.5.0. I have attached sample code below that demonstrates the problem. I realize that the current behavior may be the desired one (i.e., the methods only work against nodes that have parents), but the method signature and javadoc do not mention this. Also, there are other expressions that do work against the cloned node (e.g., "//state"). Finally, if the context node truly must have a parent, then perhaps an Exception should be thrown if this is not the case. ========================================== SAMPLE CODE ========================================== import java.io.*; import javax.xml.parsers.*; import org.w3c.dom.*; import org.w3c.dom.traversal.*; import org.apache.xpath.*; import org.xml.sax.InputSource; public class Test { public static void main(String[] args) throws Throwable{ String xmlString = "<root><state>California</state></root>"; DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance (); DocumentBuilder builder = builderFactory.newDocumentBuilder(); Document xmlDoc = builder.parse(new InputSource(new StringReader (xmlString))); Element root = xmlDoc.getDocumentElement(); Node rootClone = root.cloneNode(true); String xpath= "//root/state"; // String xpath= "//state"; // String xpath= "state"; System.out.println("Applying XPath to root -- expecting to find one node."); applyXPath(root, xpath); System.out.println("Applying XPath to root clone -- expecting to find one node."); applyXPath(rootClone, xpath); xmlDoc.removeChild(root); xmlDoc.appendChild(rootClone); System.out.println("Applying XPath to root clone after adding to Document - - expecting to find one node."); applyXPath(rootClone, xpath); } private static void applyXPath(Node node, String xpath) throws Throwable{ NodeIterator ni = XPathAPI.selectNodeIterator(node, xpath); Node selected; while ((selected = ni.nextNode()) != null){ System.out.println("Selected: " + selected.getNodeName()); } } } ========================================== OUTPUT ========================================== Applying XPath to root -- expecting to find one node. Selected: state Applying XPath to root clone -- expecting to find one node. Applying XPath to root clone after adding to Document -- expecting to find one node. Selected: state
