Yarel
Yarel

Reputation: 444

XSD elements with the same name under the same depth

I have a question whether this XSD snippet is valid:

<xsd:element name="yolo" minOccurs="0"/>
<xsd:element name="yolo" />

example XML:

<yolo />

thanks

Upvotes: 0

Views: 392

Answers (2)

Petru Gardea
Petru Gardea

Reputation: 21638

Quick answer, the snippet is not valid.

Let's say that you put the above snippet in the following context (to make it clear what do you mean by the same depth, since @Janty got confused by it):

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" xmlns="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:complexType name="test">
        <xsd:sequence>
            <xsd:element name="yolo" minOccurs="0"/>
            <xsd:element name="yolo" />         
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

Assuming you try to above with a .NET XSD processor, you would get the following error:

Multiple definition of element 'http://tempuri.org/XMLSchema.xsd:yolo' causes the content model to become ambiguous. A content model must be formed such that during validation of an element information item sequence, the particle contained directly, indirectly or implicitly therein with which to attempt to validate each item in the sequence in turn can be uniquely determined without examining the content or attributes of that item, and without any information about the items in the remainder of the sequence.

If you try with a Xerces-based processor...

cos-nonambig: "http://tempuri.org/XMLSchema.xsd":yolo and "http://tempuri.org/XMLSchema.xsd":yolo (or elements from their substitution group) violate "Unique Particle Attribution". During validation against this schema, ambiguity would be created for those two particles.

The XSD spec describes cos-nonambig here (more or less Microsoft's implementation error message).

However, to add to the confusion, some processors allow you to override the UPA behaviour (specifically or indirectly), so it might very well happen to try the same and get no error... Microsoft does it with EnableUpaCheck and Xerces with their schema-full-checking feature.

Microsoft documentation applies in your case... this is their example... matching, isn't it?

<xs:sequence>
    <xs:element name="A" type="xs:string"/>
    <xs:element name="B" type="xs:string" minOccurs="0"/>
    <xs:element name="B" type="xs:string"/>
</xs:sequence>

Upvotes: 1

Janty
Janty

Reputation: 1714

For <yolo />

you should have the below schema

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="yolo" type="xs:string"/>
</xs:schema>

Please use the online tools to generate schema to xml or xml to schema as XSD2XML and XML2XSD

Upvotes: 0

Related Questions