ASPFAQs.com
Your source for ASP-related frequently asked questions and answers!


4GuysFromRolla.com : ASP FAQS : XML

Question: What is XSLT and how does it relate to XML?

Answer: XSLT, or eXtensible Stylesheet Language, is a language for transforming XML content from its native form into some other form. For example, many times developers want to display XML data in a Web page. In these cases, XSLT is used to transform the raw XML content into HTML.

When transforming XML to HTML using XSLT, typically a .xsl file is created that contains the specific "instructions" on how to transform the XML content from XML to HTML. .xsl files are XML-formatted files themselves, and have the following content:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  ... transformation content goes here...
</xsl:stylesheet>

Since XSLT files are XML files, all of the formatting rules that apply to XML files apply to XSLT files. That is, XSLT files are case-sensitive. All tags must be properly nested and have a matching closing tag. This includes the HTML tags you use inside the XSLT file!. For example, if in your XSLT file you wish to add a <br>, you must add it like so: <br />, so that it is a complete tag. (See this FAQ for more information on XML and its formatting criteria.)

Before we dive into an XSLT example, let's first examine an XML file to display. For this FAQ, let's focus on the XML file available at
http://aspnet.4guysfromrolla.com/demos/aspnetbooks.xml.
This XML file contains information on ASP.NET books, and has the following structure:

<books>
  <book price="price of book">
    <title>book title</title>
    <authors>
      <author>Last name of first author</author>
      <author>Last name of second author</author>
      ...
    </authors>
    <year>year published</year>
  </book>

  ...
</books>

For each book, there is a <book> element. The root element of the XML file is <books>. Now that we have examined this XML file, imagine that we want to display its contents in an HTML <table>. We could use XSLT to accomplish this.

XSLT contains a number of tags itself. The most primitive tag is the <xsl:template /> tag. This tag itself contains further XSLT tags and the content to transform the XML data to. The xsl:template tag contains a match attribute, which specifies what tag to match in the XML file. For each instance of the matched tag, the rules within the xsl:template tag are applied.

Since this no doubt sounds terribly confusing, let's take a look at an example, which should help clarify things. Let's say that we have the following XSLT file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="books/book">
    <xsl:value-of select="title" /><br />
  </xsl:template>
</xsl:stylesheet>

Here, the xsl:template tag has books/book as its match attribute. This means that for each books/book in the XML file, the content inside the xsl:template will be applied. (Note that the books/book returns each book element. This books/book syntax is known as XPath, and is discussed in greater detail in this FAQ.)

This code example also introduces a new XSLT tag: xsl:value-of. The xsl:value-of tag displays the text value of the element specified by the select attribute. In the example above, this will display the title of each of the books in the XML file, with a <br /> immediately after each title.

In addition to the xsl:template and xsl:value-of tags, XSLT contains one more vitally important tag: xsl:for-each. The xsl:for-each tag is used to iterate through the child tags of a particular tag. For example, the authors tag in the XML file can have an arbitrary number of child <author> tags. If we wanted to display the authors for a book, we would need to use a xsl:for-each tag to iterate through each <author> tag. The syntax for displaying each author's name for a particular book in an unordered list would be:

<ul>
  <xsl:for-each select="authors/author">
      <li><xsl:value-of select="text()" /></li>
  </xsl:for-each>
</ul>

Here, the XPath expression authors/author specified as the select attribute for the for-each loop causes each <author> tag to be visited. In order to display the value of the <author> tag, we need to use <xsl:value-of select="text()" />. (The text() function returns the contents of the inner text of an XML element.)

Putting it all together, the XSLT file for displaying the list of books in an HTML table can be seen below:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" />
  <xsl:template match="/">
    <table border="1" cellspacing="1" align="center">
      <xsl:for-each select="books/book">
        <tr><td>
          Title: <xsl:value-of select="title" /><br />
          Price: <xsl:value-of select="@price" /><br />
          Authors:
          <ul>
            <xsl:for-each select="authors/author">
              <li><xsl:value-of select="text()" /></li>
            </xsl:for-each>
          </ul>
          Year Published: <xsl:value-of select="year" />
        </td></tr>
      </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>

Here, rather than using the xsl:template tag to match on the each books/book, we simply match on the document's root node (/) and then use an xsl:for-each to visit each <book> element.

(Also note that an <xsl:output ... /> element has been added. This element informs the XSLT engine that the output medium is HTML. If you do not set the method attribute to HTML, the XSLT will emit the XML pre-processing directive - <?xml version="1.0" encoding="utf-8" ?>.)

An example of the HTML output of the above XML and XSLT files can be seen at this live demo.

For more information on XSLT, be sure to check out the following resources:

* XSL Tutorials on w3schools.com
* XSL Tutorials on XMLFiles.com
* Beyond ASP: XML and XSL-based Solutions Simplify Your Data Presentation Layer
* Building Smart Pages with ASP, XML and XSL
* Examining the XML Web Control in ASP.NET

Happy Programming!


FAQ posted by Scott Mitchell at 4/6/2003 8:48:32 PM to the XML category. This FAQ has been viewed 68,298 times.


Copyright 2017 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers