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:


                     The SAS System                             1


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

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" />
       <TABLEREF name="TEAMS" /> 4 

  <TABLE name="TEAMS">

        <COLUMN name="NAME"> 

        <COLUMN name="ABBREV"> 

       <COLUMN name="CONFERENCE" retain="YES"> 

       <COLUMN name="DIVISION" retain="YES"> 

  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\';                                               
data out.TEAMS;                                                                                                                         
   set nhl.teams;                                                                                                                   

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

Previous Page | Next Page | Top of Page