Example: parsing XML documents with validation

This example shows the parsing of several XML documents with validation against a schema, and a processing procedure that captures the return code and reason code that the parser generates after parsing each document. All of the XML documents are well formed but not necessarily valid.

The program must be compiled using the XMLPARSE(XMLSS) compiler option.

The example uses the schema that was described in the related concept about XML schemas.

Assume that file item.xsd contains the schema in text format, and that the preprocessed schema was generated in file item.osr by means of the following z/OS® UNIX command:


xsdosrg -v -o /u/HLQ/xml/item.osr /u/HLQ/xml/item.xsd

The example uses the XML-SCHEMA clause to associate the XML schema name schema with the ddname ddschema. The following DD statement associates the ddname with the external z/OS UNIX file that contains the schema:


//GO.DDSCHEMA DD PATH='/u/HLQ/xml/item.osr'

Program ValidCk


Identification division.
  Program-id. ValidCk.
Environment division.
 Configuration section.
  Special-names.
    xml-schema schema is 'ddschema'.
Data division.
 Working-storage section.
  1 xml-decode.
   2 rtn comp   Pic 9(2).
   2 rsn comp-5 Pic 9(4).
  1 hv pic x(16) value '0123456789ABCDEF'.
  1 T           Pic 999 COMP.
  1 xml-document-1.
   2 pic x(52) value
       '<!--Valid: the "itemName" element can be omitted-->'.
   2 pic x(31) value '<stockItem itemNumber="123-AB">'.
   2 pic x(36) value '  <quantityOnHand>1</quantityOnHand>'.
   2 pic x(12) value '</stockItem>'.
  1 xml-document-2.
   2 pic x(44)
     value '<!--Invalid: missing attribute itemNumber-->'.
   2 pic x(11) value '<stockItem>'.
   2 pic x(30) value '  <itemName>No name</itemName>'.
   2 pic x(36) value '  <quantityOnHand>1</quantityOnHand>'.
   2 pic x(12) value '</stockItem>'.
  1 xml-document-3.
   2 pic x(47)
     value '<!--Invalid: unexpected attribute warehouse-->'.
   2 pic x(46) value
       '<stockItem itemNumber="074-UN" warehouse="NJ">'.
   2 pic x(37) value '  <quantityOnHand>10</quantityOnHand>'.
   2 pic x(32) value '  <itemName>Not here!</itemName>'.
   2 pic x(12) value '</stockItem>'.
  1 xml-document-4.
   2 pic x(46)
     value '<!--Invalid: illegal attribute value 123-Ab-->'.
   2 pic x(31) value '<stockItem itemNumber="123-Ab">'.
   2 pic x(33) value '  <itemName>Paintbrush</itemName>'.
   2 pic x(37) value '  <quantityOnHand>10</quantityOnHand>'.
   2 pic x(12) value '</stockItem>'.
  1 xml-document-5.
   2 pic x(46)
     value '<!--Invalid: missing element quantityOnHand-->'.
   2 pic x(31) value '<stockItem itemNumber="074-UN">'.
   2 pic x(32) value '  <itemName>Not here!</itemName>'.
   2 pic x(12) value '</stockItem>'.
  1 xml-document-6.
   2 pic x(42)
     value '<!--Invalid: unexpected element comment-->'.
   2 pic x(31) value '<stockItem itemNumber="123-AB">'.
   2 pic x(33) value '  <itemName>Paintbrush</itemName>'.
   2 pic x(36) value '  <quantityOnHand>1</quantityOnHand>'.
   2 pic x(35) value '  <comment>Nylon bristles</comment>'.
   2 pic x(12) value '</stockItem>'.
  1 xml-document-7.
   2 pic x(46) value
       '<!--Invalid: out-of-range element value 100-->'.
   2 pic x(31) value '<stockItem itemNumber="123-AB">'.
   2 pic x(33) value '  <itemName>Paintbrush</itemName>'.
   2 pic x(38) value '  <quantityOnHand>100</quantityOnHand>'.
   2 pic x(12) value '</stockItem>'.
Procedure division.
  m.
    xml parse xml-document-1 validating with file schema
        processing procedure p
    xml parse xml-document-2 validating with file schema
        processing procedure p
    xml parse xml-document-3 validating with file schema
        processing procedure p
    xml parse xml-document-4 validating with file schema
        processing procedure p
    xml parse xml-document-5 validating with file schema
        processing procedure p
    xml parse xml-document-6 validating with file schema
        processing procedure p
    xml parse xml-document-7 validating with file schema
        processing procedure p
    goback
    .
  p.
    evaluate xml-event
      when 'COMMENT'
        display ' '
        display xml-text
      when 'END-OF-DOCUMENT'
        display ' Document successfully parsed.'
      when 'EXCEPTION'
        move xml-code to xml-decode
        Divide rsn by 16 giving tally remainder T
        display ' RC=' rtn ', reason=x'''
            hv(function mod(rsn / 4096 16) + 1:1)
            hv(function mod(rsn / 256 16) + 1:1)
            hv(function mod(rsn / 16 16) + 1:1)
            hv(T + 1:1) ''''
    end-evaluate
    .
End program ValidCk.

Output from program ValidCk

In the following output, you can see which XML documents in the source program failed validation against the schema.

For those documents that were not valid, the parser signaled an XML exception and passed control to the processing procedure with special register XML-EVENT containing 'EXCEPTION' and special-register XML-CODE containing the return code and a specific reason code.


Valid: the "itemName" element can be omitted
  Document successfully parsed.

Invalid: missing attribute itemNumber
  RC=24, reason=x'8613'

Invalid: unexpected attribute warehouse
  RC=24, reason=x'8612'

Invalid: illegal attribute value 123-Ab
  RC=24, reason=x'8809'

Invalid: missing element quantityOnHand
  RC=24, reason=x'8611'

Invalid: unexpected element comment
  RC=24, reason=x'8607'

Invalid: out-of-range element value 100
  RC=24, reason=x'8803'

Related concepts  
XML-CODE  
XML schemas