fork download
  1. import java.io.*;
  2. import javax.xml.parsers.DocumentBuilderFactory;
  3. import javax.xml.xpath.*;
  4. import org.w3c.dom.*;
  5. import org.xml.sax.InputSource;
  6.  
  7. class test {
  8.  
  9. public static void main(String[] args) throws Exception {
  10.  
  11. XPathExpression expr = XPathFactory.newInstance().newXPath().compile(
  12. "/A[namespace-uri() = 'some-namespace']"); // This does not select anything, replacing A with * does
  13.  
  14. // This XPath selects as expected (in all parsers mentioned): /*[namespace-uri() = 'some-namespace']
  15.  
  16. String xml = "<A xmlns=\"some-namespace\"> </A>";
  17.  
  18. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  19. factory.setNamespaceAware(true);
  20. Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
  21. NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
  22.  
  23. System.out.println("Number of nodes selected: " + nodes.getLength());
  24.  
  25. for (int i = 0; i < nodes.getLength(); i++) {
  26. System.out.println("Node name: " + nodes.item(i).getNodeName());
  27. }
  28. }
  29. }
Success #stdin #stdout 0.09s 31732KB
stdin
Standard input is empty
stdout
Number of nodes selected: 0