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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…