XML GENERATE statement

The XML GENERATE statement converts data to XML format.

Format

Read syntax diagramSkip visual syntax diagramXML GENERATEidentifier-1FROM identifier-2COUNTINidentifier-3WITHENCODINGcodepageWITHXML-DECLARATIONWITHATTRIBUTESNAMESPACEISidentifier-4literal-4NAMESPACE-PREFIXISidentifier-5literal-5NAMEOFidentifier-6ISliteral-6TYPEOFidentifier-7ISATTRIBUTEELEMENTCONTENTSUPPRESSidentifier-8when-phrasegeneric-suppression-phraseONEXCEPTIONimperative-statement-1NOTONEXCEPTIONimperative-statement-2END-XML

when-phrase Format

Read syntax diagramSkip visual syntax diagramWHEN ZEROZEROESZEROSSPACESPACESLOW-VALUELOW-VALUESHIGH-VALUEHIGH-VALUES ORZEROZEROESZEROSSPACESPACESLOW-VALUELOW-VALUESHIGH-VALUEHIGH-VALUES

generic-suppression-phrase Format

Read syntax diagramSkip visual syntax diagram EVERYNUMERICATTRIBUTECONTENTELEMENTNONNUMERICATTRIBUTECONTENTELEMENTATTRIBUTECONTENTELEMENT when-phrase

identifier-1
The receiving area for a generated XML document. identifier-1 must reference one of the following items:
  • An elementary data item of category alphanumeric
  • An alphanumeric group item
  • An elementary data item of category national
  • A national group item

When identifier-1 references a national group item, identifier-1 is processed as an elementary data item of category national. When identifier-1 references an alphanumeric group item, identifier-1 is treated as though it were an elementary data item of category alphanumeric.

identifier-1 must not be described with the JUSTIFIED clause, and cannot be a function identifier. identifier-1 can be subscripted or reference modified.

identifier-1 must not overlap identifier-2, identifier-3, codepage (if an identifier), identifier-4, or identifier-5.

identifier-1 must not be a dynamic-length group item or a dynamic-length elementary item.

The generated XML output is encoded as described in the documentation of the ENCODING phrase.

identifier-1 must reference a data item of category national, or the ENCODING phrase must specify 1208, if any of the following statements are true:

  • The CODEPAGE compiler option specifies an EBCDIC DBCS code page.
  • identifier-4 or identifier-5 references a data item of category national.
  • literal-4, literal-5, or literal-6 is of category national.
  • The generated XML includes data from identifier-2 for:
    • Any data item of class national or class DBCS
    • Any data item with a DBCS name (that is, a data item whose name consists of DBCS characters)
    • Any data item of class alphanumeric that contains DBCS characters

identifier-1 must be large enough to contain the generated XML document. Typically, it must be from 5 to 10 times the size of identifier-2, depending on the length of the data-name or data-names within identifier-2. If identifier-1 is not large enough, an error condition exists at the end of the XML GENERATE statement.

identifier-2
The group or elementary data item to be converted to XML format.

If identifier-2 references a national group item, identifier-2 is processed as a group item. When identifier-2 includes a subordinate national group item, that subordinate item is processed as a group item.

identifier-2 cannot be a function identifier or be reference modified, but it can be subscripted.

identifier-2 must not overlap identifier-1 or identifier-3.

identifier-2 must not be a dynamic-length group item or a dynamic-length elementary item.

The data description entry for identifier-2 must not contain a RENAMES clause.

The following data items that are specified by identifier-2 are ignored by the XML GENERATE statement:

  • Any subordinate unnamed elementary data items or elementary FILLER data items
  • Any slack bytes inserted for SYNCHRONIZED items
  • Any data item subordinate to identifier-2 that is described with the REDEFINES clause or that is subordinate to such a redefining item
  • Any data item subordinate to identifier-2 that is described with the RENAMES clause
  • Any group data item all of whose subordinate data items are ignored

All data items specified by identifier-2 that are not ignored according to the previous rules must satisfy the following conditions:

  • Each elementary data item must either have class alphabetic, alphanumeric, numeric, or national, or be an index data item. (That is, no elementary data item can be described with the USAGE POINTER, USAGE FUNCTION-POINTER, USAGE PROCEDURE-POINTER, or USAGE OBJECT REFERENCE phrase.)
  • There must be at least one such elementary data item.
  • Each non-FILLER data-name must be unique within any immediately superordinate group data item.
  • Any DBCS data-names, when converted to Unicode, must be legal as names in the XML specification, version 1.0. For details about the XML specification, see XML specification.

For example, consider the following data declaration:


01 STRUCT.
  02 STAT PIC X(4).
  02 IN-AREA PIC X(100).
  02 OK-AREA REDEFINES IN-AREA.
    03 FLAGS PIC X.
    03 PIC X(3).
    03 COUNTER USAGE COMP-5 PIC S9(9).
    03 ASFNPTR REDEFINES COUNTER USAGE FUNCTION-POINTER.
    03 UNREFERENCED PIC X(92).
  02 NG-AREA1 REDEFINES IN-AREA.
    03 FLAGS PIC X.
    03 PIC X(3).
    03 PTR USAGE POINTER.
    03 ASNUM REDEFINES PTR USAGE COMP-5 PIC S9(9).
    03 PIC X(92).
  02 NG-AREA2 REDEFINES IN-AREA.
    03 FN-CODE PIC X.
    03 UNREFERENCED PIC X(3).
    03 QTYONHAND USAGE BINARY PIC 9(5).
    03 DESC USAGE NATIONAL PIC N(40).
    03 UNREFERENCED PIC X(12).

The following data items from the previous example can be specified as identifier-2:

  • STRUCT, of which subordinate data items STAT and IN-AREA would be converted to XML format. (OK-AREA, NG-AREA1, and NG-AREA2 are ignored because they specify the REDEFINES clause.)
  • OK-AREA, of which subordinate data items FLAGS, COUNTER, and UNREFERENCED would be converted. (The item whose data description entry specifies 03 PIC X(3) is ignored because it is an elementary FILLER data item. ASFNPTR is ignored because it specifies the REDEFINES clause.)
  • Any of the elementary data items that are subordinate to STRUCT except:
    • ASFNPTR or PTR (disallowed usage)
    • UNREFERENCED OF NG-AREA2 (nonunique names for data items that are otherwise eligible)
    • Any FILLER data items

The following data items cannot be specified as identifier-2:

  • NG-AREA1, because subordinate data item PTR specifies USAGE POINTER but does not specify the REDEFINES clause. (PTR would be ignored if it specified the REDEFINES clause.)
  • NG-AREA2, because subordinate elementary data items have the nonunique name UNREFERENCED.
COUNT IN phrase
If the COUNT IN phrase is specified, identifier-3 contains (after execution of the XML GENERATE statement) the count of generated XML character encoding units. If identifier-1 (the receiver) has category national, the count is in UTF-16 character encoding units. For all other encodings (including UTF-8), the count is in bytes.
identifier-3
The data count field. Must be an integer data item defined without the symbol P in its picture string.

identifier-3 must not overlap identifier-1, identifier-2, codepage (if an identifier), identifier-4, or identifier-5.

ENCODING phrase
The ENCODING phrase, if specified, determines the encoding of the generated XML document.
codepage
Must be an unsigned integer data item or unsigned integer literal and must represent a valid coded character set identifier (CCSID). Must identify one of the code pages supported for COBOL XML processing as described in The encoding of XML documents in the Enterprise COBOL Programming Guide.

If identifier-1 references a data item of category national, codepage must specify 1200, the CCSID for Unicode UTF-16.

If identifier-1 references a data item of category alphanumeric, codepage must specify 1208 or the CCSID of a supported EBCDIC code page as listed in The encoding of XML documents in the Enterprise COBOL Programming Guide.

If codepage is an identifier, it must not overlap identifier-1 or identifier-3.

If the ENCODING phrase is omitted and identifier-1 is of category national, the document encoding is Unicode UTF-16, CCSID 1200.

If the ENCODING phrase is omitted and identifier-1 is of category alphanumeric, the XML document is encoded using the code page specified by the CODEPAGE compiler option in effect when the source code was compiled.

If the ENCODING phrase is omitted and identifier-1 is of category alphanumeric, the XML document is encoded using the code page specified by the EBCDIC_CODEPAGE environment variable in effect when the source code was compiled.

XML-DECLARATION phrase
If the XML-DECLARATION phrase is specified, the generated XML document starts with an XML declaration that includes the XML version information and an encoding declaration.

If identifier-1 is of category national, the encoding declaration has the value UTF-16 (encoding="UTF-16").

If identifier-1 is of category alphanumeric, the encoding declaration is derived from the ENCODING phrase, if specified, or from the CODEPAGE compiler option in effect for the program if the ENCODING phrase is not specified. See the description of the ENCODING phrase for further details.

For an example of the effect of coding the XML-DECLARATION phrase, see Generating XML output in the Enterprise COBOL Programming Guide.

If the XML-DECLARATION phrase is omitted, the generated XML document does not include an XML declaration.

ATTRIBUTES phrase
If the ATTRIBUTES phrase is specified, each eligible item included in the generated XML document is expressed as an attribute of the XML element that corresponds to the data item immediately superordinate to that eligible item, rather than as a child element of the XML element. To be eligible, a data item must be elementary, must have a name other than FILLER, and must not specify an OCCURS clause in its data description entry.

If the TYPE phrase is specified for particular identifiers, the TYPE phrase takes precedence for those identifiers over the WITH ATTRIBUTES phrase.

For an example of the effect of the ATTRIBUTES phrase, see Generating XML output in the Enterprise COBOL Programming Guide.

NAMESPACE and NAMESPACE-PREFIX phrases
Use the NAMESPACE phrase to identify a namespace for the generated XML document. If the NAMESPACE phrase is not specified, or if identifier-4 has length zero or contains all spaces, the element names of XML documents produced by the XML GENERATE statement are not in any namespace.

Use the NAMESPACE-PREFIX phrase to qualify the start and end tag of each element in the generated XML document with a prefix.

If the NAMESPACE-PREFIX phrase is not specified, or if identifier-5 is of length zero or contains all spaces, the namespace specified by the NAMESPACE phrase specifies the default namespace for the document. In this case, the namespace declared on the root element applies by default to each element name in the document, including that of the root element. (Default namespace declarations do not apply directly to attribute names.)

If the NAMESPACE-PREFIX phrase is specified, and identifier-5 is not of length zero and does not contain all spaces, then the start and end tag of each element in the generated document is qualified with the specified prefix. The prefix should therefore preferably be short. When the XML GENERATE statement is executed, the prefix must be a valid XML name, but without the colon (:), as defined in Namespaces in XML 1.0. The prefix can have trailing spaces, which are removed before use.

identifier-4, literal-4; identifier-5, literal-5
identifier-4, literal-4: The namespace identifier, which must be a valid Uniform Resource Identifier (URI) as defined in Uniform Resource Identifier (URI): Generic Syntax.

identifier-5, literal-5: The namespace prefix, which serves as an alias for the namespace identifier.

identifier-4 and identifier-5 must reference data items of category alphanumeric or national.

identifier-4 and identifier-5 must not overlap identifier-1 or identifier-3.

literal-4 and literal-5 must be of category alphanumeric or national, and must not be figurative constants.

For full details about namespaces, see Namespaces in XML 1.0.

For examples that show the use of the NAMESPACE and NAMESPACE-PREFIX phrases, see Generating XML output in the Enterprise COBOL Programming Guide.

NAME phrase
Allows you to supply element and attribute names.
identifier-6 must reference identifier-2 or one of its subordinate data items. It cannot be a function identifier and cannot be reference modified or subscripted. It must not specify any data item which is ignored by the XML GENERATE statement. For more information about identifier-2, see the description of identifier-2. If identifier-6 is specified more than once in the NAME phrase, the last specification is used.
literal-6 must be an alphanumeric or national literal containing the attribute or element name to be generated in the XML document corresponding to identifier-6. It must be a valid XML local name. If literal-6 is a national literal, identifier-1 must reference a data item of category national or the encoding phrase must specify 1208.
TYPE phrase
Allows you to control attribute and element generation.
identifier-7 must reference an elementary data item that is subordinate to identifier-2. It cannot be a function identifier and cannot be reference modified or subscripted. It must not specify any data item which is ignored by the XML GENERATE statement. For more information about identifier-2, see the description of identifier-2. If identifier-7 is specified more than once in the TYPE phrase, the last specification is used.
  • If the XML GENERATE statement also includes a WITH ATTRIBUTES phrase, the TYPE phrase has precedence for identifier-7.
  • When ATTRIBUTE is specified, identifier-7 must be eligible to be an XML attribute. identifier-7 is expressed in the generated XML as an attribute of the XML element immediately superordinate to identifier-7 rather than as a child element.
  • When ELEMENT is specified, identifier-7 is expressed in the generated XML as an element. The XML element name is derived from identifier-7 and the element character content is derived from the converted content of identifier-7 as described in Operation of XML GENERATE.
  • When CONTENT is specified, identifier-7 is expressed in the generated XML as element character content of the XML element that corresponds to the data item immediately superordinate to identifier-7. The value of the element character content is derived from the converted content of identifier-7 as described in Operation of XML GENERATE. When CONTENT is specified for multiple identifiers all corresponding to the same superordinate identifier, the multiple contributions to the element character content are concatenated.
SUPPRESS phrase
Allows you to identify and unconditionally suppress items that are subordinate to identifier-2 and selectively generate output for the XML GENERATE statement. If the SUPPRESS phrase is specified, identifier-1 must be large enough to contain the generated XML document before any suppression.
With the generic-suppression-phrase, elementary items subordinate to identifier-2 that are not otherwise ignored by XML GENERATE operations are identified generically for potential suppression. Either items of class numeric, if the NUMERIC keyword is specified, or items that are not of class numeric, if the NONNUMERIC keyword is specified, or both, might be suppressed. If the ATTRIBUTE keyword is specified, only items that would be expressed in the generated XML document as an XML attribute are identified for potential suppression. If the ELEMENT keyword is specified, only items that would be expressed in the generated XML document as an XML element are identified for potential suppression. If the CONTENT keyword is specified, only items that would be expressed in the generated XML document as element character content of the XML element corresponding to the data item superordinate to the CONTENT data item are identified for potential suppression.
If multiple generic-suppression-phrase are specified, the effect is cumulative.
identifier-8 explicitly identifies items for potential suppression. If the WHEN phrase is specified, identifier-8 must reference an elementary data item that is subordinate to identifier-2 and that is not otherwise ignored by the XML GENERATE operations. identifier-8 cannot be a function identifier and cannot be reference modified or subscripted.If the WHEN phrase is omitted, identifier-8 can reference not only an elementary data item but also a group data item. That group data item and all data items that are subordinate to the group item are suppressed. If identifier-8 is specified more than once in the SUPPRESS phrase, the last specification is used. The explicit suppression specification for identifier-8 overrides the suppression specification that is implied by any generic-suppression-phrase, if identifier-8 is also one of the identifiers generically identified.
If identifier-8 is specified, the following rules apply to it:
  • If ZERO, ZEROES, or ZEROS is specified in the WHEN phrase, identifier-8 must not be of USAGE DISPLAY-1.
  • If SPACE or SPACES is specified in the WHEN phrase, identifier-8 must be of USAGE DISPLAY, DISPLAY-1, or NATIONAL. If identifier-8 is a zoned or national decimal item, it must be an integer.
  • If LOW-VALUE, LOW-VALUES, HIGH-VALUE, or HIGH-VALUES is specified in the WHEN phrase, identifier-8 must be of USAGE DISPLAY or NATIONAL. If identifier-8 is a zoned or national decimal item, it must be an integer.
If the generic-suppression-phrase is specified, data items are selected for potential suppression according to the following rules:
  • If ZERO, ZEROES, or ZEROS is specified in the WHEN phrase, all data items except those that are defined with USAGE DISPLAY-1 are selected.
  • If SPACE or SPACES is specified in the WHEN phrase, data items of USAGE DISPLAY, DISPLAY-1, or NATIONAL are selected. For zoned or national decimal items, only integers are selected.
  • If LOW-VALUE, LOW-VALUES, HIGH-VALUE, or HIGH-VALUES is specified in the WHEN phrase, data items of USAGE DISPLAY or NATIONAL are selected. For zoned or national decimal items, only integers are selected.
The comparison operation that determines whether an item will be suppressed is a relation condition as shown in the table of Comparisons involving figurative constants. That is, the comparison is a numeric comparison if the value specified is ZERO, ZEROS, or ZEROES, and the item is of class numeric. For all other cases, the comparison operation is an alphanumeric, DBCS, or national comparison, depending on whether the item is of usage DISPLAY, DISPLAY-1 or NATIONAL, respectively.
When the SUPPRESS phrase is specified, a group item subordinate to identifier-2 is suppressed in the generated XML document if all the eligible items subordinate to the group item are suppressed or if, after suppressing any subordinate items, the XML corresponding to the group item would be an empty element with no attributes. The root element is always generated, even if all the items subordinate to identifier-2 are suppressed.
ON EXCEPTION phrase
An exception condition exists when an error occurs during generation of the XML document, for example if identifier-1 is not large enough to contain the generated XML document. In this case, XML generation stops and the content of the receiver, identifier-1, is undefined. If the COUNT IN phrase is specified, identifier-3 contains the number of character positions that were generated, which can range from 0 to the length of identifier-1.

If the ON EXCEPTION phrase is specified, control is transferred to imperative-statement-1. If the ON EXCEPTION phrase is not specified, the NOT ON EXCEPTION phrase, if any, is ignored, and control is transferred to the end of the XML GENERATE statement. Special register XML-CODE contains an exception code, as detailed in Handling XML GENERATE exceptions in the Enterprise COBOL Programming Guide.

NOT ON EXCEPTION phrase
If an exception condition does not occur during generation of the XML document, control is passed to imperative-statement-2, if specified, otherwise to the end of the XML GENERATE statement. The ON EXCEPTION phrase, if specified, is ignored. Special register XML-CODE contains zero after execution of the XML GENERATE statement.
END-XML phrase
This explicit scope terminator delimits the scope of XML GENERATE or XML PARSE statements. END-XML permits a conditional XML GENERATE or XML PARSE statement (that is, an XML GENERATE or XML PARSE statement that specifies the ON EXCEPTION or NOT ON EXCEPTION phrase) to be nested in another conditional statement.

The scope of a conditional XML GENERATE or XML PARSE statement can be terminated by:

  • An END-XML phrase at the same level of nesting
  • A separator period

END-XML can also be used with an XML GENERATE or XML PARSE statement that does not specify either the ON EXCEPTION or the NOT ON EXCEPTION phrase.

For more information about explicit scope terminators, see Delimited scope statements.