Skip to main content

Search 412 pages — tools, formats, elements, namespaces, directory, comparisons, XPath, datatypes, glossary, parse errors, schema errors, use cases, guides, blog and product.

XML Schema (XSD)

<xs:choice>

A model group that selects one of its child particles each time the group occurs.

choice expresses alternatives, not unordered children. With its default occurrence range, exactly one branch is selected.

Set minOccurs=0 to make the whole decision optional, or raise maxOccurs when several branch selections may appear in sequence.

What it contains

Attributes

  • minOccursoptionalSet to zero when selecting no branch is valid.
  • maxOccursoptionalControls how many branch selections may occur.

Cardinality and constraints

Zero or one as a primary model group; it may be nested and repeated.

  • One branch is selected per occurrence.
  • It does not allow child elements in arbitrary order.

What our validator does

The XML validator checks well-formedness and selected SOAP or XML Schema vocabulary rules. It reports this content-model defect with a concrete repair while staying silent on rules it cannot establish from one document.

Broken and fixed

Both documents below are well-formed XML — that is the point of this corpus — and both are run through the validator on every test run. The verdicts here are what it returned, not what we expect it to.

broken
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:example:catalog">
  <xs:complexType name="Product"><xs:attribute name="active" type="xs:boolean" maxOccurs="2"/></xs:complexType>
</xs:schema>
xs:attribute cannot use minOccurs or maxOccurs; attributes use the use attribute instead.
fixed
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="urn:example:catalog" targetNamespace="urn:example:catalog" elementFormDefault="qualified">
  <xs:simpleType name="Sku"><xs:restriction base="xs:string"><xs:minLength value="1"/></xs:restriction></xs:simpleType>
  <xs:complexType name="Product"><xs:sequence><xs:element name="sku" type="tns:Sku"/></xs:sequence><xs:attribute name="active" type="xs:boolean"/></xs:complexType>
  <xs:element name="product" type="tns:Product"/>
</xs:schema>
No errors. Warnings may remain — a conforming feed can still be missing a self link — but nothing here is wrong.

Get started

Bring order to the XML your team can't afford to ignore.

Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.