Skip to main content

XML Parsing RECORD and TRANSFORM Functions

The following functions are valid for use only in field definition expressions within a RECORD structure or TRANSFORM function that is used to define the result set from the PARSE function, or the input RECORD structure for a DATASET containing XML data.

XMLTEXT(xmltag)

XMLTEXT returns the ASCII text from the xmltag.

XMLUNICODE(xmltag)

XMLUNICODE returns the Unicode text from the xmltag.

XMLPROJECT(xmltag, transform)

XMLPROJECT returns the text from the xmltag as a child dataset.

xmltagA string constant naming the XPATH to the tag containing the data (see the XPATH Support section under the RECORD structure discussion). This may contain an instance number (such as tagname[1]).
transformThe TRANSFORM function that produces the child dataset.

Example:

d := DATASET([{'<library><book isbn="123456789X">' +
  '<author>Bayliss</author><title>A Way Too Far</title></book>' +
  '<book isbn="1234567801">' +
  '<author>Smith</author><title>A Way Too Short</title></book>' +
  '</library>'}],
  {STRING line });

rform := RECORD
  STRING author := XMLTEXT('author');
  STRING title := XMLTEXT('title');
END;

books := PARSE(d,line,rform,XML('library/book'));

OUTPUT(books);

//*******************************************
/* The following XML can be parsed using XMLPROJECT
<XML>
<Field name='surname' distinct=2>
<Value count=3>Halliday</Value>
<Value count=2>Chapman</Value>
</Field>
<XML>
*/
extractedValueRec := RECORD
  STRING value;
  UNSIGNED cnt;
END;

extractedRec := RECORD
  STRING name;
  UNSIGNED cnt;
  DATASET(extractedValueRec) values;
END;

x := DATASET([{'<XML>' +
               '<Field name="surname" distinct="2">' +
               '<Value count="3">Halliday</Value>' +
               '<Value count="2">Chapman</Value>' +
               '</Field>' + 
               '</XML>'}],{STRING line});

extractedRec t1 := TRANSFORM
 SELF.name   := XMLTEXT('@name');
 SELF.cnt    := (UNSIGNED)XMLTEXT('@distinct');
 SELF.values := XMLPROJECT('Value',
                            TRANSFORM(extractedValueRec,
                                      SELF.value := XMLTEXT(''),
                                      SELF.cnt :=
                                      (UNSIGNED)XMLTEXT('@count')))(cnt > 1);
 END;
p := PARSE(x, line, t1, XML('XML/Field'));
OUTPUT(p);

See Also: PARSE, RECORD Structure, TRANSFORM Structure, DATASET