When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article

The 4 Guys Present: ASPFAQs.com

Jump to a FAQ
Enter FAQ #:
..or see our 10 Most Viewed FAQs.

4GuysFromRolla.com : ASP FAQS : XML


What is XSLT and how does it relate to XML?

[Print this FAQ]

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

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
This XML file contains information on ASP.NET books, and has the following structure:

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


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

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:

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

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">
          Title: <xsl:value-of select="title" /><br />
          Price: <xsl:value-of select="@price" /><br />
            <xsl:for-each select="authors/author">
              <li><xsl:value-of select="text()" /></li>
          Year Published: <xsl:value-of select="year" />

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.

Do you have a FAQ you'd like to suggest? Suggestions? Comments? If so, send it in! Also, if you'd like to be a FAQ Admin (creating/editing FAQs), let me know! If you are looking for other FAQs, be sure to check out the 4Guys FAQ and Commonly Asked Messageboard Questions!

Most Viewed FAQs:

1.) How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency. (761643 views)
2.) I am using Access and getting a 80004005 error (or a [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot open the file '(unknown)' error) when trying to open a connection! How can I fix this problem? (207777 views)
3.) How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset? (202549 views)
4.) How can I quickly sort a VBScript array? (196039 views)
5.) How can I find out if a record already exists in a database? If it doesn't, I want to add it. (156019 views)
6.) How do I display data on a web page using arrays instead of Do...While...MoveNext...???... (152331 views)
7.) When I get a list of all files in a directory via the FileSystemObject, they aren't ordered in any reasonable way. How can I sort the files by name? Or by size? Or by date created? Or... (140381 views)
8.) For session variables to work, must the Web visitor have cookies enabled? (110162 views)
9.) Can I send emails without using CDONTS? (107083 views)
10.) How can I take the result of a SELECT...MULTIPLE or a group of same-named checkboxes and turn it into a query? That is, if the user selects 3 answers, how can I construct a query that looks for all 3? (106308 views)
Last computed at 9/17/2007 3:22:00 AM

ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article