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

This example explains how to use an existing XMLMap to tell the XML engine how to map a SAS data set back into the specific XML document structure. The XMLMap was used to import the SAS data set NHL.TEAMS in the section Using an XMLMap to Import an XML Document as One SAS Data Set.
First, here is the SAS data set named NHL.TEAMS 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="windows-1252" ?>
<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> 2
          <ATTRIBUTE  name="description" 3
                      value="Teams of the National Hockey League" />
       </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 To use an XMLMap to export the SAS data set as an XML document, you must specify 1.9 or 2.1 as the XMLMap version number.
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 contains one or more HEADING elements and one TABLEREF element.
3 The ATTRIBUTE element, which defines additional file attribute information, specifies a name and description for the exported XML document.
4 The TABLEREF element, which references the name of the table to be exported, specifies the table TEAMS.
The following SAS statements export the SAS data set named NHL.TEAMS to an XML document named NHLOUT.XML, using an XMLMap named NHLEXPORT.MAP:
libname nhl 'C:\My Documents\myfiles';

filename out 'C:\My Documents\XML\NHLOUT.xml';

libname out xmlv2 xmltype=xmlmap  xmlmap='C:\My Documents\XML\NHLexport.map';

data out.TEAMS;
   set nhl.teams;
run;
Here is the resulting XML document:
<?xml version="1.0" encoding="windows-1252" ?>
<!--    SAS XML Libname Engine (SAS92XML)
      SAS XMLMap Generated Output
      Version 9.03.01B0D11142010
      Created 2010-11-15T09:46:32
  -->

<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>