Groups

There are three kinds of groups we can define in an XML Schema: <xs:sequence> groups, <xs:choice> groups, and <xs:all> groups. Normally these groups are inside the declaration of a type or element, but we can also use the <xs:group> element to create a group separately and refer to it as we need it.

The most flexible group is a choice group. A choice group contains a list of elements, only one of which may appear in a valid XML document. Although that sounds restrictive, we can say that a choice group can appear zero or more times, and we can put it in a mixed content model. For example, here’s a choice group that says a <p> element can contain text and any combination of <a>, <b>, <br>, <code>, <i>, or <img> elements:

<?xml version="1.0" encoding="UTF-8"?>
<!-- paragraph.xsd -->
<xs:schema 
  xmlns="http://www.oreilly.com/xslt"
  targetNamespace="http://www.oreilly.com/xslt"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="a">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:string">
          <xs:attribute name="href" type="xs:string"/>
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>

  <xs:element name="b">
    <xs:complexType mixed="true">
      <xs:choice minOccurs="0” maxOccurs="unbounded">
        <xs:element ref="a"/>
        <xs:element ref="br"/>
        <xs:element ref="code"/>
        <xs:element ref="i"/>
      </xs:choice>
    </xs:complexType>
  </xs:element>

  <xs:element name="br"/>

  <xs:element name="code">
    <xs:complexType mixed="true">
 <xs:choice minOccurs="0” maxOccurs="unbounded"> ...

Get XSLT, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.