Example: parsing an XML document that uses namespaces

This example shows the parsing of a document that uses namespaces and namespace prefixes. The program must be compiled using the XMLPARSE(XMLSS) compiler option.

Namespace identifiers and namespace prefixes are used in the program to qualify element names and attribute names. This qualification makes it possible to use the same name in more than one context: title is used both as an author's title (Mr) and as a book title (Writing COBOL for Fun and Profit).

Sample XML document

The following XML document contains several namespace declarations: a default namespace; then three namespace identifiers with prefixes (bk, pi, and isbn). Notice that the default namespace is set to the empty string for the element comment (xmlns=''). This setting undeclares the default namespace, with the result that there is no default namespace.



<section
 xmlns="http://www.ibm.com/events"
 xmlns:bk="urn:loc.gov:books"
 xmlns:pi="urn:personalInformation"
 xmlns:isbn='urn:ISBN:0-395-36341-6'>
  <title>Book-Signing Event</title>
  <signing>
    <bk:author pi:title="Mr" pi:name="Jim Ross"/>
    <book bk:title="Writing COBOL for Fun and Profit" isbn:number="0426070806"/>
    <comment xmlns=''>What a great issue!</comment>
  </signing>
  </section>

Results from parsing

The following table shows the sequence of events that the processing procedure receives from the parser, and shows the content of the associated XML special registers.

Table 1. XML events and special registers
XML-EVENT XML-TEXT XML-NAMESPACE-PREFIX XML-NAMESPACE
START-OF-DOCUMENT      
START-OF-ELEMENT section   http://www.ibm.com/events
NAMESPACE-DECLARATION     http://www.ibm.com/events
NAMESPACE-DECLARATION   bk urn:loc.gov:books
NAMESPACE-DECLARATION   pi urn:personalInformation
NAMESPACE-DECLARATION   isbn urn:ISBN:0-395-36341-6
START-OF-ELEMENT title   http://www.ibm.com/events
CONTENT-CHARACTERS Book-Signing Event    
END-OF-ELEMENT title   http://www.ibm.com/events
START-OF-ELEMENT signing   http://www.ibm.com/events
START-OF-ELEMENT author bk urn:loc.gov:books
ATTRIBUTE-NAME title pi urn:personalInformation
ATTRIBUTE-CHARACTERS Mr    
ATTRIBUTE-NAME name pi urn:personalInformation
ATTRIBUTE-CHARACTERS Jim Ross    
END-OF-ELEMENT author bk urn:loc.gov:books
START-OF-ELEMENT book   http://www.ibm.com/events
ATTRIBUTE-NAME title bk urn:loc.gov:books
ATTRIBUTE-CHARACTERS
Writing COBOL for
Fun and Profit
   
ATTRIBUTE-NAME number isbn urn:ISBN:0-395-36341-6
ATTRIBUTE-CHARACTERS 0426070806    
END-OF-ELEMENT book   http://www.ibm.com/events
START-OF-ELEMENT comment    
NAMESPACE-DECLARATION      
CONTENT-CHARACTERS What a great issue!    
END-OF-ELEMENT comment    
END-OF-ELEMENT signing   http://www.ibm.com/events
END-OF-ELEMENT section   http://www.ibm.com/events
END-OF-DOCUMENT      

XML PARSE example with an undeclared namespace prefix

The following XML document contains undeclared namespace prefixes:

Identification division.
         Program-id. XMLup.
       Data division.
        Working-storage section.
         1 d.
          2 pic x(40) value '<pfx0:root xmlns:pfx1="http://whatever">'.
          2 pic x(19) value '<pfx1:localElName1>'.
          2 pic x(20) value '<pfx2:localElName2/>'.
          2 pic x(40) value '<pfx3:localElName3 pfx4:localAtName4="">'.
          2 pic x(02) value 'c1'.
          2 pic x(41) value '<pfx5:localElName5 pfx6:localAtName6=""/>'.
          2 pic x(24) value 'c2</pfx3:localElName3>c3'.
          2 pic x(32) value '</pfx1:localElName1></pfx0:root>'.
       Procedure division.
         main.
           display 'XML document:  ' d
           display ' '
           xml parse d processing procedure h
           goback.
         h.
           if xml-event = 'EXCEPTION'
             display ' '
           end-if
           display xml-event xml-code '|' xml-text '|'
               xml-namespace-prefix '|'
               xml-namespace '|'
           if xml-event = 'EXCEPTION' and xml-code = 264192 or 264193
             move 0 to xml-code
           end-if
           .
       End program XMLup.

Results from parsing XML document with an undeclared namespace prefix

The following table lists the sequence of events that the processing procedure receives from the parser, and shows the content of the associated XML special registers.

Table 2. XML events and special registers from parsing XML document with an undeclared namespace prefix
XML-EVENT XML-CODE XML-TEXT XML-NAMESPACE-PREFIX XML-NAMESPACE
START-OF-DOCUMENT 000000000      
EXCEPTION 000264193 pfx0:root    
START-OF-ELEMENT 000000000 root pfx0  
NAMESPACE-DECLARATION 000000000   pfx1 http://whatever
START-OF-ELEMENT 000000000 localElName1 pfx1 http://whatever
EXCEPTION 000264193 pfx2:localElName2    
START-OF-ELEMENT 000000000 localElName2 pfx2  
END-OF-ELEMENT 000000000 localElName2 pfx2  
EXCEPTION 000264193 pfx3:localElName3    
START-OF-ELEMENT 000000000 localElName3 pfx3  
EXCEPTION 000264192 pfx4:localAtName4    
ATTRIBUTE-NAME 000000000 localAtName4 pfx4  
ATTRIBUTE-CHARACTERS 000000000      
CONTENT-CHARACTERS 000000000 c1    
EXCEPTION 000264193 pfx5:localElName5    
START-OF-ELEMENT 000000000 localElName5 pfx5  
EXCEPTION 000264192 pfx6:localAtName6    
ATTRIBUTE-NAME 000000000 localAtName6 pfx6  
ATTRIBUTE-CHARACTERS 000000000      
END-OF-ELEMENT 000000000 localElName5 pfx5  
CONTENT-CHARACTERS 000000000 c2    
END-OF-ELEMENT 000000000 localElName3 pfx3  
CONTENT-CHARACTERS 000000000 c3    
END-OF-ELEMENT 000000000 localElName1 pfx1 http://whatever
END-OF-ELEMENT 000000000 root pfx0  
END-OF-DOCUMENT 000000000      

For a detailed description of the set of XML events, see the related reference about XML-EVENT.

related references
XMLPARSE (compiler option)   
XML-EVENT (Enterprise COBOL for z/OS® Language Reference)