Using an XMLMap to Export an XML Document with a Hierarchical Structure

This example modifies an existing XMLMap to export a SAS data set to a different XML document structure. For the original XMLMap and data set, see Using an XMLMap to Import an XML Document as One SAS Data Set.

Here is the nhl.teams data set to be exported:

PRINT Procedure Output of Data Set nhl.teams

PRINT Procedure Output of Data Set nhl.teams

If the data were exported without an XMLMap, the structure of the resulting XML document would be rectangular and consist of a TEAMS element for each observation in the SAS data set. For example:


<?xml version="1.0" encoding="utf-8" ?>
<TABLE>
  <TEAMS>
    <NAME>Thrashers</NAME>
    <ABBREV>ATL</ABBREV>
    <CONFERENCE>Eastern</CONFERENCE>
    <DIVISION>Southeast</DIVISION>
  </TEAMS>
  <TEAMS>
    <NAME>Hurricanes</NAME>
    <ABBREV>CAR</ABBREV>
    <CONFERENCE>Eastern</CONFERENCE>
    <DIVISION>Southeast</DIVISION>
  </TEAMS>
.
.
.
</TABLE>

To export the SAS data set as an XML document that structures data hierarchically by division within each conference, an XMLMap is required. The only change to the existing XMLMap is to include the OUTPUT element. Notations in the XMLMap syntax are explained.

<?xml version="1.0" ?>
<SXLEMAP version="2.1"> <!--1-->
  <OUTPUT> <!--2-->
    <HEADING> 
      <ATTRIBUTE name="description"
        value="Teams of the National Hockey League" /> <!--3-->
    </HEADING>
    <TABLEREF name="TEAMS" /> <!--4-->
  </OUTPUT>

  <TABLE name="TEAMS">
    <TABLE-PATH syntax="XPath">/NHL/CONFERENCE/DIVISION/TEAM</TABLE-PATH>

    <COLUMN name="NAME">
      <PATH syntax="XPath">/NHL/CONFERENCE/DIVISION/TEAM/@name</PATH>
      <TYPE>character</TYPE>
      <DATATYPE>STRING</DATATYPE>
      <LENGTH>30</LENGTH>
    </COLUMN>

    <COLUMN name="ABBREV">
      <PATH syntax="XPath">/NHL/CONFERENCE/DIVISION/TEAM/@abbrev</PATH>
      <TYPE>character</TYPE>
      <DATATYPE>STRING</DATATYPE>
      <LENGTH>3</LENGTH>
    </COLUMN>

    <COLUMN name="CONFERENCE" retain="YES">
      <PATH syntax="XPath">/NHL/CONFERENCE</PATH>
      <TYPE>character</TYPE>
      <DATATYPE>STRING</DATATYPE>
      <LENGTH>10</LENGTH>
    </COLUMN>

    <COLUMN name="DIVISION" retain="YES">
      <PATH syntax="XPath">/NHL/CONFERENCE/DIVISION</PATH>
      <TYPE>character</TYPE>
      <DATATYPE>STRING</DATATYPE>
      <LENGTH>10</LENGTH>
    </COLUMN>
  </TABLE>
</SXLEMAP>
  1. See the XMLMap syntax for the version numbers that are supported for export.

  2. To use an XMLMap to export the SAS data set as an XML document, you must include the OUTPUT element in the XMLMap. The OUTPUT element can contain the optional HEADING element.

  3. The optional ATTRIBUTE element defines additional XML file attribute information. Each ATTRIBUTE element specifies a name/value pair. To be usable XML, the specification must be a valid XML attribute name and value. Each ATTRIBUTE element is included as an attribute of the root element of the exported document in the form name=value. See the result in the exported XML below.

  4. The TABLEREF element references the name of the table to be exported. The TABLEREF element must reference the name of a TABLE element in the XMLMap.

The following SAS statements export the SAS data set named myfiles.teams to an XML document named nhlout.xml, using an XMLMap named nhlexport.map:

libname myfiles 'path-name/test';
filename out 'path-name/output/nhlout.xml';
libname out xmlv2 xmltype=xmlmap xmlmap='path-name/example/nhlexport.map';
data out.TEAMS;
   set myfiles.teams;
run;

Notice in the code above that TEAMS is uppercase to match the casing in the XMLMap. Here is the resulting XML document:


<?xml version="1.0" encoding="utf-8" ?>
<!--  SAS XML Libname Engine (SAS92XML)
      SAS XMLMap Generated Output
      Version V.04.00M0P09282020
      Created 2020-09-29T13:58:31
  -->

<NHL description="Teams of the National Hockey League">
   <CONFERENCE>Eastern
      <DIVISION>Southeast
         <TEAM name="Thrashers" abbrev="ATL" />
         <TEAM name="Hurricanes" abbrev="CAR" />
         <TEAM name="Panthers" abbrev="FLA" />
         <TEAM name="Lightning" abbrev="TB" />
         <TEAM name="Capitals" abbrev="WSH" />
      </DIVISION>
   </CONFERENCE>
   <CONFERENCE>Western
      <DIVISION>Pacific
         <TEAM name="Stars" abbrev="DAL" />
         <TEAM name="Kings" abbrev="LA" />
         <TEAM name="Ducks" abbrev="ANA" />
         <TEAM name="Coyotes" abbrev="PHX" />
         <TEAM name="Sharks" abbrev="SJ" />
      </DIVISION>
   </CONFERENCE>
</NHL>
Last updated: April 10, 2024