Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
113 views
in Technique[技术] by (71.8m points)

java - Using XPath to select an element in XSD file always returns an empty list

I have an XSD file as below. And I want to select the node xs:element based on its attribute value name=content. I use the code below to get that element, but it always returns an empty list.

String strBuiltinSchema = new String(schemaContent, StandardCharsets.UTF_8);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(strBuiltinSchema)));

XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();

String expression = "//xs:element[@name='myapp']//xs:element[@name='content']";
NodeList nodeList = (NodeList) xPath.evaluate(expression,doc, XPathConstants.NODESET);
// nodeList is always empty...

And here is my input XSD file.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://xmlns.oracle.com/cloud/adapter/nxsd/surrogate/request"
       xmlns="http://xmlns.oracle.com/cloud/adapter/nxsd/surrogate/request"
       elementFormDefault="qualified">
<xs:element name="myapp">
    <xs:complexType>
    <xs:sequence>
        <xs:element name="content">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="EmployeeID" type="xs:string" maxOccurs="1" minOccurs="0"/>
                    <xs:element name="EName" type="xs:string" maxOccurs="1" minOccurs="0"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="attribute">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="item" type="xs:integer" maxOccurs="1" minOccurs="0"/>                        
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>

And here is its XML

<?xml version="1.0" encoding="utf-8"?>
<myapp>
  <content>
    <EmployeeID>7856932</EmployeeID>
    <EName>John William</EName>
  </content>
  <attribute>
    <item>3922</item>
  </attribute>
</myapp>
question from:https://stackoverflow.com/questions/65897917/using-xpath-to-select-an-element-in-xsd-file-always-returns-an-empty-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have 2 options:

  1. Remove xs: from the XPath expression (simple, but not recommended):

    String expression = "//element[@name='myapp']//element[@name='content']";
    

    See demo on IDEONE.

  2. Enable namespaces:

    factory.setNamespaceAware(true);
    
    xPath.setNamespaceContext(new NamespaceContext() {
        @Override
        public String getNamespaceURI(String prefix) {
            switch (prefix) {
                case "xs": return "http://www.w3.org/2001/XMLSchema";
                default:   return null;
            }
        }
        @Override
        public String getPrefix(String namespaceURI) {
            throw new UnsupportedOperationException();
        }
        @Override
        public Iterator<String> getPrefixes(String namespaceURI) {
            throw new UnsupportedOperationException();
        }
    });
    

    See demo on IDEONE.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...