解析 xsd 文件后得到空结果

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());
        }
    }
}

输入文件如下:

<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>

我使用 this 站点检查 xpath 是否正确并获取结果。我尝试了两种方法 method1() 和 method2() 但结果是一样的。我的目标是获取值的集合,但结果节点列表为空,请建议正确的获取方式。

我对 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);
}

我从 org.springframework.util.xml 复制了 class SimpleNamespaceContext 并且它起作用了。