celeborn
celeborn

Reputation: 292

XML schema attribute errors with type, appearance and syntax

I need to validate my XML against the well defined schema. The problem is that I normally receive a lot of errors. Had an extensive look of your examples and everything seems to be alright. The problems are as follows:

Multiple annotations found at this line:
    - s4s-att-not-allowed: Attribute 'type' cannot appear in element 'attribute'.
    - s4s-elt-invalid-content.1: The content of '#AnonType_frominterNodeConnect' is invalid. Element 'attribute' is invalid, misplaced, or 
     occurs too often.
    - src-resolve: Cannot resolve the name 'name' to a(n) 'attribute declaration' component.

Here is the example of XML:

<struct>
        <attribute name="sensorReading"/>
        <field name="photo" type="integer"/>
        <field name="solar" type="integer"/>
        <field name="temp" type="real"/>
        <field name="humid" type="real"/>
</struct>

Here is the schema that needs to validate it:

<xs:element name="struct">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="field" minOccurs="0" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:attribute ref="name" type="xs:string" minOccurs="1" maxOccurs="1"/>
                        <xs:attribute ref="type" type="xs:string" minOccurs="1" maxOccurs="1"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:attribute ref="name" type="xs:string" minOccurs="1" maxOccurs="1"/> 
        </xs:complexType>
</xs:element>

I do not understand why this errors appear. Read somewhere here that attribute needs to be moved at the end of the complex type but this obviously doesn't help much.

Regards to every one that knows where the errors are.

Upvotes: 1

Views: 240

Answers (1)

sergioFC
sergioFC

Reputation: 6016

A possible XML Schema to validate that XML is this:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="struct">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="attribute">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="xs:string">
                                <xs:attribute name="name" use="required" />
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
                <xs:element name="field" minOccurs="0" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:attribute name="name" type="xs:string" use="required" />
                        <xs:attribute name="type" type="xs:string" use="required" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

¿Why?

  1. Attributes can't have maxOccurs and minOccurs (they have attribte use=required|optional|prohibited).
  2. You have in your XML a element named attribute (that's not an attribute, its an element).
  3. Attribute ref its used to reference things defined in other parts of your XML Schema. If you want to specify the attribute name use the attribute name="x".

As I said in my comment it would be interesting if you read this simple tutorial

Upvotes: 2

Related Questions