fork download
  1. import java.io.StringReader;
  2. import java.util.Iterator;
  3. import javax.xml.namespace.NamespaceContext;
  4. import javax.xml.parsers.DocumentBuilder;
  5. import javax.xml.parsers.DocumentBuilderFactory;
  6. import javax.xml.xpath.XPath;
  7. import javax.xml.xpath.XPathConstants;
  8. import javax.xml.xpath.XPathFactory;
  9. import org.w3c.dom.Document;
  10. import org.w3c.dom.NodeList;
  11. import org.xml.sax.InputSource;
  12.  
  13. class Ideone {
  14. public static void main(String[] args) throws Exception {
  15. String schema = "<xs:schema xmlns:xs=\"http://w...content-available-to-author-only...3.org/2001/XMLSchema\" targetNamespace=\"http://x...content-available-to-author-only...e.com/cloud/adapter/nxsd/surrogate/request\"\r\n" +
  16. " xmlns=\"http://x...content-available-to-author-only...e.com/cloud/adapter/nxsd/surrogate/request\"\r\n" +
  17. " elementFormDefault=\"qualified\">\r\n" +
  18. "<xs:element name=\"myapp\">\r\n" +
  19. " <xs:complexType>\r\n" +
  20. " <xs:sequence>\r\n" +
  21. " <xs:element name=\"content\">\r\n" +
  22. " <xs:complexType>\r\n" +
  23. " <xs:sequence>\r\n" +
  24. " <xs:element name=\"EmployeeID\" type=\"xs:string\" maxOccurs=\"1\" minOccurs=\"0\"/>\r\n" +
  25. " <xs:element name=\"EName\" type=\"xs:string\" maxOccurs=\"1\" minOccurs=\"0\"/>\r\n" +
  26. " </xs:sequence>\r\n" +
  27. " </xs:complexType>\r\n" +
  28. " </xs:element>\r\n" +
  29. " <xs:element name=\"attribute\">\r\n" +
  30. " <xs:complexType>\r\n" +
  31. " <xs:sequence>\r\n" +
  32. " <xs:element name=\"item\" type=\"xs:integer\" maxOccurs=\"1\" minOccurs=\"0\"/> \r\n" +
  33. " </xs:sequence>\r\n" +
  34. " </xs:complexType>\r\n" +
  35. " </xs:element>\r\n" +
  36. " </xs:sequence>\r\n" +
  37. " </xs:complexType>\r\n" +
  38. "</xs:element>\r\n" +
  39. "</xs:schema>\r\n";
  40. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  41. factory.setNamespaceAware(true);
  42. DocumentBuilder builder = factory.newDocumentBuilder();
  43. Document doc = builder.parse(new InputSource(new StringReader(schema)));
  44.  
  45. XPathFactory xPathFactory = XPathFactory.newInstance();
  46. XPath xPath = xPathFactory.newXPath();
  47. xPath.setNamespaceContext(new NamespaceContext() {
  48. @Override
  49. public String getNamespaceURI(String prefix) {
  50. switch (prefix) {
  51. case "xs": return "http://w...content-available-to-author-only...3.org/2001/XMLSchema";
  52. default: return null;
  53. }
  54. }
  55. @Override
  56. public String getPrefix(String namespaceURI) {
  57. }
  58. @Override
  59. public Iterator<String> getPrefixes(String namespaceURI) {
  60. }
  61. });
  62.  
  63. String expression = "//xs:element[@name='myapp']//xs:element[@name='content']";
  64. NodeList nodeList = (NodeList) xPath.evaluate(expression,doc, XPathConstants.NODESET);
  65.  
  66. System.out.println("Node count: " + nodeList.getLength());
  67. }
  68. }
Success #stdin #stdout 0.41s 42688KB
stdin
Standard input is empty
stdout
Node count: 1