Previous Page | Next Page

The XSL Procedure (Preproduction)

Example 1: Transforming an XML Document into Another XML Document


The following example transforms an XML document into another XML document.

This is the input XML document named XMLInput.xml, which contains data about vehicles. Each second-level repeating element describes a particular car, with the nested elements that contain information about the model and year. The make information is an attribute on the second-level repeating element.

<?xml version="1.0" ?>
<vehicles>
  <car make="Ford">
    <model>Mustang</model>
    <year>1965</year>
  </car>
  <car make="Chevrolet">
    <model>Nova</model>
    <year>1967</year>
  </car>
</vehicles>

This is the XSL style sheet named XSLTransform.xsl that describes how to transform the XML. The conversion creates <root> as the root-enclosing element and <model> as the second-level repeating element. Each <model> element in the output XML document will include the values from the <car> element and the make= attribute from the input XML document.

<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/> 
 
<xsl:template match="/vehicles">
       <root> <xsl:apply-templates select="car"/> </root>
</xsl:template>
 
<xsl:template match="car">
	<model make="{@make}">
	   <xsl:value-of select="model" />
	</model>
</xsl:template>
 
</xsl:stylesheet>

The following SAS program transforms the XML document. The procedure specifies the input XML document, the XSL style sheet, and the output XML document.

proc xsl
   in='C:\XMLInput.xml'
   xsl='C:\XSLTransform.xsl'
   out='C:\XMLOutput.xml';
run;

Here is the resulting output XML document named XMLOutput.xml.

Output XML Document

<?xml version="1.0" encoding="UTF-8"?>
<root>
<model make="Ford">Mustang</model>
<model make="Chevrolet">Nova</model>
</root>

Previous Page | Next Page | Top of Page