Problem:
The documentation for COBOL XML Syntax show that you can parse XML where the actual element names are unknown. How can you extend this through more levels of XML hierarchy ?
Resolution:
You need to build more levels on depth into the XD definitions. An example of extension to a lower depth based on the documented example:-
$set sourceformat"variable" preprocess(prexml) o(foo.pp) warn endp
select doc assign to "MYXML.XML" *>address of mybuf
organization is xml
document-type is external doc-type
file status is doc-status.
xd doc.
01 root-tag identified by root-tag-name.
10 root-tag-name pic x(80).
10 root-tag-val pic x(80).
10 root-tag-attr identified by root-tag-attr-name
is attribute.
15 root-tag-attr-name pic x(80).
15 root-tag-attr-val pic x(80).
10 sub-tag1 identified by sub-tag1-name.
15 sub-tag1-name pic x(80).
15 sub-tag1-val pic x(80).
15 sub-tag1-attr identified by sub-tag1-attr-name
is attribute.
20 sub-tag1-attr-val pic x(80).
20 sub-tag1-attr-name pic x(80).
15 sub-tag2 identified by sub-tag2-name.
20 sub-tag2-name pic x(80).
20 sub-tag2-val pic x(80).
20 sub-tag2-attr identified by sub-tag2-attr-name
is attribute.
25 sub-tag2-attr-val pic x(80).
25 sub-tag2-attr-name pic x(80).
20 sub-tag3 identified by sub-tag3-name.
25 sub-tag3-name pic x(80).
25 sub-tag3-val pic x(80).
25 sub-tag3-attr identified by sub-tag3-attr-name
is attribute.
30 sub-tag3-attr-val pic x(80).
30 sub-tag3-attr-name pic x(80).
25 sub-tag4 identified by sub-tag4-name.
30 sub-tag4-name pic x(80).
30 sub-tag4-val pic x(80).
30 sub-tag4-attr identified by sub-tag4-attr-name
is attribute.
35 sub-tag4-attr-val pic x(80).
35 sub-tag4-attr-name pic x(80).
30 sub-tag5 identified by sub-tag5-name.
35 sub-tag5-name pic x(80).
35 sub-tag5-val pic x(80).
35 sub-tag5-attr identified by sub-tag5-attr-name
is attribute.
40 sub-tag5-attr-val pic x(80).
40 sub-tag5-attr-name pic x(80).
working-storage section.
01 doc-type pic x(80).
01 doc-status pic s9(9) comp.
01 mybuf.
10 pic x(300) value
'<library location="here" '
& 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
& 'xsi:schemaLocation="library.xsd">'
& '<book published="yes" goodreading="ok">'
& 'booktext'
& '</book>'
& 'is a place'
& '</library>'.
procedure division.
open input doc
read doc
display "Document type is: " doc-type
display "Tag name: " root-tag-name
display "Tag value:" root-tag-val
start doc key root-tag-attr
*>
*> Loop through all attributes dumping names
*>
perform until exit
read doc next key root-tag-attr
if doc-status not = 0
exit perform
end-if
display "Attribute name : " root-tag-attr-name
display "Attribute value: " root-tag-attr-val
end-perform
*> Loop through all sub-tag1s
start doc key sub-tag1
perform until exit
read doc
next key sub-tag1 *> index is 1 is default
if doc-status not = 0
exit perform
end-if
display " Sub tag name: " sub-tag1-name
display " Sub tag value:" sub-tag1-val
perform parse-sub-tag2
start doc key sub-tag1-attr index is 1
perform until exit
read doc next key sub-tag1-attr
if doc-status not = 0
exit perform
end-if
display " Sub tag attribute name : "
sub-tag1-attr-name
display " Sub tag attribute value: "
sub-tag1-attr-val
end-perform
end-perform
close doc
stop run.
parse-sub-tag2.
*> Loop through all sub-tag1s
start doc key sub-tag2
perform until exit
read doc
next key sub-tag2 *> index is 1 is default
if doc-status not = 0
exit perform
end-if
display " Sub tag name: " sub-tag2-name
display " Sub tag value:" sub-tag2-val
perform parse-sub-tag3
end-perform
.
parse-sub-tag3.
*> Loop through all sub-tag1s
start doc key sub-tag3
perform until exit
read doc
next key sub-tag3 *> index is 1 is default
if doc-status not = 0
exit perform
end-if
display " Sub tag name: " sub-tag3-name
display " Sub tag value:" sub-tag3-val
perform parse-sub-tag4
end-perform
.
parse-sub-tag4.
*> Loop through all sub-tag1s
start doc key sub-tag4
perform until exit
read doc
next key sub-tag4 *> index is 1 is default
if doc-status not = 0
exit perform
end-if
display " Sub tag name: " sub-tag4-name
display " Sub tag value:" sub-tag4-val
perform parse-sub-tag5
end-perform
.
parse-sub-tag5.
*> Loop through all sub-tag1s
start doc key sub-tag5
perform until exit
read doc
next key sub-tag5 *> index is 1 is default
if doc-status not = 0
exit perform
end-if
display " Sub tag name: " sub-tag5-name
display " Sub tag value:" sub-tag5-val
end-perform
.
This goes down to a depth of 5 but the technique can be extended as required.




