XSLT Basics

XSLT, also referred to as XSL, is a simple XML-based language for transforming XML into other formats. In its most common use, XSLT transforms XML into HTML, but XSLT can also be used for any output format, including text or XML. In contrast to HTML, XSLT is a procedural language in that you can develop templates and rules for specific XML formats and nodes. XSLT is run through an XSL processor, either a server-side framework such as the .NET Framework, a client library such as MSXML, or the native Web browser.

Although Visual Studio has an item template for XSLT files, we’ll use a simpler syntax. Before we go further, let’s look at a basic Hello World XSLT file. Example 9-1 shows an XSLT file that outputs the text "Hello, World!" while matching the root XML element.

Note

Note

Code for this book is available online at http://www.microsoft.com/mspress/companion/9780735625914. The code for this chapter is in the file Chapter 9.zip.

Example 9-1. A simple XSLT file matches the root node and applies further processing instructions (Web/ XML/HelloWorld.xslt).

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" method="html" encoding="utf-16" />

    <xsl:template match='/'>
        Hello, World!
    </xsl:template>

</xsl:stylesheet>

Tip

Tip

While Visual Studio does include an item template for an XSLT document, the template uses the advanced expanded XSLT syntax, which can be difficult to follow unless you’re an experienced XSLT developer. When developing XSLT resources in my AJAX libraries, I’ll start by replacing the contents of the XSLT file generated by Visual Studio with a root template match, as shown in Example 9-1.

XSLT is based on XPath (XML Path Language), a simple way to select nodes in an XML document. The slash character (/) delimits XML elements, and a single slash is used to select the root element. Therefore, the XSL template defined in Example 9-1 always matches any root element with the root template definition and inserts the contents of the template ("Hello, World!") into the output.

Tip

Tip

Because XSLT is based on XPath, it’s important to understand basic XPath notation. There are two XPath notation styles, an abbreviated notation and an expanded notation. To keep matters simple, I’ll use the abbreviated notation in this book. In fact, I recommend always using the abbreviated syntax in AJAX applications because it is a simpler format.

XSLT has two basic constructs as its building blocks: the template and value-of elements. The xsl:template element defines a template that is processed in response to a match in the XML file. The template element has a match attribute that causes the element to be processed when a match from the XML document is found. Basically, the XML document starts at the root node and processes any matches found in the XSLT. Each template generally defines some output text or HTML and nested select statements, which cause additional elements to be processed recursively.

The xsl:value-of element outputs the text value of the XPath expression it defines. Also of importance is the apply-templates element, which instructs the XSLT processor to process any additional matches at the current level. The XSLT processor always processes the XML document as it traverses the node set—that is, it starts at the root element and recursively processes any matches that are defined by the XSLT document.

Tip

Tip

I’ll cover the basics of XSLT and XPath in this book, but you might want to pick up an additional reference or simply use MSDN online documentation.

For the following XSLT examples, we’ll use the following XML document as the input data. The document defines a simple data structure for a list of books that I’ve written, with two books defined so far. Each book has an id attribute and elements that define its authors.

<books>
  <book id="soAJAX">
    <title>Service Oriented AJAX on the Microsoft Platform</title>
    <author>
      <name>Daniel Larson</name>
      <url>http://daniellarson.spaces.live.com</url>
    </author>
  </book>
  <book id="insideWSS">
    <title>Inside Microsoft Windows SharePoint Services 3.0</title>
    <author>
      <name>Ted Pattison</name>
      <url>http://tedpattison.com</url>
    </author>
    <author>
      <name>Daniel Larson</name>
      <url>http://daniellarson.spaces.live.com</url>
    </author>
  </book>
</books>

When processing XSLT, you must always start at the root of the document because the XSLT processor always starts with the root XML element and processes it. The processor only processes matches, so if you include only a rule matching a book element, the rule will not be processed. To select the root of the document, use a slash. You can also specify a match only for a specific root element by using the slash, such as /books. In the sample document, you would want to select the /books element and then process each book node. The following rules would be used to match books in the sample document. Keep in mind that the rule for /books will only match the root element for books.

<xsl:template match='/books'>
  <xsl:apply-templates />
</xsl:template>

<xsl:template match='book'>
  <!-- Process each book element here. -->
</xsl:template>

Tip

Tip

You might want to create an alternative root match rule for error handling so that you can render friendly error messages for known errors.

You can select child elements from the currently selected node simply by using their name. For example, while the code is processing the XSL template that matches the /books element, you can select the child book nodes by defining a rule for book. For example, to select the XML element <book>, define a template with the rule match=’book’. Within the book template, you would use value-of elements, which select the value of the given XPath expression. For example, to match the XML element title, select ‘title’. To select attributes, use the @ sign. For example, to select the id attribute, select ‘@id’. The following code sample demonstrates a simple select statement for the title element and the id attribute.

<xsl:template match='book'>
    Book: <xsl:value-of select='title'/>
    (<xsl:value-of select='@id'/>)
</xsl:template>

Tip

Tip

To select elements, use the slash (/) to delimit named element nodes. A preceding slash indicates the document root element. For example, the path /foo/bar would select the bar node under the foo root element.

XML and XPath expressions are namespace sensitive. Namespaces in XML documents are used to disambiguate data schemas. When using WCF technologies such as the Data Contract Serializer, namespaces are implemented automatically in the generated XML and are based on the namespace defined on the data contract. For example, the preceding code sample might be a specialized book library schema for your custom application. In that case, you might not be able to understand all XML formats for books, but you do know how to handle the book schema that you’ve defined. To create a namespace in an XML element, define it on the root node as in the following example:

<books xmlns="http://mylibrary" />

The addition of the xmlns attribute specifies the default namespace for the document. This also changes the XPath expressions for the document. In the sample XML document, with the XML namespace added, the root node books is represented by the XPath expression /lib:books, assuming that the XML namespace http://mylibrary has been assigned to the prefix lib. Namespaces are used in XSLT documents to define known elements. You’ll see more examples of XML namespaces in the next section when we implement a real-world use case.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset