Creating List Types

Another datatype we can create is a list type. A list type references a datatype; the new list type allows space-separated values of that type. We’ll add a new datatype called state-abbr to our schema, then create a list type based on it. Here’s the schema:

<?xml version="1.0" encoding="UTF-8"?>
<!-- list.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="person">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="name"/>
        <xs:element ref="birthday"/>
        <xs:element ref="age"/> 
        <xs:element ref="priorAddresses"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:simpleType name="state-abbr-type">
    <xs:restriction base="xs:string">
      <xs:length value="2"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="state-abbr-list-type">
    <xs:list itemType="state-abbr-type"/>
  </xs:simpleType>

  <xs:element name="name" type="xs:string"/>
  <xs:element name="birthday" type="xs:date"/>
  <xs:element name="age" type="xs:positiveInteger"/> 
  <xs:element name="priorAddresses">
    <xs:complexType>
      <xs:attribute name="states" type="state-abbr-list-type"/>
    </xs:complexType>
  </xs:element>

</xs:schema>

Now we have an attribute named states whose content is a list of one or more state abbreviations. Here’s a valid document:

<?xml version="1.0" encoding="utf-8"?>
<!-- list.xml --> <person xmlns="http://www.oreilly.com/xslt" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.oreilly.com/xslt ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.