dynamic table on COBOL

I need to generate an xml of invoices, which the XSD specifies that the detail of each invoice can have up to a maximum of 1000 lines.
If I generate with the CBL2XML the XD record file, it generates the following

04 tbf-InvoiceDetails identified by "InvoiceDetails" count
in tbf-InvoiceDetails-count.
05 tbf-IDInvoiceDetail occurs 1000 times identified by
06 tbf-DescriptionDetail PIC X(250) identified by
06 tbf-Quantity PIC X(80) identified by
06 tbf-UnitAmount PIC X(80) identified by
06 tbf-Discount PIC X(80) identified by
06 tbf-TotalAmount PIC X(80) identified by

How can you define a dynamic table, depending on the exact number of invoice lines the invoice contains. Now as it is it generates me an xml with 1000 with "InvoiceDetail" most of them empty.


  • Look at the COUNT IN phrase in the docs. If you use something like the following:

    05 tbf-IDInvoiceDetail occurs 1000 times identified by
    "IDInvoiceDetail" count in detail-count.

    When you read the xml file, detail-count will contain the actual number of IDInvoiceDetail records that are present and if you set detail-count to a value when writing then that is how many IDInvoiceDetail records that will be written to the xml stream.

    Is this what you are looking for?

    • Hi, I have looked at the documentation in

    XML Syntax - COUNT IN Clause


    Syntax Rules:
    1. Data-name-1 is an implicitly defined numeric data item with a picture clause of PIC S9(9) USAGE COMP-5.
    General Rules:
    1. If COUNT IN appears on a data description entry with an OCCURS clause, the dimension of data-name-1 is one less than the dimension of the associated data item.
    2. If COUNT IN appears on a data description without an OCCURS clause, the dimension of data-name-1 is the same as the dimension of the associated data item.

    The COUNT IN clause enables the program to determine the number of occurrences just read, or to specify the number of element occurrences to be output.

    And it is not exactly what I want.
    What I want and need is that if the invoice has as detail only 2 or 3 lines of detail,

    it generates me 2 or 3 lines of the section InvoiceDetail, and not to fill the remaining 999 or 1000 empty elements.

      <DescripcionDetalle>Impresora Tranf.termica Godez modelo RT700</DescripcionDetalle> 
    <DescripcionDetalle>Gastos de envio</DescripcionDetalle>


    Although I indicate in count in "detail-count". that they are 2 lines it generates me the remaining empty ones in the XML.

    I hope I have explained correctly and you understand me.


  • For me the count in phrase appears to do exactly what you are describing.

    Here is the simple example that I am using:

          $set preprocess(prexml) warn endp                     
              select xml-stream assign "out.xml"                
                      organization  is xml                      
                      document-type is "group"                  
                      file status is xml-status.         
           xd xml-stream.                                       
           01  xmls-group   identified by "group".              
               05  f1                       
                   identified by "mytable" occurs 10 times
                      count in table-count.
                   10  field-1 pic x(15)                        
                   identified by "field1".                
                   10  field-2 pic x(15)                        
                   identified by "field2". 
           working-storage section.                             
           01 xml-status         pic s9(9) comp.
           01 any-key            pic x.
           procedure division.                                  
                open output xml-stream                          
                move "Chris" to field-1(1)
                move "micro focus" to field-2(1)
                move "John" to field-1(2)
                move "smith co" to field-2(2)
                move 2 to table-count
                write xmls-group                                
                close xml-stream                                
                accept any-key
                move spaces to xmls-group
                open input xml-stream
                read xml-stream
                display table-count
                accept any-key
                close xml-stream                                
                stop run.

    The resulting XML file has only the 2 records specified in it.

  • Perfect! with the example you provided, I have applied it to my development and it also generates only the records I need.

    Thank you very much.