XMLDECOD: Decoding XML-Encoded Characters

How to:

The XMLDECOD function decodes the following five standard XML-encoded characters when they are encountered in a string:

Character Name

Character

XML-Encoded Representation

ampersand

&

&

greater than symbol

>

>

less than symbol

<

&lt;

double quotation mark

"

&quot;

single quotation mark (apostrophe)

'

&apos;


Top of page

x
Syntax: How to Decode XML-Encoded Characters
XMLDECOD(inlength, source_string, outlength,  output)

where:

inlength

Integer

Is the length of the field containing the source character string or is a field that contains the length.

source_string

Alphanumeric

Is the name of the field containing the source character string or the string enclosed in single quotation marks (').

outlength

Integer

Is the length of the output character string or a field that contains the length.

output

Integer

Is the field that contains the result or the format of the output value enclosed in single quotation marks.



Example: Decoding XML-Encoded Characters

The file XMLFUNCS is a .csv file that contains some unencoded characters and some XML-encoded characters. The Master File is:

FILE = XMLFUNCS, SUFFIX=COM,$
SEGNAME = SEG01, SEGTYPE=S1,$
FIELD=INSTRING, ALIAS=CHARS, USAGE=A30,ACTUAL=A30,$

The contents of the file follow:

CHARS: &  < >  ,$
ENCODED: &amp; &gt;  ,$
ENCODED: &quot; &apos;   ,$
MIXED:  &amp;  < &gt;   ,$

XMLDECOD decodes any of the supported XML-encoded characters. Note that some viewers automatically decode the encoded values for display, so the output is produced in a plain text format (FORMAT WP):

FILEDEF XMLFUNCS DISK xmlfuncs.csv
DEFINE FILE XMLFUNCS
OUTSTRING/A30=XMLDECOD(30,INSTRING,30,'A30');
END
TABLE FILE XMLFUNCS
PRINT INSTRING OUTSTRING
ON TABLE PCHOLD FORMAT WP
ON TABLE SET PAGE NOPAGE

In the output string, XML-encoded characters have been decoded, and characters that were not encoded have been left as they were in the input string:

  INSTRING                        OUTSTRING          
  --------                        ---------                     
  CHARS: &  < >                   CHARS: &  < >                 
  ENCODED: &amp; &gt;             ENCODED: & >                  
  ENCODED: &quot; &apos;          ENCODED: " '                  
  MIXED:  &amp;  < &gt;           MIXED:  &  < >                

WebFOCUS