[ http://issues.apache.org/jira/browse/XALANJ-1630?page=all ]
Brian Minchau updated XALANJ-1630:
----------------------------------
Version: Latest Development Code
(was: 2.5)
> XPath expresssion does not work against cloned node
> ---------------------------------------------------
>
> Key: XALANJ-1630
> URL: http://issues.apache.org/jira/browse/XALANJ-1630
> Project: XalanJ2
> Type: Bug
> Components: XPath
> Versions: Latest Development Code
> Environment: Operating System: Windows XP
> Platform: PC
> Reporter: Gary Hirschhorn
> Assignee: Xalan Developers Mailing List
>
> 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
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]