Processing XML Documents Using Style Sheets

for RuBoard

In the previous sections, you saw how to create a schema to define and validate the structure of an XML document. One of the reasons to use schemas is to make your applications more flexible and easier to maintain by dissociating the definition of an XML document from the data found in that document. XML also provides a technology that enables you to define how a given document should be displayed, in a way that is similarly dissociated from the data document itself. This technology is known as XML style sheets, or XSL.

Like XML schemas, XSL style sheets are themselves XML documents, so you don't have to learn a whole new set of data structures to use XSL. However, you will have to ascend a bit of a learning curve as you come to understand the tags and attribute settings offered by XSL.

In addition to defining rules for how XML documents are displayed and formatted, style sheets also perform another interesting and useful function. They can define rules for transforming one type of XML document into another. This process is known as a transformation. The subset of the style sheet language that is responsible for transformations is known as XSLT.

The capability of style sheets to define the display of an XML document and to perform transformations are somewhat different, so we'll cover them separately in this section, starting with transformations.

NOTE

Like the other XML technologies covered in this chapter, we won't cover every aspect of XSL here, only the basics and techniques for integrating XSL into ASP.NET applications using the XML-handling objects found in the .NET framework. For more information on XSL, see the W3C site for XSL located at http://www.w3.org/Style/XSL/. A link to the current (as of this writing) recommendation on XSLT is located at http://www.w3.org/TR/xslt.


Transforming XML Documents Using Style Sheets

No matter what kind of data you have, eventually it will need to be converted for use in some other context. This is the case even with a "universal" data format such as XML. Just because the data is in XML format doesn't mean that every XML-aware software process will be able to consume that data meaningfully. In many cases the document you've defined will need to be changed, or transformed, so that it can be consumed by some other program. This is where the XML Style Sheet Transformations (XSLT) come in.

XSLT is based on the idea that a set of rules (known as a style sheet ) can be applied to an XML document (known as the source tree ) and produce another XML document (known as the result tree ).

For example, suppose you're a bookseller running a point-of-sale software application that exports information about books in a form similar to that shown in Listing 10.40.

Listing 10.40 Bookseller's Hypothetical Output
 <?xml version="1.0"?> <STOCK>   <BOOK isbn="0805062971" ti="Fight Club" au="Chuck Palahniuk" />   <BOOK isbn="0751320005" ti="Eyewitness Travel Guides: London" /> </STOCK> 

This system might export a list of books sold on a daily basis. This list could be sent to one or more publishers' fulfillment systems, which would then automatically restock your shelves with fresh copies of these books to sell to your clamoring customers.

The tricky part here lies in getting your point-of-sale system to talk to the publisher's automated order system. The publisher might expose an XML schema that lets you know exactly what structure an inbound XML document needs to have to be valid, but how do you get the XML emitted by your point-of-sale system to arrange itself so that it's acceptable to the publisher's order system?

To answer this question, let's start by illustrating what a book order might look like in the publisher's hypothetical order system. Listing 10.41 shows such a document.

Listing 10.41 Publisher's Hypothetical Input
 <?xml version="1.0"?> <ORDER custid='10459' date='2001-03-19'>   <BOOK isbn="0805062971">     <TITLE>Fight Club</TITLE>   </BOOK>   <BOOK isbn="0751320005">     <TITLE>Eyewitness Travel Guides: London"</TITLE>   </BOOK> </ORDER> 

As you can see, most of the information provided by the point-of-sale system is used in the publisher's ordering system, but the structure of the document is different. First, the publisher's system requires that you add your customer ID and order date as an attribute of the ORDER node. Next , the title of the book is expressed as a child node of the BOOK node rather than as an attribute. Finally, the author name is discarded (because the publisher can look up the author name , if needed, given the ISBN number that uniquely identifies all books).

It's true that you could write complicated parsing code (possibly using XML DOM objects or other XML-handling objects found in the .NET framework) that transforms the structure of your data document to the structure required by the publisher's system. But such code would likely be inefficient and difficult to maintain. If the publisher's requirements changed at some point in the future, you'd have to go in and hack your export code so that your exported data would continue to conform to their requirements. Instead, transforming the data using XSLT means that you have a much smaller body of code to contend with.

To get you started, we'll include a few minimal examples of XSLT transformations in this chapter and give you pointers on how to perform XSLT transformations programmatically using the XML-handling objects found in the .NET framework classes.

First, consider a document called stock.xml that contains output from the hypothetical point-of-sale application we mentioned earlier. To transform this document into a structure you can use, you first create an XSLT style sheet and then associate that sheet with the document.

Creating a Style Sheet to Transform Data

You use the XslTransform class to transform an XML document from one format to another. Transformations done using XSLT can convert XML files from one XML structure to another or convert an XML file to a different format entirely (such as HTML).

NOTE

The XslTransform class is found in the System.Xml.Xsl namespace. A reference to the classes, properties, and methods introduced in this chapter is included at the end of this chapter.


To transform an XML document, you first create an XSLT style sheet that determines how the input document will be transformed. You then associate the style sheet with the input document (typically using the Load method of the XslTransform object).

Creating an XSLT Style Sheet

An XSLT style sheet is an XML document that specifies how to convert, or transform, another XML document. As with the other XML applications we've discussed in this chapter, such as XSD schemas and XPath queries, XSLT has its own syntax but is ultimately built on an XML framework.

XSLT style sheets typically begin with a stylesheet declaration and a matching expression. The matching expression is an XPath query that determines which elements of the source document you want to convert. If you want to convert the entire source document, you use the expression match="/" .

After you've determined the elements you want to convert, you then create a template to perform the transformation. This is similar to template-based programming in ASP.NET; you hard-code the way you want your output to appear and then include XSL expressions that control how the data is merged with the template.

Listing 10.42 shows an example of a simple style sheet that takes the books.xml data file and transforms it into an HTML table.

Listing 10.42 An XSLT Style Sheet That Converts an XML Document into an HTML Document
 <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   <!-- 'match' attribute is an XPath expression --> <xsl:template match="/">  <HTML>  <BODY>      <TABLE border="1" cellspacing="0" cellpadding="3">        <TR>         <TD bgcolor='#CCCCCC'><B>Title</B></TD>         <TD bgcolor='#CCCCCC'><B>Author</B></TD>       </TR>       <xsl:for-each select="BOOKS/BOOK">       <TR>         <TD>                 <xsl:value-of select="TITLE" />         </TD>         <TD>                 <xsl:value-of select="AUTHOR" />         </TD>       </TR>       </xsl:for-each>       </TABLE>  </BODY>  </HTML> </xsl:template> </xsl:stylesheet> 

You can see the <xsl:stylesheet> heading at the beginning of the style sheet, as well as the <xsl:template> element that marks the beginning of the template. In the body of the template are HTML markup tags, looping constructs ( for-each ), and functions that extract the value from XML nodes and insert them into the output of the transformation ( value-of ). There are a number of other operations similar to for-each and value-of in XSLT; however, you can perform the vast majority of XSLT transformations with these two links.

To see the effect this style sheet has on the XML document, you can create a link from the XML file to the style sheet (described in the next section) and then view the XML file in Internet Explorer.

Associating a Document with a Style Sheet

To link an XML style sheet to an XML document, you use a processing instruction that is similar in syntax to the optional XML declaration. Listing 10.43 shows an example of how to link an XML file to an XSL style sheet.

Listing 10.43 Version of the books.xml File Containing a Link to the books.xsl Style Sheet
 <?xml version="1.0"?> <?xml:stylesheet type="text/xsl" href="books.xsl"?> <BOOKS>   <BOOK>     <TITLE>C# Developer's Guide To ASP.NET, XML and ADO.NET</TITLE>     <AUTHOR id="101" location="San Francisco">Jeffrey P. McManus</AUTHOR>     <AUTHOR id="107" location="Seattle">Chris Kinsman</AUTHOR>   </BOOK>   <BOOK>     <TITLE>How to Pluck a Purdue Chicken</TITLE>     <AUTHOR id="107" location="Seattle">Chris Kinsman</AUTHOR>   </BOOK>   <BOOK>     <TITLE>My Life Among the Baboons</TITLE>     <AUTHOR id="107" location="Seattle">Chris Kinsman</AUTHOR>   </BOOK> </BOOKS> 

When you view this file in Internet Explorer 5.0 or later, you should be able to see the data formatted as an HTML table.

Note that if a hard-coded link to the style sheet exists in the XML document, you do not need to write ASP.NET code to perform the transformation ”as long as you're viewing the XML file in Internet Explorer and the style sheet file is where the link element says it should be, the XML data should be transformed.

Note, too, that it is legal to include multiple xsl-style-sheet instructions in a given XML document. The XSL specification dictates that when multiple style sheets exist, they're interpreted one at a time, one after the other.

Programmatically transforming the document on the server using .NET code requires a bit more effort, however; this is discussed in the next section.

Performing XSL Transformations Programmatically

You may find yourself in a situation where you want to apply an XSL transformation to a given XML file, but you can't or don't want to hard-code a link to the style sheet in the XML document. Perhaps the file is streamed to your application via HTTP, or it resides in a file system in a read-only state. In this case, you must associate the XSL style sheet with the XML data programmatically.

After you have the data and the XSL style sheet, programmatically transforming an XML document is quite easy. To do this, you use the XslTransform object, first loading the XSL style sheet using the Load method and then transforming the XML data using the Transform method. Listing 10.44 shows an example of how this works.

Listing 10.44 Programmatically Transforming an XML File Using an XSL Style Sheet and the XslTransform Object
 <%@ Page debug="true" ContentType="text/xml" %> <%@ Import namespace="System.Xml" %> <%@ Import namespace="System.Xml.XPath" %> <%@ Import namespace="System.Xml.Xsl" %> <SCRIPT runat='server'>   void Page_Load(Object Sender,EventArgs e)   {     XslTransform xslt = new XslTransform();     XPathDocument xpd = new XPathDocument(Server.MapPath("books.xml"));     xslt.Load(Server.MapPath("books.xsl"));     xslt.Transform(xpd, null, Response.OutputStream);   } </SCRIPT> 

It just happens that we chose to stream the output of the transformation to the OutputStream of the Response object (thereby sending the data immediately to the browser). If you choose, you can use one of the many overloaded versions of the Transform method to stream the output to a file or to an XmlReader object for further processing.

Editing XSLT Files Using Visual Studio .NET

You can use Visual Studio .NET to create and edit XSLT documents. To do this, simply right-click a Web project, select Add from the pop-up menu, and choose Add New Item from the submenu. Then, from the dialog box, select XSLT File. Give the file a name and click the Open button, and a blank XSLT document will be created. Visual Studio .NET will create the boilerplate definition of your XSLT document, including a link to the W3C namespace for XSLT located at http://www.w3.org/1999/XSL/Transform.

You'll need to be careful with one thing when using Visual Studio .NET to create and edit XSLT style sheets ”testing XSL by loading the XML document into Internet Explorer (as described in the section "Validating Documents Using W3C Schemas" earlier in this chapter) won't work with the XSLT boilerplate generated by Visual Studio .NET. This is because Internet Explorer versions 5.0 and 5.5 (the current version as of this writing) support an earlier revision of the XSL recommendation.

Specifically, Internet Explorer 5.x requires a link to the older version of the W3C namespace for XSL located at http://www.w3.org/TR/WD-xsl. Internet Explorer 6.0 works fine with the default XSLT schema supplied by Visual Studio .NET.

Fortunately, however, the XML-handling objects in the .NET framework support the current revision of XSLT, so you can use the XSLT editor in Visual Studio .NET to perform server-side transformations.

for RuBoard


C# Developer[ap]s Guide to ASP. NET, XML, and ADO. NET
C# Developer[ap]s Guide to ASP. NET, XML, and ADO. NET
ISBN: 672321556
EAN: N/A
Year: 2005
Pages: 103

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net