Use this syntax when declaring a COBOL data record to map the record to an XML element (tag) or an XML attribute that is defined in an XML schema or document.
You match up an XML tag with a COBOL data record using IDENTIFIED BY, and match up attributes for the XML tags with the addition of IS ATTRIBUTE. For example:
If you had an XML document that contained the following:
<company_name type="accounting">Webber Lynch</company_name>
a matching COBOL record using the IDENTIFIED BY and IS ATTRIBUTE clauses would be as follows:
05 company identified by "company_name". 10 company-name-value pic X(30). 10 company-type pic x(10) identified by "type" is attribute.
You can work with variable tag and attribute names in your XML-enabled program, as well as known tag names. Using this method, you specify tags and attributes by substituting a COBOL data-name for a tag or an attribute name. On an XML file opened for output, this allows you to write purely dynamic and arbitrarily complex XML documents. On a file opened for input this allows you to read such complex documents. For example, the following program reads any XML stream that has a single root tag, any number of single nested tags and any number of attributes per tag:
0010 $set preprocess(prexml) o(foo.pp) warn endp 0030 0040 select doc assign address of mybuf 0050 organization is xml 0060 document-type is external doc-type 0070 file status is doc-status. 0080 xd doc. 0090 01 root-tag identified by root-tag-name. 0100 10 root-tag-name pic x(80). 0110 10 root-tag-val pic x(80). 0120 10 root-tag-attr identified by root-tag-attr-name 0130 is attribute. 0140 15 root-tag-attr-name pic x(80). 0150 15 root-tag-attr-val pic x(80). 0160 0170 10 sub-tag identified by sub-tag-name. 0180 15 sub-tag-name pic x(80). 0190 15 sub-tag-val pic x(80). 0200 15 sub-tag-attr identified by sub-tag-attr-name 0210 is attribute. 0220 20 sub-tag-attr-val pic x(80). 0230 20 sub-tag-attr-name pic x(80). 0240 0250 working-storage section. 0260 01 doc-type pic x(80). 0270 01 doc-status pic s9(9) comp. 0280 01 mybuf. 0290 10 pic x(300) value 0300 '<library location="here" ' 0310 & 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' 0320 & 'xsi:schemaLocation="library.xsd">' 0330 & '<book published="yes" goodreading="ok">' 0340 & 'booktext' 0350 & '</book>' 0360 & 'is a place' 0370 & '</library>'. 0380 0390 procedure division. 0400 open input doc 0410 read doc 0420 display "Document type is: " doc-type 0430 display "Tag name: " root-tag-name 0440 display "Tag value:" root-tag-val 0450 start doc key root-tag-attr 0460 *> 0470 *> Loop through all attributes dumping names 0480 *> 0490 0500 perform until exit 0510 read doc next key root-tag-attr 0520 if doc-status not = 0 0530 exit perform 0540 end-if 0550 display "Attribute name : " root-tag-attr-name 0560 display "Attribute value: " root-tag-attr-val 0570 end-perform 0580 0590 *> Loop through all sub-tags 0600 start doc key sub-tag 0610 perform until exit 0620 read doc 0630 next key sub-tag *> index is 1 is default 0640 if doc-status not = 0 0650 exit perform 0660 end-if 0670 display " Sub tag name: " sub-tag-name 0680 display " Sub tag value:" sub-tag-val 0690 0700 start doc key sub-tag-attr index is 1 0710 perform until exit 0720 read doc next key sub-tag-attr 0730 if doc-status not = 0 0740 exit perform 0750 end-if 0760 display " Sub tag attribute name : " 0770 sub-tag-attr-name 0780 display " Sub tag attribute value: " 0790 sub-tag-attr-val 0800 end-perform 0810 end-perform 0820 0830 close doc 0840 0850 stop run.
Lines 0040 - 0070:
0040 select doc assign address of mybuf 0050 organization is xml 0060 document-type is external doc-type 0070 file status is doc-status.
Assigns a filename and the buffer mybuf as the source of the XML input, specifies the organization of the file (XML), provides a data-name that variably specifies the XML schema name, and provides a data-name for the file status.
Lines 0080 - 0230:
0080 xd doc. 0090 01 root-tag identified by root-tag-name. 0100 10 root-tag-name pic x(80). 0110 10 root-tag-val pic x(80). 0120 10 root-tag-attr identified by root-tag-attr-name 0130 is attribute. 0140 15 root-tag-attr-name pic x(80). 0150 15 root-tag-attr-val pic x(80). 0160 0170 10 sub-tag identified by sub-tag-name. 0180 15 sub-tag-name pic x(80). 0190 15 sub-tag-val pic x(80). 0200 15 sub-tag-attr identified by sub-tag-attr-name 0210 is attribute. 0220 20 sub-tag-attr-val pic x(80). 0230 20 sub-tag-attr-name pic x(80).
Sets up the record structure of the buffer area.
Lines 0280 - 0370:
0280 01 mybuf. 0290 10 pic x(300) value 0300 '<library location="here" ' 0310 & 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' 0320 & 'xsi:schemaLocation="library.xsd">' 0330 & '<book published="yes" goodreading="ok">' 0340 & 'booktext' 0350 & '</book>' 0360 & 'is a place' 0370 & '</library>'.
Codes the XML stream to be read into a static buffer. While this example has the XML stream coded directly into the program, you could also place the XML into the buffer (mybuf) using any standard input.
Lines 0400 - 0440:
0400 open input doc 0410 read doc 0420 display "Document type is: " doc-type 0430 display "Tag name: " root-tag-name 0440 display "Tag value:" root-tag-val
Opens the buffer area, reads in the XML record, and displays the document type, the name of the root tag, and the value of the root tag.
Line 0450:
Sets the starting point in the buffer to the first attribute for the root tag.
Lines 0500 - 0570:
0500 perform until exit 0510 read doc next key root-tag-attr 0520 if doc-status not = 0 0530 exit perform 0540 end-if 0550 display "Attribute name : " root-tag-attr-name 0560 display "Attribute value: " root-tag-attr-val 0570 end-perform
Loops through information in the root tag, reading each attribute for the root tag and displaying the attribute names and values.
Line 0600:
Sets the starting point in the buffer to the first nested tag (non-root tag).
Lines 0610 - 0810:
0610 perform until exit 0620 read doc 0630 next key sub-tag *> index is 1 is default 0640 if doc-status not = 0 0650 exit perform 0660 end-if 0670 display " Sub tag name: " sub-tag-name 0680 display " Sub tag value:" sub-tag-val 0690 0700 start doc key sub-tag-attr index is 1 0710 perform until exit 0720 read doc next key sub-tag-attr 0730 if doc-status not = 0 0740 exit perform 0750 end-if 0760 display " Sub tag attribute name : " 0770 sub-tag-attr-name 0780 display " Sub tag attribute value: " 0790 sub-tag-attr-val 0800 end-perform 0810 end-perform
Loops through all non-root tags, displaying each XML tag name and value, and all its attribute names and values.