xml - Specify type for an element that must have at least one child element -


the element elements must contain @ least element element1 or element2, each of them @ once. i've specified following xml schema:

<?xml version="1.0" encoding="utf-8"?>  <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema" elementformdefault="qualified">    <xs:element name="elements" type ="elementstype"/>    <xs:complextype name="elementstype">       <xs:choice>          <xs:sequence>             <xs:element name="element1" type="xs:string" minoccurs="0"/>             <xs:element name="element2" type="xs:double"/>          </xs:sequence>          <xs:element name="element1" type="xs:string"/>       </xs:choice>    </xs:complextype> <xs:schema/> 

validation of xml schema gives "unique particle attribution" violation error:

cos-nonambig: element1 , element1 (or elements substitution group) violate "unique particle attribution". during validation against schema, ambiguity created 2 particles.

why?

the reason xsd violates unique particle attribution due ambiguity across xs:choice whether element1 should associated first or second choice possibility.

you can avoid violation , still require @ least 1 of element1 , element2 using xsd 1.1's assertions:

<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema"   xmlns:vc="http://www.w3.org/2007/xmlschema-versioning" elementformdefault="qualified"   vc:minversion="1.1">   <xs:element name="elements" type ="elementstype"/>   <xs:complextype name="elementstype">     <xs:sequence>       <xs:element name="element1" type="xs:string" minoccurs="0"/>       <xs:element name="element2" type="xs:double" minoccurs="0"/>     </xs:sequence>     <xs:assert test="count(*) > 0"/>   </xs:complextype> </xs:schema> 

update

i don't understand why there ambiguity. if instance contains element1 , element2 or element2 first choice possible. otherwise, if there element2, second choice applies only.

pretend you're validating parser , encounter element1. won't know whether associate first or second branch of xs:choice, ambiguity. sure, look-ahead 1 level , resolve ambiguity, how far should have look-ahead? xsd recommendation says validating parser shouldn't have look-ahead @ in these circumstances. therefore, job validator parser easier (but job xsd author bit harder).


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -