XML のスキーマまたはドキュメント内で定義されている XML の要素 (タグ) や属性にレコードをマップする COBOL データ レコードの宣言に使用します。
COBOL データ レコードと XML タグを IDENTIFIED BY で対応付け、さらに IS ATTRIBUTE を追加して XML タグの属性を対応付けます。次に例を示します。
次の内容を持つ XML ドキュメントを想定します。
<company_name type="accounting">Webber Lynch</company_name>
この場合には、対応する COBOL レコードは、IDENTIFIED BY 句と IS ATTRIBUTE 句を使用して次のように記述できます。
05 company identified by "company_name". 10 company-name-value pic X(30). 10 company-type pic x(10) identified by "type" is attribute.
XML 構文拡張を使用するプログラムでは、既知のタグ名のみでなく、タグ名と属性名の変数も操作できます。COBOL データ名をタグ名や属性名で置き換えることによって、タグや属性を指定します。その結果、出力用に開いた XML ファイルに、XML ドキュメントを動的、かつ任意の複雑さの度合いで出力できます。また、入力用に開いたファイルから、そのような複雑なドキュメントを読み取ることができます。たとえば、次に示すプログラムはルートタグと単一階層のネストタグを含む XML ストリームを、ネストタグの数や、タグあたりの属性数に関係なく読み取ります。
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.
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.
XML 入力のソースとしてファイル名とバッファー (mybuf) を割り当て、ファイル編成 (XML)、XML スキーマ名を指定する変数のデータ名、およびファイル状態を示すデータ名を指定しています。
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).
バッファー領域のレコード構造を設定しています。
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>'.
静的バッファーに格納する XML ストリームを指定しています。このプログラムでは XML ストリームをコード内で指定していますが、標準入力を使用して XML をバッファー (mybuf) に格納することもできます。
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
バッファー領域を開いて XML レコードを読み取り、ドキュメントタイプ、ルートタグ名、およびルートタグの値を表示します。
0450 行目:
バッファー内で、ルートタグの最初の属性が開始する位置を設定します。
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
ルートタグ内の属性情報をループして読み取り、属性の名前と値を順次表示します。
0600 行目:
バッファー内で、最初のネストタグ (ルート以外のタグ) の開始位置を設定します。
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
ルート以外のすべてのタグにわたってループし、XML タグの名前と値、および属性の名前と値を順次表示します。