[英]I am trying to validate XML against XMLSchema( XSD), below is the error I got for the code I have. May I know where I am going wrong?
Below is the code I have for validating XML file with XSD file.下面是我用 XSD 文件验证 XML 文件的代码。
public class XmlXsdValidation {
public static void main(String[] args) {
final String xsdPath = "D://tmp/xsdFile.xsd";
final String xmlPath = "D://tmp/xmlFile.xml";
File xsdFile = new File(xsdPath);
File xmlFile = new File(xmlPath);
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
javax.xml.validation.Schema xmlSchema = factory.newSchema(xsdFile);
Validator validator = xmlSchema.newValidator();
validator.validate(new StreamSource(xmlFile));
System.out.println("Sucess....");
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
}
Below is the xmlFile.xml content:以下是 xmlFile.xml 内容:
<s11:Envelope xmlns:s11='http://schemas.xmlsoap.org/soap/envelope/'>
<s11:Body>
<ns1:LanguageName xmlns:ns1='http://www.oorsprong.org/websamples.countryinfo'>
<ns1:sISOCode>abk</ns1:sISOCode>
</ns1:LanguageName>
</s11:Body>
</s11:Envelope>
Below is the xsdFile.xsd content:下面是 xsdFile.xsd 内容:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" targetNamespace="http://schemas.xmlsoap.org/soap/envelope/" elementFormDefault="qualified">
<xsd:element name="Envelope">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="Body" minOccurs="0">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="LanguageNameResponse" minOccurs="0">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="LanguageNameResult" minOccurs="0" type="xsd:normalizedString" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
OutPut( Error message): file:/D:/tmp/xmlFile.xml;输出(错误消息):文件:/D:/tmp/xmlFile.xml; lineNumber: 3;
行号:3; columnNumber: 83;
列号:83; cvc-complex-type.2.4.a: Invalid content was found starting with element 'ns1:LanguageName'.
cvc-complex-type.2.4.a:发现从元素“ns1:LanguageName”开始的无效内容。 One of '{"http://schemas.xmlsoap.org/soap/envelope/":LanguageNameResponse}' is expected.
'{"http://schemas.xmlsoap.org/soap/envelope/":LanguageNameResponse}' 之一是预期的。
WSDL file considered: https://www.swi-prolog.org/pack/file_details/wsdl/examples/country.wsdl考虑的 WSDL 文件: https ://www.swi-prolog.org/pack/file_details/wsdl/examples/country.wsdl
The SOAP envelope and the SOAP body are in different namespaces. SOAP 信封和 SOAP 主体位于不同的名称空间中。 You need one schema document for each namespace, with appropriate
xs:import
links connecting them.您需要为每个命名空间创建一个模式文档,并使用适当的
xs:import
链接将它们连接起来。 Each element must be declared in the schema document with the correct targetNamespace
.每个元素都必须在模式文档中用正确的
targetNamespace
声明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.