XML schemas

An XML schema is a mechanism, defined by the W3C, for describing and constraining the structure and content of XML documents. An XML schema, which is itself expressed in XML, effectively defines a class of XML documents of a given type, for example, purchase orders.

For Enterprise COBOL, XML schemas used for validating XML documents must be in a preprocessed format known as Optimized Schema Representation (OSR). For information about this format, see the related reference about z/OS® XML System Services.

Consider an XML document that describes an item for stock-keeping purposes:


<stockItem itemNumber="453-SR">
  <itemName>Stainless steel rope thimbles</itemName>
  <quantityOnHand>23</quantityOnHand>
</stockItem> 

The example document above is both well formed and valid according to the following schema. (The numbers that precede each line are not part of the schema, but are used in the explanations after the schema.)


 1. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 2.  
 3. <xsd:element name="stockItem" type="stockItemType"/>
 4. 
 5. <xsd:complexType name="stockItemType"> 
 6.   <xsd:sequence> 
 7.     <xsd:element name="itemName" type="xsd:string" minOccurs="0"/> 
 8.     <xsd:element name="quantityOnHand"> 
 9.       <xsd:simpleType> 
10.         <xsd:restriction base="xsd:nonNegativeInteger"> 
11.           <xsd:maxExclusive value="100"/> 
12.         </xsd:restriction> 
13.       </xsd:simpleType> 
14.     </xsd:element> 
15.   </xsd:sequence>
16.   <xsd:attribute name="itemNumber" type="SKU" use="required"/> 
17. </xsd:complexType>
18.
19. <xsd:simpleType name="SKU"> 
20.   <xsd:restriction base="xsd:string"> 
21.     <xsd:pattern value="\d{3}-[A-Z]{2}"/> 
22.   </xsd:restriction> 
23. </xsd:simpleType> 
24.
25. </xsd:schema>
The schema declares (line 3) that the root element is stockItem, which has a mandatory itemNumber attribute (line 16) of type SKU, and includes a sequence (lines 6 - 15) of other elements:
  • An optional itemName element of type string (line 7)
  • A required quantityOnHand element that has a constrained range of 1 - 99 based on the type nonNegativeInteger (lines 8 - 14)

Type declarations can be inline and unnamed, as in lines 9 - 13, which include the maxExclusive facet to specify the legal values for the quantityOnHand element.

For the itemNumber attribute, by contrast, the named type SKU is declared separately in lines 19 - 23, which include a pattern facet that uses regular expression syntax to specify that the legal values for that type consist of (in order): 3 digits, a hyphen-minus, then two uppercase letters.

The example referenced below shows a program that parses documents against this schema.

Example: parsing XML documents with validation

related references  
z/OS XML System Services User's Guide and Reference