Reputation: 744
I have an XML file which has contents as below
<?xml version="1.0" encoding="UTF-8"?>
<SETTINGS Version="1">
<CLEANING amount="40"/>
<CLEANING name="abcd"/>
<CLEANING initials="ABCD"/>
<MAINTENANCE state="on"/>
<MAINTENANCE temperature="F"/>
</SETTINGS>
I created an XSD schema for this XML using some tool which generated the below XSD output.
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="SETTINGS">
<xs:complexType>
<xs:sequence>
<xs:element name="CLEANING" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="amount" type="xs:unsignedByte" use="required" />
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="initials" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
<xs:element name="MAINTENANCE" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="state" type="xs:string" use="required" />
<xs:attribute name="temperature" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="Version" type="xs:unsignedByte" use="required" />
</xs:complexType>
</xs:element>
</xs:schema>
Now when i try to validate this XMl against the XSD schema generated, i get below errors in validation(validated using some online validator).
Not valid.
Error - Line 3, 24: org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 24; cvc-complex-type.4: Attribute 'name' must appear on element 'CLEANING'.
Error - Line 3, 24: org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 24; cvc-complex-type.4: Attribute 'initials' must appear on element 'CLEANING'.
Error - Line 4, 24: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 24; cvc-complex-type.4: Attribute 'amount' must appear on element 'CLEANING'.
Error - Line 4, 24: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 24; cvc-complex-type.4: Attribute 'initials' must appear on element 'CLEANING'.
Error - Line 5, 28: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 28; cvc-complex-type.4: Attribute 'amount' must appear on element 'CLEANING'.
Error - Line 5, 28: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 28; cvc-complex-type.4: Attribute 'name' must appear on element 'CLEANING'.
Error - Line 6, 26: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 26; cvc-complex-type.4: Attribute 'temperature' must appear on element 'MAINTENANCE'.
Error - Line 7, 31: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 31; cvc-complex-type.4: Attribute 'state' must appear on element 'MAINTENANCE'.
Both XML and XSD are valid formats as validated online. I am not able to understand the reason behind this error. I suspect it is because ofhaving multiple elements sharing same name for which I think there is some constraint in generating the XSD schema. Not sure exactly what's wrong.
What am I doing wrong here and what could be solution for this error?
Upvotes: 1
Views: 818
Reputation: 528
<xs:complexType>
<xs:attribute name="amount" type="xs:unsignedByte" use="required" />
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="initials" type="xs:string" use="required" />
</xs:complexType>
the word
require
mean that the attribute is needed so the corect writting of your XML is :
<CLEANING amount="40" name="abcd" initials="ABCD"/>
if you want to have you writing you need to remove require
of your XSD
this is the same for MAINTENANCE
your XSD verify this text :
<SETTINGS Version="1">
<CLEANING amount="40" name="abcd" initials="ABCD"/>
<MAINTENANCE state="on" temperature="F"/>
</SETTINGS>
But if you want to validate you XML with this look it is impossible to put use=require
so you will have somethig like :
...
<xs:complexType>
<xs:attribute name="amount" type="xs:unsignedByte" />
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="initials" type="xs:string" />
</xs:complexType>
...
Moreover if you want to have your form (so <a att1 ="" /> <a att2="" />
)
you will need to change your atribute like that :
<SETTINGS Version="1">
<A amount="40" />
<B name="abcd" />
<C initials="ABCD"/>
<D state="on" />
<E temperature="F"/>
</SETTINGS>
but I think that the last possiblility is maybe overkill to the first option
Upvotes: 2