Xml 针对 xsd 的验证接受错误的 xml 作为有效
Xml validation against xsd accept wrong xml as valid
我有 java 代码可以根据 xsd:
验证我的 xml
public static void main(String[] args) throws Exception {
Source xmlFile = new StreamSource(new File("src/main/resources/file.xml"));
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new Source[] {
new StreamSource(new File("src/main/resources/RegTypy.xsd")),
new StreamSource(new File("src/main/resources/XopInclude.xsd")),
new StreamSource(new File("src/main/resources/RobTypy.xsd")),
new StreamSource(new File("src/main/resources/RobDotazyData.xsd")), });
Validator validator = schema.newValidator();
try {
validator.validate(xmlFile);
System.out.println(xmlFile.getSystemId() + " is valid");
} catch (SAXException e) {
System.out.println(xmlFile.getSystemId() + " is NOT valid");
System.out.println("Reason: " + e.getLocalizedMessage());
}
}
Xsd下载是here:根目录和rob/xsd目录
现在我的 xml 看起来像:
<data:RobCtiAifoData xmlns:ns2="urn:cz:isvs:rob:schemas:RobTypy:v1" xmlns:data="urn:cz:isvs:rob:schemas:RobDotazyData:v1" xmlns:reg="urn:cz:isvs:reg:schemas:RegTypy:v1">
<data:Aifo stav="spravny" xsi:type="ns2:LokalniAifoStavType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2</data:Aifo>
<data:VyuzitiPoskytnuti>vyuziti</data:VyuzitiPoskytnuti>
</data:RobCtiAifoData>
xsd 的重要部分是:
<xs:complexType name="RobCtiAifoDataType">
<xs:annotation>
<xs:documentation xml:lang="cs">Čtení referenčních údajů podle AIFO.</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="Aifo" type="reg:LokalniAifoType" />
<!-- omezení - jenom využití nebo poskytnutí -->
<xs:element name="VyuzitiPoskytnuti" type="rob:TypVyuzitiPoskytnutiType" />
</xs:sequence>
<xs:attribute name="znepristupniLog" type="xs:boolean" />
</xs:complexType>
如您所见,Aifo 的类型应为 reg:LokalniAifoType
,但在 xml 中,它被覆盖为 ns2:LokalniAifoStavType,后者具有附加参数 stav。问题是为什么这种类型在 xml 中被接受以及为什么它有效 xml.
更新:
<xs:complexType name="LokalniAifoType">
<xs:annotation>
<xs:documentation xml:lang="cs">Lokální identifikátor AIFO. Klíč typu integer.</xs:documentation>
</xs:annotation>
<xs:simpleContent>
<xs:extension base="xs:int">
<xs:attribute name="prevodAifoStatus" type="PrevodAifoStatusType">
<xs:annotation>
<xs:documentation xml:lang="cs">Informace o výsledku převodu v ORG, pokud se nepodařilo přeložit.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="stavOvereniAifo" type="xs:boolean">
<xs:annotation>
<xs:documentation xml:lang="cs">Existence AIFO se má ověřit v ROB.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="LokalniAifoStavType">
<xs:annotation>
<xs:documentation xml:lang="cs">Agendový identifikátor fyzické osoby včetně stavu a času
poslední změny.
</xs:documentation>
</xs:annotation>
<xs:simpleContent>
<xs:extension base="reg:LokalniAifoType">
<xs:attribute name="stav" type="reg:StavType" />
<xs:attribute name="zmenaCas" type="ZmenaCasType" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
您显然是在 LokalniAifoStavType
中使用 xs:extension
进行扩展?如果一个元素指定它的类型 LokalniAifoType
,那么它的任何扩展类型都可以出现在 XML.
我有 java 代码可以根据 xsd:
验证我的 xmlpublic static void main(String[] args) throws Exception {
Source xmlFile = new StreamSource(new File("src/main/resources/file.xml"));
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new Source[] {
new StreamSource(new File("src/main/resources/RegTypy.xsd")),
new StreamSource(new File("src/main/resources/XopInclude.xsd")),
new StreamSource(new File("src/main/resources/RobTypy.xsd")),
new StreamSource(new File("src/main/resources/RobDotazyData.xsd")), });
Validator validator = schema.newValidator();
try {
validator.validate(xmlFile);
System.out.println(xmlFile.getSystemId() + " is valid");
} catch (SAXException e) {
System.out.println(xmlFile.getSystemId() + " is NOT valid");
System.out.println("Reason: " + e.getLocalizedMessage());
}
}
Xsd下载是here:根目录和rob/xsd目录
现在我的 xml 看起来像:
<data:RobCtiAifoData xmlns:ns2="urn:cz:isvs:rob:schemas:RobTypy:v1" xmlns:data="urn:cz:isvs:rob:schemas:RobDotazyData:v1" xmlns:reg="urn:cz:isvs:reg:schemas:RegTypy:v1">
<data:Aifo stav="spravny" xsi:type="ns2:LokalniAifoStavType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2</data:Aifo>
<data:VyuzitiPoskytnuti>vyuziti</data:VyuzitiPoskytnuti>
</data:RobCtiAifoData>
xsd 的重要部分是:
<xs:complexType name="RobCtiAifoDataType">
<xs:annotation>
<xs:documentation xml:lang="cs">Čtení referenčních údajů podle AIFO.</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="Aifo" type="reg:LokalniAifoType" />
<!-- omezení - jenom využití nebo poskytnutí -->
<xs:element name="VyuzitiPoskytnuti" type="rob:TypVyuzitiPoskytnutiType" />
</xs:sequence>
<xs:attribute name="znepristupniLog" type="xs:boolean" />
</xs:complexType>
如您所见,Aifo 的类型应为 reg:LokalniAifoType
,但在 xml 中,它被覆盖为 ns2:LokalniAifoStavType,后者具有附加参数 stav。问题是为什么这种类型在 xml 中被接受以及为什么它有效 xml.
更新:
<xs:complexType name="LokalniAifoType">
<xs:annotation>
<xs:documentation xml:lang="cs">Lokální identifikátor AIFO. Klíč typu integer.</xs:documentation>
</xs:annotation>
<xs:simpleContent>
<xs:extension base="xs:int">
<xs:attribute name="prevodAifoStatus" type="PrevodAifoStatusType">
<xs:annotation>
<xs:documentation xml:lang="cs">Informace o výsledku převodu v ORG, pokud se nepodařilo přeložit.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="stavOvereniAifo" type="xs:boolean">
<xs:annotation>
<xs:documentation xml:lang="cs">Existence AIFO se má ověřit v ROB.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="LokalniAifoStavType">
<xs:annotation>
<xs:documentation xml:lang="cs">Agendový identifikátor fyzické osoby včetně stavu a času
poslední změny.
</xs:documentation>
</xs:annotation>
<xs:simpleContent>
<xs:extension base="reg:LokalniAifoType">
<xs:attribute name="stav" type="reg:StavType" />
<xs:attribute name="zmenaCas" type="ZmenaCasType" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
您显然是在 LokalniAifoStavType
中使用 xs:extension
进行扩展?如果一个元素指定它的类型 LokalniAifoType
,那么它的任何扩展类型都可以出现在 XML.