When you generate an XML document from a data structure whose items have data-names that contain hyphens, the generated XML has element or attribute names that contain hyphens. This example shows a way to convert such hyphens to underscores without changing hyphens that occur in element or attribute values.
1 Customer-Record. 2 Customer-Number pic 9(9). 2 First-Name pic x(10). 2 Last-Name pic x(20).
When the data structure above is populated with some sample values, and XML is generated from it and then formatted using program Pretty (shown in Example: generating XML), the result might be 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 has been set to the length of the 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-5 pic 9(5). 1 pos comp-5 pic 9(5). 1 tagstate comp-5 pic 9 value zero. 1 quotestate comp-5 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 if xmldoc(pos:1) = '"' if quotestate = 0 move 1 to quotestate else move 0 to quotestate end-if end-if end-if if tagstate = 1 and quotestate = 0 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, but hyphens in the element values have been preserved, as shown below:
<Customer_Record> <Customer_Number>12345</Customer_Number> <First_Name>John</First_Name> <Last_Name>Smith-Jones</Last_Name> </Customer_Record>