SAS Institute. The Power to Know

FOCUS AREAS

Return to previous page

Base SAS

Now What?!?

Transformations

One of the easiest things to do with XML is to transform it into different XML. Why would you want to do that? Many applications are now becoming XML compatible which means that they can read and/or export XML. If the XML output from one application can be transformed into the input format of another application, you have an easy way of sharing data between these two applications.

One of the most recent developments in XML transformations is XSLT (Extensible Stylesheet Language Transformations). XSLT is actually one part of a set of tools under the category XSL at the W3C (World Wide Web Consortium). The other two parts are:

XSL deals with formatting information much like CSS in the HTML world, and the primary purpose of XPath is to traverse the XML document. XPath also includes some facilities for manipulation of strings, numbers and booleans.

NOTE: The examples that follow use the information from the above W3C recommendations, so you may want to read over the XSL recommendations and/or tutorials at the W3C web site before continuing so that you are familiar with the basic syntax.

XSLT Tools

One of the most up-to-date XSLT applications is James Clark's XT. (This may very well be due to the fact that Clark is the editor of the XSLT recommendation.) This application also requires a SAX based XML parser such as XP. Since XT and XP are Java applications, they can run on any platform that has the Java Platform installed. MS Windows users that have the Microsoft Java VM installed can simply download the XT Win32 executable from the XT web site. Users on other platforms will have to download and install all of the packages.

NOTE: If you are not familiar with Java, running XT can be a little confusing. A UNIX shell script can be used to simplify matters. Simply unzip the XT and XP packages, set the JAVA, XT_DIR, and XP_DIR variables in the shell script, and set the file permissions to executable (i.e. chmod +x xt).

Once you have all of the packages installed, you should be able to run XT as follows:

    xt source-xml-file stylesheet-file output-file

Example  3:  Translating Version 8 ODS XML to HTML

Basics

After following the GLMEX example, you should have a valid XML file. This XML file can be converted quite simply to HTML using XT and the XSL file . Simply save the XSL file as odsxml2html.xsl and run the following command.

    xt glmex.xml odsxml2html.xsl glmex.html

You should now have an HTML file like in the figure below.

[Figure]

I want more!

"That's just as fine as frog's hair", you say, but "I want my company's colors to be used in the HTML". There are two ways you can do that (1) modify the XSL file to include the colors you want on each element, or (2) use a CSS (Cascading Style Sheet) file. The first way should work in most browsers used today, but the CSS file will only work in Netscape Navigator/Communicator 4.+ and MS Internet Explorer 3.+.

Suppose your company has the adopted a color scheme that uses Hunter's Orange and Fluorescent Green (don't look at me, it's your company). You could format the tables in the HTML to have these colors in either of the following ways.

XSL with Plain HTML

To change the format of the table cells using XSL, simply replace the data template in the XSL file with the following templates. These templates specify a separate format for a table cell depending on whether or not the cell belongs to the body or a header/footer. This process does not catch row headers, although you could write a template that looks at the class attribute and formats accordingly. I'll just leave that as an exercise for the reader.

      <xsl:template match="data[ancestor::output-head or ancestor::output-foot]">
        <td bgcolor="#44ff44">
          <xsl:apply-templates select="style"/>
          <xsl:apply-templates/>
        </td>
      </xsl:template>

      <xsl:template match="data[ancestor::output-body]">
        <td bgcolor="#ffaa33">
          <xsl:apply-templates select="style"/>
          <xsl:apply-templates/>
        </td>
      </xsl:template>

With this new XSL file, your HTML output should come out looking something like the figure below.

[Figure]

Using this same process with the other templates in the XSL file, you can customize the HTML output as much as you wish.

HTML with CSS

This process still involves a couple changes to the XSL file: adding a link to the CSS file and passing the ODS class names to the HTML table cells. These changes are shown below.

      <xsl:template match="sasxml">
        <html>
          <head>
            <title><xsl:value-of select="proc/title"/></title>
            <link rel="stylesheet" type="text/css" href="xml2html.css"/>
          </head>
          <body>
            <xsl:apply-templates/>
          </body>
        </html>
      </xsl:template>

      <xsl:template match="data">
        <td class="{@class}">
          <xsl:apply-templates select="style"/>
          <xsl:apply-templates/>
        </td>
      </xsl:template>

The CSS file ( xml2html.css) will contain the following:

    .Data {
      background-color: #44ff44;
    }
    .RowHeader, .EmptyHeader, .Header {
      background-color: #ffaa33;
    }

The code above simply states that any tag with the class name Data, RowHeader, EmptyHeader, or Header should be formatted as specified. Here we just changed the background color, but you can change many other parameters such as font characteristics, borders, spacing, etc.

The HTML with CSS should look similar to the figure below.

[Figure]

Example  4:  Translating Version 8 TS1 ODS XML to HTML

Basics

After following the GLMEX example, you should have a valid XML file. This XML file can be converted quite simply to HTML using XT and the XSL file . Simply save the XSL file as odsxml2html.xsl and run the following command.

    xt glmex.xml odsxml2html.xsl glmex.html

You should now have an HTML file like the one in the following figure.

[Figure]

I want more!

"That's just as fine as frog's hair", you say, but "I want my company's colors to be used in the HTML". There are two ways you can do that (1) modify the XSL file to include the colors you want on each element, or (2) use a CSS (Cascading Style Sheet) file. The first way should work in most browsers used today, but the CSS file will only work in Netscape Navigator/Communicator 4.+ and MS Internet Explorer 3.+.

Suppose your company has the adopted a color scheme that uses Hunter's Orange and Fluorescent Green (don't look at me, it's your company). You could format the tables in the HTML to have these colors in either of the following ways.

XSL with Plain HTML

To change the format of the table cells using XSL, simply replace the data and header templates in the XSL file with the following templates. These templates specify extra formatting based on whether or not it is a header cell. You could also write a template that looks at the class attribute and formats accordingly. I'll just leave that as an exercise for the reader.

      <xsl:template match="data">
        <td bgcolor="#ffaa33">
          <xsl:apply-templates select="style"/>
          <xsl:apply-templates/>
        </td>
      </xsl:template>

      <xsl:template match="header">
        <th bgcolor="#44ff44">
          <xsl:apply-templates select="style"/>
          <xsl:apply-templates/>
        </th>
      </xsl:template>

With this new XSL file, your HTML output should come out looking something like in the following figure.

[Figure]

Using this same process with the other templates in the XSL file, you can customize the HTML output as much as you wish.

HTML with CSS

This process still involves one change to the XSL file: adding a link to the CSS file. This change is shown below.

      <xsl:template match="data">
        <td bgcolor="#ffaa33">
          <xsl:apply-templates select="style"/>
          <xsl:apply-templates/>
        </td>
      </xsl:template>

      <xsl:template match="header">
        <th bgcolor="#44ff44">
          <xsl:apply-templates select="style"/>
          <xsl:apply-templates/>
        </th>
      </xsl:template>

The CSS file ( xml2html.css) will contain the following:

    td {
      background-color: #44ff44;
    }
    th {
      background-color: #ffaa33;
    }

The code above simply states that any tag with the name td or th should be formatted as specified. Here we just changed the background color, but you can change many other parameters such as font characteristics, borders, spacing, etc. You could also pass the class name from the XML to the HTML, and set styles based on that criteria.

The HTML with CSS should look like the figure below.

[Figure]