1

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?

Jack
  • 694
  • 8
  • 20

1 Answers1

2
<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

charles Lgn
  • 500
  • 2
  • 7
  • 28
  • 1
    Charles is correct, the XSD specifies that each element of CLEANING, of which you can have multiple (maxOccurs=unbounded) needs to have all attributes. You will either have to change the XML or the XSD to make them validate together. And yes, the XSD in itself is valid, as is the XML, however the XML does not adhere to the specific rules set by this XSD, only the general XML rules. – ophychius Dec 18 '18 at 06:55
  • I have a doubt here @charles, Does and mean one and same thing. I mean if we are using first version will the code be able to parse and extract attribute values in the same way if we change xml to second way. Will there any code changes required to parse the new XML. – Jack Dec 18 '18 at 07:01
  • In case if i dont put use="required" in XSD for an attribute and if my XML doesn't contain that attribute too it will lead to successful validation, however in my case all these attributes need to be present in XML. – Jack Dec 18 '18 at 07:08
  • 1
    it depend of what you want @Viki. in your settings, you have only one different attribute and three case so write ``is different than ` ` in the first case you have all you need in one bloc. on the other side you have all split. the fact is that if you take the second option (so you remove require) you will also be able to write ` ` or ` ` or `` or that `` for instance and I don't think that what you want – charles Lgn Dec 18 '18 at 07:12
  • @charles We have legacy code which uses XML's in above format and now we are planning to write XSD for verifying the schema as well as content values in XML's by using restriction on attribute values in XSD file. For some reason changing XML format is not possible, in this case. Is it impossible to write XSD schema with restrictions on attribute values. As per your suggestion if we remove "use=required" then it might work but in that case even if the XML doesn't contain that attribute it leads to successful validation of XML---> – Jack Dec 18 '18 at 15:31
  • ---which is not desired as all the attributes and elements mentioned in XML must be present in our case. Is there any workaround to write XSD(with restriction on attribute values) for such XML's without changing XML format. – Jack Dec 18 '18 at 15:31
  • @Viki I don't think we can do that... [a stack with the same prob](https://stackoverflow.com/questions/763072/xsd-one-of-2-attributes-is-required) – charles Lgn Dec 18 '18 at 15:37