javaxpathxsdxml-parsingjavax

Getting empty result after parsing xsd file


public class XSDReader {
    public static void main(String[] args) {
        try {
            method1();
            method2();
        } catch (XPathExpressionException | ParserConfigurationException | IOException | SAXException e1) {
            e1.printStackTrace();
        }

    }

    private static void method1() throws XPathExpressionException, FileNotFoundException {
        XPath xpath = XPathFactory.newInstance().newXPath();
        String expression= "//xs:simpleType[@name='ReferenceDateEnum']//xs:enumeration";
        NodeList result=(NodeList) xpath.evaluate(expression,new InputSource(new FileInputStream("cprd-schema-v4-1.xsd")), XPathConstants.NODESET);     
        print(result);
    }

    private static void method2() throws ParserConfigurationException, XPathExpressionException, SAXException, IOException {

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        DocumentBuilder db = dbf.newDocumentBuilder();
        final InputStream fileInputStream = new FileInputStream("cprd-schema-v4-1.xsd");
        Document doc = db.parse(fileInputStream);
        XPath xpath = XPathFactory.newInstance().newXPath();
        String expression= "//xs:simpleType[@name='ReferenceDateEnum']//xs:enumeration";
        NodeList result=(NodeList)  xpath.compile(expression).evaluate(doc,XPathConstants.NODESET);
        print(result);


    }

    private static void print(NodeList result) {
        for(int i = 0; i < result.getLength(); i++) 
        {
            Element e = (Element) result.item(i);
            System.out.println(e.getAttribute("name") + " = " + e.getNodeValue());
        }
    }
}

Input File is as below:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:xmlns:centralbank.ie:anacredit" xmlns:cbi="urn:xmlns:centralbank.ie:anacredit" elementFormDefault="qualified" >
 <xs:simpleType name="ReferenceDateEnum">
    <xs:restriction base="xs:string">
      <xs:enumeration value="2017-09-30"/>
      <xs:enumeration value="2017-10-31"/>
      <xs:enumeration value="2017-11-30"/>
      <xs:enumeration value="2017-12-31"/>
      <xs:enumeration value="2018-01-31"/>
      <xs:enumeration value="2018-02-28"/>
      <xs:enumeration value="2018-03-31"/>
      <xs:enumeration value="2018-04-30"/>
      <xs:enumeration value="2018-05-31"/>
      <xs:enumeration value="2018-06-30"/>
      <xs:enumeration value="2018-07-31"/>
      <xs:enumeration value="2018-08-31"/>
      <xs:enumeration value="2018-09-30"/>
      <xs:enumeration value="2018-10-31"/>

    </xs:restriction>
  </xs:simpleType>

</xs:schema>

I used this site to check if the xpath is correct and it fetches the result. I tried two ways method1() and method2() but the result is same. My aim is to get the collection of the values but I'm getting empty result node list, please suggest the right way to fetch it.


Solution

  • I made following changes in method1()

    private static void method1() throws XPathExpressionException, FileNotFoundException {
        XPath xpath = XPathFactory.newInstance().newXPath();
        HashMap<String, String> prefMap = new HashMap<String, String>();
        prefMap.put("xs", "http://www.w3.org/2001/XMLSchema");
        prefMap.put("cbi","urn:xmlns:centralbank.ie:anacredit");
        xpath.setNamespaceContext(new SimpleNamespaceContext(prefMap));
        String expression="//xs:simpleType[@name='ReferenceDateEnum']//xs:enumeration";
        NodeList result=(NodeList) xpath.evaluate(expression,new InputSource(new FileInputStream(new File("cprd-schema-v4-1.xsd"))), XPathConstants.NODESET);       
        print(result);
    }
    

    where I copied the class SimpleNamespaceContext from org.springframework.util.xml and it worked.