Search code examples
cobol

Adjust the generated name with XML GENERATE Function


If I have the COBOL structure like this:

(the structure should be like this because I have to convert it using XML Generate Function, the variable name just sample)

01. MyData.
02. Corp-Entity
    03. Corp-Branch1.
        04. Address.
            05. AddressLine1 PIC X(20).
            05. AddressLine2 PIC X(20).
            05. PostalCode   PIC 9(05).
        04. PIC-Entity.
            05. Address.
                06. AddressLine1 PIC X(20).
                06. AddressLine2 PIC X(20).
                06. PostalCode   PIC 9(05).

How to move a value in 05. AddressLine1?

If I write this code:

move valueAddressLine1 to AddressLine1 
                       in Address
                       in Corp-Brach1
                       in MyData

But this code is valid for 06. AddressLine1

move valueAddressLine1 to AddressLine1 
                       in Address
                       in PIC-Entity

Error Message: "AddressLine1 of Address of PIC-Entity" was not a uniquely defined name.
The definition to be used could not be determined from the context. The reference to the name was discarded.


Solution

  • Pre-notes:

    • That code won't work because of the periods after the level numbers, get rid of these.
    • To have a unique name you don't need the full qualification. For example the following is fine:
                move valueAddressLine1 to AddressLine1 
                                       in PIC-Entity
    

    Answer:

    In cases like this: "fix" the COBOL side by using a name that can be accessed uniquely, in most cases by its "parent" items and where this isn't possible like your 04 Address by using a different name (for example 04 corp-address) .

    As you want this structure in XML you may use the NAME phrase as IBM's docs XML element name and attribute name formation says:

    In the XML documents that are generated from identifier-2, the XML element names and attribute names are obtained from the NAME phrase if specified; otherwise they are derived from the names of the data item specified by identifier-2 and from any eligible data-names that are subordinate to identifier-2.

    IBM's docs on XML GENERATE statement have details on this. For the given sample and assumed 2 corp-branches:

               XML GENERATE variable-name
                   FROM Corp-Entity
                   NAME OF corp-address in corp-branch1 IS 'Address'
                           corp-address in corp-branch2 IS 'Address'