1 XSLT Introduction
2 XSLT XSL (eXtensible Stylesheet Language) is a styling language for XML. XSLT stands for XSL Transformations. This tutorial will teach you how to use XSLT to transform XML documents into other formats (like transforming XML into HTML).
3
4 <. xml version="1. 0". > My CD Collection
Title Artist
5 XSLT Elements ReferenceDescription apply-imports Applies a template rule from an imported style sheet apply-templates Applies a template rule to the current element or to the current element's child nodes attribute Adds an attribute attribute-set Defines a named set of attributes call-template Calls a named template choose Used in conjunction with
6 Element Description copy Creates a copy of the current node (without child nodes and attributes) copy-of Creates a copy of the current node (with child nodes and attributes) decimal-format Defines the characters and symbols to be used when converting numbers into strings, with the format-number() function element Creates an element node in the output document fallback Specifies an alternate code to run if the processor does not support an XSLT element for-each Loops through each node in a specified node set if Contains a template that will be applied only if a specified condition is true import Imports the contents of one style sheet into another. Note: An imported style sheet has lower precedence than the importing style sheet include Includes the contents of one style sheet into another. Note: An included style sheet has the same precedence as the including style sheet
7 Element Description key Declares a named key that can be used in the style sheet with the key() function message Writes a message to the output (used to report errors) namespace-alias Replaces a namespace in the style sheet to a different namespace in the output number Determines the integer position of the current node and formats a number otherwise Specifies a default action for the
8 Element Description stylesheet Defines the root element of a style sheet template Rules to apply when a specified node is matched text Writes literal text to the output transform value-of Extracts the value of a selected node variable Declares a local or global variable when Specifies an action for the
9 XSL(T) Languages XSLT is a language for transforming XML documents.XPath is a language for navigating in XML documents. XQuery is a language for querying XML documents. What is XSLT? XSLT stands for XSL Transformations XSLT is the most important part of XSL XSLT transforms an XML document into another XML document XSLT uses XPath to navigate in XML documents XSLT is a W3C Recommendation
10 Create an XSL Style Sheet My CD Collection
Title Artist
11 <. xml version="1. 0" encoding="UTF-8"
12 RESULT
13 XSLT
14 Create an XSL Style Sheet My CD Collection
Title Artist . .
15 <. xml version="1. 0" encoding="UTF-8"
16
17
18
19 My CD Collection
Title Artist .
20 XSLT Example ExplainedSince an XSL style sheet is an XML document, it always begins with the XML declaration: . The next element,
21 XSLT
22 <. xml version="1. 0" encoding="UTF-8" My CD Collection
Title Artist
23 My CD Collection
Title Artist
24 XSLT
25 XSLT
26 <. xml version="1. 0" encoding="UTF-8" My CD Collection
Title Artist
27 RESULT
28 XSLT
29 XSLT
30 XSLT
31 <. xml version="1. 0" encoding="UTF-8" My CD Collection
Title Artist
32 Note: The select attribute indicates what XML element to sort on.RESULT Note: The select attribute indicates what XML element to sort on.
33 XSLT
34 <. xml version="1. 0" encoding="UTF-8" My CD Collection
Title Artist Price
35 XSLT
36 <. xml version="1. 0" encoding="UTF-8" My CD Collection
Title Artist
37 RESULT
38 XSLT
39 <. xml version="1. 0" encoding="UTF-8" My CD Collection
40 RESULT
41 XSLT - On the Client XSLT can be used to transform the document to XHTML in your browser. A JavaScript Solution In the previous chapters we have explained how XSLT can be used to transform a document from XML to XHTML. We did this by adding an XSL style sheet to the XML file and let the browser do the transformation.Even if this works fine, it is not always desirable to include a style sheet reference in an XML file (e.g. it will not work in a non XSLT aware browser.) A more versatile solution would be to use a JavaScript to do the transformation. By using a JavaScript, we can: do browser-specific testing use different style sheets according to browser and user needs That is the beauty of XSLT! One of the design goals for XSLT was to make it possible to transform data from one format to another, supporting different browsers and different user needs.
42 XML File
43 XSL File My CD Collection
Title Artist
44 Transforming XML to XHTML in the Browser
46 RESULT
47 Transforming XML to XHTML in the BrowserThe loadXMLDoc() function does the following: Create an XMLHttpRequest object Use the open() and send() methods of the XMLHttpRequest object to send a request to a server Get the response data as XML data The displayResult() function is used to display the XML file styled by the XSL file: Load XML and XSL files Test what kind of browser the user has If Internet Explorer: Use the transformNode() method to apply the XSL style sheet to the xml document Set the body of the current document (id="example") to contain the styled xml document If other browsers: Create a new XSLTProcessor object and import the XSL file to it Use the transformToFragment() method to apply the XSL style sheet to the xml document
48 XSLT - On the Server To make XML data available to all kind of browsers, we can transform the XML document on the SERVER and send it back to the browser as XHTML. A Cross Browser Solution In the previous chapter we explained how XSLT can be used to transform a document from XML to XHTML in the browser. We used a JavaScript and an XML parser for the transformation. However, this will not work in a browser that doesn't have an XML parser.To make XML data available to all kind of browsers, we can transform the XML document on the server and send back to the browser as XHTML. That's another beauty of XSLT. One of the design goals for XSLT was to make it possible to transform data from one format to another on a server, returning readable data to all kinds of browsers.
49 XML File
50 XSL FILE My CD Collection
Title Artist
51 PHP Code: Transform XML to XHTML on the Serverload('cdcatalog.xml'); // Load XSL file $xsl = new DOMDocument; $xsl->load('cdcatalog.xsl'); // Configure the transformer $proc = new XSLTProcessor; // Attach the xsl rules $proc->importStyleSheet($xsl); echo $proc->transformToXML($xml); ?>
52 ASP Code: Transform XML to XHTML on the Server<% 'Load XML file set xml = Server.CreateObject("Microsoft.XMLDOM") xml.async = false xml.load(Server.MapPath("cdcatalog.xml")) 'Load XSL file set xsl = Server.CreateObject("Microsoft.XMLDOM") xsl.async = false xsl.load(Server.MapPath("cdcatalog.xsl")) 'Transform file Response.Write(xml.transformNode(xsl)) %>