I am trying to validate a XML file against a XSD file but extra elements I add to the XML are not recognized as wrong.
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\Documents\20220110.txt</localstorage>
</item>
</items>
the xml validates but has an extra element not belonging in the Schema. Any Ideas?
CodePudding user response:
Please try the following 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>
CodePudding user response:
The schema itself is not valid, so you should never have got as far as using it to validate an instance document. Saxon (running within Oxygen) reports:
Description: A <complexType> element must not appear as a child
of an <xs:element> with a @type attribute
Start location: 7:47
If I fix that, I get an error because the namespace of the source document doesn't match the target namespace of the schema (http vs https).
If I fix that, I get three validation errors:
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
So I don't know what you're doing wrong if validation appeared to succeed.