针对 xsd 验证 xml 会忽略额外的元素
validating xml against xsd ignores extra elements
我正在尝试根据 XSD 文件验证 XML 文件,但我添加到 XML 的额外元素未被识别为错误。
xsd:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://myschema.test.de"
xmlns="https://myschema.test.de"
elementFormDefault="qualified">
<xs:element name="items" type="itemsType">
<xs:complexType >
<xs:sequence>
<xs:element name="item" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="id"/>
<xs:element type="xs:string" name="listrelativepath"/>
<xs:element type="xs:string" name="filename"/>
<xs:element name="fields" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="field"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element type="xs:string" name="localstorage">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
xml:
<?xml version="1.0" encoding="UTF-8"?>
<!--File Backup Generated 05/09/2022 16:53:53
v1.0-->
<items xmlns="http://myschema.test.de" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="https://myschema.test.de/xml/schema.xsd">
<item>
<ElementNotBelongingHere />
<id>1</id>
<listrelativepath>202010 H3D/Antragsakte/Externe Stellungnahme (ohne EIB,NAM,SAC)</listrelativepath>
<filename />
<fields>
<field FileLeafRef="Placeholder.docx" />
</fields>
<localstorage>C:\Users\prupprecht\Documents220110.txt</localstorage>
</item>
</items>
xml 验证但有一个不属于架构的额外元素。有什么想法吗?
请尝试以下XSD。
XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://myschema.test.de" xmlns:ns1="http://myschema.test.de">
<xs:element name="items">
<xs:complexType>
<xs:sequence>
<xs:element ref="ns1:item"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item">
<xs:complexType>
<xs:sequence>
<xs:element ref="ns1:id"/>
<xs:element ref="ns1:listrelativepath"/>
<xs:element ref="ns1:filename"/>
<xs:element ref="ns1:fields"/>
<xs:element ref="ns1:localstorage"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="id" type="xs:integer"/>
<xs:element name="listrelativepath" type="xs:string"/>
<xs:element name="filename">
<xs:complexType/>
</xs:element>
<xs:element name="fields">
<xs:complexType>
<xs:sequence>
<xs:element ref="ns1:field"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="field">
<xs:complexType>
<xs:attribute name="FileLeafRef" use="required" type="xs:NCName"/>
</xs:complexType>
</xs:element>
<xs:element name="localstorage" type="xs:string"/>
</xs:schema>
架构本身无效,因此您永远不会使用它来验证实例文档。撒克逊人(运行 在氧气中)报告:
Description: A <complexType> element must not appear as a child
of an <xs:element> with a @type attribute
Start location: 7:47
如果修复该问题,我会收到错误消息,因为源文档的名称空间与架构的目标名称空间不匹配(http 与 https)。
如果我修复该问题,我会收到三个验证错误:
Description: Attribute @schemaLocation is not allowed on element <items>
Start location: 4:151
Description: In content of element <item>: The content model does not allow element
<Q{.../myschema.test.de}ElementNotBelongingHere> to appear as the first child.
Expected <Q{.../myschema.test.de}id>.
Start location: 6:34
Description: Attribute @FileLeafRef is not allowed on element <field>
Start location: 11:50
所以如果验证似乎成功,我不知道你做错了什么。
我正在尝试根据 XSD 文件验证 XML 文件,但我添加到 XML 的额外元素未被识别为错误。
xsd:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://myschema.test.de"
xmlns="https://myschema.test.de"
elementFormDefault="qualified">
<xs:element name="items" type="itemsType">
<xs:complexType >
<xs:sequence>
<xs:element name="item" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="id"/>
<xs:element type="xs:string" name="listrelativepath"/>
<xs:element type="xs:string" name="filename"/>
<xs:element name="fields" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="field"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element type="xs:string" name="localstorage">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
xml:
<?xml version="1.0" encoding="UTF-8"?>
<!--File Backup Generated 05/09/2022 16:53:53
v1.0-->
<items xmlns="http://myschema.test.de" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="https://myschema.test.de/xml/schema.xsd">
<item>
<ElementNotBelongingHere />
<id>1</id>
<listrelativepath>202010 H3D/Antragsakte/Externe Stellungnahme (ohne EIB,NAM,SAC)</listrelativepath>
<filename />
<fields>
<field FileLeafRef="Placeholder.docx" />
</fields>
<localstorage>C:\Users\prupprecht\Documents220110.txt</localstorage>
</item>
</items>
xml 验证但有一个不属于架构的额外元素。有什么想法吗?
请尝试以下XSD。
XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://myschema.test.de" xmlns:ns1="http://myschema.test.de">
<xs:element name="items">
<xs:complexType>
<xs:sequence>
<xs:element ref="ns1:item"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item">
<xs:complexType>
<xs:sequence>
<xs:element ref="ns1:id"/>
<xs:element ref="ns1:listrelativepath"/>
<xs:element ref="ns1:filename"/>
<xs:element ref="ns1:fields"/>
<xs:element ref="ns1:localstorage"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="id" type="xs:integer"/>
<xs:element name="listrelativepath" type="xs:string"/>
<xs:element name="filename">
<xs:complexType/>
</xs:element>
<xs:element name="fields">
<xs:complexType>
<xs:sequence>
<xs:element ref="ns1:field"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="field">
<xs:complexType>
<xs:attribute name="FileLeafRef" use="required" type="xs:NCName"/>
</xs:complexType>
</xs:element>
<xs:element name="localstorage" type="xs:string"/>
</xs:schema>
架构本身无效,因此您永远不会使用它来验证实例文档。撒克逊人(运行 在氧气中)报告:
Description: A <complexType> element must not appear as a child
of an <xs:element> with a @type attribute
Start location: 7:47
如果修复该问题,我会收到错误消息,因为源文档的名称空间与架构的目标名称空间不匹配(http 与 https)。
如果我修复该问题,我会收到三个验证错误:
Description: Attribute @schemaLocation is not allowed on element <items>
Start location: 4:151
Description: In content of element <item>: The content model does not allow element
<Q{.../myschema.test.de}ElementNotBelongingHere> to appear as the first child.
Expected <Q{.../myschema.test.de}id>.
Start location: 6:34
Description: Attribute @FileLeafRef is not allowed on element <field>
Start location: 11:50
所以如果验证似乎成功,我不知道你做错了什么。