3

First, let me apologize if I use wrong vocabulary, as I'm not too familiar with COBOL. We are trying to generate xml documents from a fixed data structure in COBOL. This is working well, using the GENERATE statement, but we have an issue.

If the data is the following:

First;Second;Age;Optional
JOHN;SNOW;18;Something
JOHN;DOE;45;Nothing
JOHN;ISSUE;30;

Then the generated xml looks like this:

<Persons>
   <Person First="JOHN" Second="SNOW" Age="18" Optional="Something"/>
   <Person First="JOHN" Second="DOE" Age="45" Optional="Nothing"/>
   <Person First="JOHN" Second="ISSUE" Age="18" Optional=""/>
</Persons>

When we would like the generator not to create the attributes when they have an empty value:

<Persons>
   <Person First="JOHN" Second="SNOW" Age="18" Optional="Something"/>
   <Person First="JOHN" Second="DOE" Age="45" Optional="Nothing"/>
   <Person First="JOHN" Second="ISSUE" Age="18" />
</Persons>

Is there an easy way to acheive this ? Been looking around in IBM documentation and on google, with no luck.

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • Check out this answer: stackoverflow.com/a/2120511/192510. The ODO solutions can get quite messy and sometimes are not feasable (depending on individual circumstances). Note that you will need to "tinker" with the program in the link to use "Attribute" events tags in place of "Content" event tags because you are using element attibutes to convey content as opposed to Element text. – NealB Oct 25 '12 at 14:02

2 Answers2

2

We have had success using OCCURS 0 to 1 DEPENDING var for optional items. We set var to 0 or 1 depending on whether the referent exists.

It's a kludge. There is an extant SHARE requirement to make the kludge unnecessary; I expect the functionality is in the v.Next COBOL compiler.

cschneid
  • 10,237
  • 1
  • 28
  • 39
0

For my mind, an easier way would be to use the XML PARSE verb to write a pretty printer custom to your schema. Do the GENERATE, then feed it through the pretty printer to apply your specific rules to produce the final output.

Joe Zitzelberger
  • 4,238
  • 2
  • 28
  • 42