[英]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?
下面是我用 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();
}
}
}
以下是 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>
下面是 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>
輸出(錯誤消息):文件:/D:/tmp/xmlFile.xml; 行號:3; 列號:83; cvc-complex-type.2.4.a:發現從元素“ns1:LanguageName”開始的無效內容。 '{"http://schemas.xmlsoap.org/soap/envelope/":LanguageNameResponse}' 之一是預期的。
考慮的 WSDL 文件: https ://www.swi-prolog.org/pack/file_details/wsdl/examples/country.wsdl
SOAP 信封和 SOAP 主體位於不同的名稱空間中。 您需要為每個命名空間創建一個模式文檔,並使用適當的xs:import
鏈接將它們連接起來。 每個元素都必須在模式文檔中用正確的targetNamespace
聲明。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.