Use this subprocedure when you need to escape common XML entities & (ampersand), < (less than), > (greater than), ‘ (single quote), and ” (double quote). Several web reports I generate using CGIDEV2 are in HTML or XML form, and those special XML characters must be escaped.
Author’s note: please excuse the old SyntaxHIghlighter format. I hope to write a lexer for Pygments.
- // Variables for XML entity replacement.
- d xmlentities s 1a dim(5) ctdata
- d xmlreplacement s 4a dim(5) alt(xmlentities)
- // Prototype.
- d ReplaceXmlEntities…
- d pr 400a varying
- d invalue 200a options(*varsize) const
- // Subprocedure.
- p ReplaceXmlEntities…
- p b
- d ReplaceXmlEntities…
- d pi 400a varying
- d invalue 200a options(*varsize) const
- d lastpos s 10i 0 inz
- d start s 10i 0 inz
- d workvalue s 400a inz varying
- d z s 10i 0 inz
- /free
- // See the XMLEntities CTDATA array for list.
- // Ampersand (&) should be first because that is what all the
- // other replacements use.
- workvalue = invalue;
- for z = 1 to %elem(xmlentities);
- lastpos = 0;
- start = 1;
- lastpos = %scan(xmlentities(z):invalue:start);
- dow lastpos > 0;
- workvalue = %replace(‘&’+%trim(xmlreplacement(z))+’;’ :
- workvalue:lastpos:1); // ”:1” overwrites the existing “bad” char
- start = lastpos+1;
- lastpos = %scan(xmlentities(z):invalue:start);
- enddo;
- endfor;
- return workvalue;
- /end-free
- p ReplaceXmlEntities…
- p e
- // Compile-time data.
- // This can also be stored in a D-spec array.
- **CTDATA xmlentities
- &
- <lt
- >gt
- ‘apos
- “quot
Feel free to change the size of the invalue, workvalue, and return sizes. I use the procedure over single fields of data which are not very large.