2

I would like to create a XSD like so:

<xs:schema>
  <xs:element name="a">
    <xs:complexType>
      <xs:sequence>
        <xs:any minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="b" minOccurs="1" maxOccurs="unbounded"/>
        <xs:any minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Explaination:

Element a required, inside the element:

  • Any element 0 to infinity times.
  • Element b 1 to infinity times.
  • Any element 0 to infinity times.

I tried to use choice, all and etc, but I did not find any solution.

Error from XmlSpy:

<any...> makes the content model non-deterministic against <xs:element name="b" ...>. Possible causes: name equality, overlapping occurrence or substitution groups.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Nir Schwartz
  • 1,723
  • 2
  • 15
  • 27
  • What error are you currently getting? And where is the `name` attribute for your outer `` tag? – Tim Biegeleisen Aug 19 '15 at 08:30
  • Can you post the code for where you use this XSD definition? – Tim Biegeleisen Aug 19 '15 at 08:34
  • Sry, i added the name now. Error from XmlSpy: " makes the content model non-deterministic against . Possible causes: name equality, overlapping occurrence or substitution groups." – Nir Schwartz Aug 19 '15 at 08:39
  • 1
    The `` tags you have in there are causing a potential ambiguity which could occur when an XML file gets validated against your schema. Read [this SO article](http://stackoverflow.com/questions/386377/ambiguous-xml-schema) for more information. I don't have a quick solution to your problem at the moment. – Tim Biegeleisen Aug 19 '15 at 08:50
  • Ok, thank you, i will read the article. If any one else have an answer please comment. – Nir Schwartz Aug 19 '15 at 08:53

1 Answers1

2

Your XSD effectively is attempting to say that a can have any sequence of children elements as long as there is at least one b element.

XSD 1.0 cannot enforce such a constraint because any attempt to do so, including the one you offer, would violate the Unique Particle Principle.

XSD 1.1 could enforce such a constraint via a simple assertion stating that b exists among the xsd:any children of a.

XSD 1.1

<?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"
  vc:minVersion="1.1">
  <xs:element name="a">
    <xs:complexType>
      <xs:sequence>
        <xs:any processContents="skip" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:assert test="b"/>
    </xs:complexType>
   </xs:element>
</xs:schema>
kjhughes
  • 106,133
  • 27
  • 181
  • 240