Whitespace in document causes unexpected XPath behavior -------------------------------------------------------
Key: XMLBEANS-313 URL: https://issues.apache.org/jira/browse/XMLBEANS-313 Project: XMLBeans Issue Type: Bug Components: XPath Environment: Microsoft Windows XP Professional Version 2002 (Service Pack 2) Pentium 4 CPU 3.00GHz, 2.00 GB of RAM Reporter: Coram Bryant There is a bug in XmlBeans whereby the existence of whitespace in a document, wherein the elements are in a default namespace, causes unexpected XPath results. There are various ways in which this can be manifested (some more difficult to reproduce than others). The following code demonstrates a simple condition in which this can be seen, as well as similar conditions in which the bug is not manifested. In short, the following condition causes an invalid XPath: 1) The document has a default namespace 2) There are pair of siblings, the first of which is preceded by whitespace 3) In the context of the first sibling (in document order), the XPath expression "following-sibling::*[1]" yields the first sibling (rather than the second sibling, as would be expected). try { String xml; XmlObject xmlObject; XmlObject[] results; // This XML snippet contains a whitespace character before the child1 element. // There is a default namespace. // These conditions will yield the bug xml = "<root xmlns='http://some_ns'> <child1/><child2/></root>"; xmlObject = XmlObject.Factory.parse(xml); results = xmlObject.selectPath("declare namespace sn='http://some_ns'; /sn:root/sn:child1/following-sibling::*[1]/name()"); System.out.println(results[0].xmlText()); // Prints <xml-fragment>child2</xml-fragment> // This is the case that yields incorrect results // Note that the context element must be the first sibling in order to yield the bug. // The bug does not manifest itself if the XPath expression is evaluated from an ancestor, as shown above. results = xmlObject.selectPath("declare namespace sn='http://some_ns'; /sn:root/sn:child1"); results = results[0].selectPath("declare namespace sn='http://some_ns'; following-sibling::*[1]/name()"); System.out.println(results[0].xmlText()); // Prints <xml-fragment>child1</xml-fragment> // This XML snippet contains no whitespace characters. // There is a default namespace. // This condition does not cause the bug xml = "<root xmlns='http://some_ns'><child1/><child2/></root>"; xmlObject = XmlObject.Factory.parse(xml); results = xmlObject.selectPath("declare namespace sn='http://some_ns'; /sn:root/sn:child1/following-sibling::*[1]/name()"); System.out.println(results[0].xmlText()); // Prints <xml-fragment>child2</xml-fragment> results = xmlObject.selectPath("declare namespace sn='http://some_ns'; /sn:root/sn:child1"); results = results[0].selectPath("declare namespace sn='http://some_ns'; following-sibling::*[1]/name()"); System.out.println(results[0].xmlText()); // Prints <xml-fragment>child2</xml-fragment> // This XML Snippet contains a whitespace character before the child1 element. // There is no namespace. // This condition does not cause the bug xml = "<root> <child1/><child2/></root>"; xmlObject = XmlObject.Factory.parse(xml); results = xmlObject.selectPath("/root/child1/following-sibling::*[1]/name()"); System.out.println(results[0].xmlText()); // Prints <xml-fragment>child2</xml-fragment> results = xmlObject.selectPath("/root/child1"); results = results[0].selectPath("following-sibling::*[1]/name()"); System.out.println(results[0].xmlText()); // Prints <xml-fragment>child2</xml-fragment> } catch (XmlException e) { e.printStackTrace(); } -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]