Introducing XSLT

XML is a great way to represent data in a portable format, but XML doesn't contain information on how to format that data for display. The Extensible Stylesheet Language Transformation (XSLT) allows you to control the formatting of XML data, and may be used to transform XML data to a format suitable for displaying it as a document.

An XSL stylesheet-also known as an XSLT file-is a template that contains the rules that describe how the data in the XML file is to be formatted for viewing.

The XML and XSLT files are processed together by an XSLT processor. The rules defined in the XSLT file are applied to the data in the XML file, and the final result is output by the XSLT processor. Microsoft Internet Explorer contains an XSLT processor, and you'll see examples in this section that display the results of processing an XML and XSLT file in Internet Explorer.

Note 

Internet Explorer actually comes with a default XSLT file, which causes XML files to use different colors for the parts of the XML document and to be displayed with + and - icons to expand and collapse nested XML data.

Listing 16.9 shows an example XSLT file named CustomersStylesheet.xsl, which you'll find in the xml directory. Later, you'll see how to apply this XLST file to an XML file containing customer data.

Listing 16.9: CUSTOMERSSTYLESHEET.XSL

start example
 <?xml version="1.0"?> <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0"> <xsl:template match="/"> <HTML> <HEAD>   <TITLE>Customers</TITLE> </HEAD> <BODY>   <xsl:for-each select="/NorthwindCustomers/Customers">     <p>     <b>Customer:</b>     <br><xsl:value-of select="CustomerID"/></br>     <br><xsl:value-of select="CompanyName"/></br>     <br><xsl:value-of select="PostalCode"/></br>     <br><xsl:value-of select="Country"/></br>     <br><xsl:value-of select="Phone"/></br>     </p>   </xsl:for-each> </BODY> </HTML> </xsl:template> </xsl:stylesheet> 
end example

As you can see, the CustomersStylesheet.xsl file contains HTML tags and xsl tags. The xsl tags are instructions that indicate how XML is to be transformed. You can reference this XSLT file in an XML file; the rules in the XSLT file are then applied to the data in the XML file. You'll see how to do that later in this section.

Let's take a closer look at the lines in the CustomersStylesheet.xsl file. This file starts with the following line, which indicates that the file uses XML version 1.0:

 <?xml version="1.0"?> 

The next lines are xsl tags:

 <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0"> <xsl:template match="/"> 

The first line uses the xsl:stylesheet tag and specifies that the http://www.w3.org/1999/XSL/Transform namespace is to be used. The second line uses the xsl:template tag and sets the match attribute to /, which specifies that the entire XML document is to be selected and used by the XSLT processor, starting at the root node.

Note 

You can set the match attribute to any XPath expression. For example, if you set match to //Customers, then all the Customers nodes would be selected.

The next lines are HTML tags, which start the HTML part of the file, define a header, and start the body:

 <HTML> <HEAD>   <TITLE>Customers</TITLE> </HEAD> <BODY> 

The next lines are the real meat of the XSLT file and use the xsl:for-each tag to iterate over the Customers nodes:

 <xsl:for-each select="/NorthwindCustomers/Customers">   <p>   <b>Customer:</b>   <br><xsl:value-of select="CustomerID"/></br>   <br><xsl:value-of select="CompanyName"/></br>   <br><xsl:value-of select="PostalCode"/></br>   <br><xsl:value-of select="Country"/></br>   <br><xsl:value-of select="Phone"/></br>   </p> </xsl:for-each> 

The xsl:value-of tag is used to retrieve the element values from the XML file. For example, <xsl:value-of select="CustomerID"/> retrieves the CustomerID element.

The remaining lines close up the HTML and the xsl parts of the file:

 </BODY> </HTML> </xsl:template> </xsl:stylesheet> 

The XSLT file contains only the rules to transform XML data; we still need to provide the XML data itself. Listing 16.10 shows an XML file named CustomersUsingStylesheet.xml, which contains XML data for two customers.

Listing 16.10: CUSTOMERSUSINGSTYLESHEET.XML

start example
 <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="CustomersStylesheet.xsl"?> <NorthwindCustomers>   <Customers>     <CustomerID>ALFKI</CustomerID>     <CompanyName>Alfreds Futterkiste</CompanyName>     <PostalCode>12209</PostalCode>     <Country>Germany</Country>     <Phone>030-0074321</Phone>   </Customers>   <Customers>     <CustomerID>ANATR</CustomerID>     <CompanyName>Ana Trujillo Emparedados y helados</CompanyName>     <PostalCode>05021</PostalCode>     <Country>Mexico</Country>     <Phone>(5) 555-4729</Phone>   </Customers> </NorthwindCustomers> 
end example

This listing is identical to the Customers.xml file shown earlier in Listing 16.7 but with the addition of the following line that references the CustomersStylesheet.xsl file:

 <?xml-stylesheet type="text/xsl" href="CustomersStylesheet.xsl"?> 

This line causes the XSLT processor to read and apply the rules in the CustomersStylesheet.xsl file to the XML data in the CustomersUsingStylesheet.xml file. Figure 16.5 shows how CustomersUsingStylesheet.xml looks when viewed with Internet Explorer. To view this file, right-click on CustomersUsingStylesheet.xml in Windows Explorer and select Open With Internet Explorer.

click to expand
Figure 16.5: Viewing CustomersUsing-Stylesheet.xml in Internet Explorer

When you open CustomersUsingStylesheet.xml, Internet Explorer's XSLT processor opens the CustomersStylesheet.xsl file and applies the rules in it to the XML data in CustomersUsingStylesheet.xml. The output generated by the XSLT processor is then displayed in Internet Explorer.

I've only touched on using XSLT in this section. XSLT is a very powerful language that contains many functions you can use to format your XML data. For more information, see Mastering XSLT by Chuck White (Sybex, 2002). You can also learn more at the World Wide Web Consortium's Web site at www.w3.org; just look for XSLT in the table of contents.




Mastering C# Database Programming
Mastering the SAP Business Information Warehouse: Leveraging the Business Intelligence Capabilities of SAP NetWeaver
ISBN: 0764596373
EAN: 2147483647
Year: 2003
Pages: 181

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