Example: converting hyphens in element names to underscores

When you generate an XML document from a data structure whose items have data-names that contain hyphens, the generated XML will have element names that contain hyphens. This example shows a way to convert the hyphens in the element names to underscores without changing any hyphens that occur in the element values.

Consider the following data structure:

1 Customer-Record.
 2 Customer-Number pic 9(9).
 2 First-Name      pic x(10).
 2 Last-Name       pic x(20).

When this data structure is populated with some sample values, and XML is generated directly from it and then formatted using program Pretty (shown in Example: generating XML), the result is as follows:

<Customer-Record>
  <Customer-Number>12345</Customer-Number>
  <First-Name>John</First-Name>
  <Last-Name>Smith-Jones</Last-Name>
</Customer-Record>

The element names contain hyphens, and the content of the element Last-Name also contains a hyphen.

Assuming that this XML document is the content of data item xmldoc, and that charcnt is set to the length of this XML document, you can change all the hyphens in the element names to underscores but leave the element values unchanged by using the following code:

1 xmldoc          pic x(16384). 
1 charcnt  comp pic 9(5).
1 pos      comp pic 9(5).
1 tagstate comp pic 9  value zero.
. . .
dash-to-underscore.
  perform varying pos from 1 by 1
          until pos > charcnt 
    if xmldoc(pos:1) = '<'
      move 1 to tagstate
    end-if
    if tagstate = 1 and xmldoc(pos:1) = '-'
      move '_' to xmldoc(pos:1)
    else
      if xmldoc(pos:1) = '>'
        move 0 to tagstate
      end-if
    end-if 
  end-perform.

The revised XML document in data item xmldoc has underscores instead of hyphens in the element names, as shown below:

<Customer_Record>
  <Customer_Number>12345</Customer_Number>
  <First_Name>John</First_Name>
  <Last_Name>Smith-Jones</Last_Name>
</Customer_Record>