Previous Page | Next Page

Exporting XML Documents Using an XMLMap

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:

PROC PRINT of Data Set NHL.TEAMS

                     The SAS System                             1

 Obs    NAME          ABBREV    CONFERENCE    DIVISION

   1    Thrashers      ATL       Eastern      Southeast
   2    Hurricanes     CAR       Eastern      Southeast
   3    Panthers       FLA       Eastern      Southeast
   4    Lightning      TB        Eastern      Southeast
   5    Capitals       WSH       Eastern      Southeast
   6    Stars          DAL       Western      Pacific
   7    Kings          LA        Western      Pacific
   8    Ducks          ANA       Western      Pacific
   9    Coyotes        PHX       Western      Pacific
  10    Sharks         SJ        Western      Pacific

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" ?>
<LIBRARY type="GENERIC" version="9.2">
  <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>
.
.
.

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="1.9"> 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>/NHL/CONFERENCE/DIVISION/TEAM</TABLE-PATH>  

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

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

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

       <COLUMN name="DIVISION" retain="YES"> 
           <PATH>/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 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 xml92 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.02.02M3D10192009
      Created 2009-10-20T15:03:53
  -->

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

Previous Page | Next Page | Top of Page