1 cs336607
2 XSL XSL is a standard that consists of three parts: XPath (navigation in documents) XPath was taught in the DB course, so it will not be taught XSLT (transformation of documents) XSLFO (FO for formatting objects) This is a rather complex language for typesetting (i.e., preparing text for printing) It will not be taught cs336607
3
4 XSLT XSLT is a language for transforming XML documents into other XML documents For example, XHTML, RSS, KML, GML, MathML, WML Can also transform XML to text documents, e.g., SQL programs An XSLT program is itself an XML document (called an XSL stylesheet) that describes the transformation process for input documents cs336607
5 XSLT Processors XSLT Processor cs336607
6 Web Page Layout Doc. Structure Data Web Pages – The Whole Picture XML XSLXHTML Style Knowledge CSS cs336607
7 Dark Side of the Moon Pink Floyd Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 catalog.xml cs336607
8 <xsl:stylesheet version="1.0" xmlns:xsl=" cd catalog This is a cd catalog! Valid XML! Commands are XML elements with the namespace xsl Includes XHTML elements catalog.xsl cs336607
9 Applying XSL Stylesheets to XML There are several ways of applying an XSL stylesheet to an XML document: Directly applying an XSLT processor to the XML document and the XSL stylesheet Calling an XSLT processor from within a program Adding to the XML document a link to the XSL stylesheet and letting the browser do the transformation The resulting XHTML document is shown instead of the original XML cs336607
10 Processing XSL in Java You can use the XALAN package of Apache in order to process XSL transformations java org.apache.xalan.xslt.Process -IN myXmlFile.xml -XSL myXslFile.xsl -OUT myOutputFile.html cs336607
11 How Does XSLT Work? An XSL stylesheet is a collection of templates that are applied to source nodes (i.e., nodes of the given XML) Each template has a match attribute that specifies to which source nodes the template can be applied Each source node has at a template that matches it The current source node is processed by applying a template that matches this node When processing a node, it is possible (but not necessary) to recursively process other nodes, e.g., the children of the processed node The XSLT processor processes the document root ( / ) cs336607
12 Templates A template has the form... The content of a template consists of XML elements (e.g., XHTML) and text that are copied to the result XSL elements ( ) that are actually instructions The pattern syntax is a subset of XPath cs336607
13 <xsl:stylesheet version="1.0" xmlns:xsl=" cd catalog This is a cd catalog! catalog1.xsl cs336607
14 Dark Side of the Moon Pink Floyd Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 catalog1.xml cs336607
15 The Result cd catalog This is a cd catalog! In XALAN, automatically added to cs336607
16
17 Examples of Match Attributes match="cd", All elements with tag name cd match="//cd", match="/catalog/cd/artist" All matches of the absolute XPath match="cd/artist" All artist nodes that have a cd parent match="catalog//artist" All artist nodes that have a catalog ancestor cs336607
18 Processing starts by applying a template to the root If no specified template matches the root, then one is inserted by default (see the next slide) The XSL stylesheet must specify explicitly whether templates should be applied to descendants of a node It is done by putting inside a template the instruction: In xpath, the current processed node is used as the context node Without the select attribute, this instruction processes all the children of the current node (including text nodes) <xsl:apply-templates> cs336607
19 Default Templates XSL provides implicit built-in templates that match every element and text nodes Templates we write always override these built-in templates (when they match) cs336607
20 <xsl:stylesheet version="1.0" xmlns:xsl=" Dark Side of the Moon Pink Floyd Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 cs In XALAN and in IE (it yields an empty page in Firefox)
21 <xsl:stylesheet version="1.0" xmlns:xsl=" A cd! A cd! cs336607
22 <xsl:stylesheet version="1.0" xmlns:xsl=" A cd! A cd! Aretha: Lady Soul Aretha Franklin 9.90 cs336607
23 <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:apply-templates An artist! An artist! cs336607
24 <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:apply-templates An artist! cs Does not match the context node
25 <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:apply-templates mode=“first” <xsl:apply-templates mode=“second” An artist! The artist is coming back! cs336607
26
27 The Most Frequently Used Elements of XSL This element extracts the value of a node from the nodelist located by xpath This element loops over all the nodes in the node list located by xpath,, etc. This element is for conditional processing cs336607
28 <xsl:stylesheet version="1.0" xmlns:xsl=" cd catalog CD catalog [ ] Currently selected element is the context (current) node Example 1 catalog2.xsl cs336607
29
30 <xsl:stylesheet version="1.0" xmlns:xsl=" cd catalog CD catalog Example 2 catalog3.xsl cs336607
31 : ( now on sale: $ ) Example 2 (cont.) Entities replace characters price<10 → price<10 cs336607
32 ( Special price! ) ( Good price! ) (Normal price.) Example 3 : Switch syntax for conditions cs336607
33 The Element The element is used to sort the list of nodes that are looped over by the element Thus, the must appear inside the element The looping is done in sorted order cs336607
34 <xsl:stylesheet version="1.0" xmlns:xsl=" cd catalog CD catalog CDs are iterated in ascending order of the titles catalog4.xsl cs336607
35 Setting Values in Attributes The element cannot be used within attribute value However, we can insert expressions into attribute values, by putting the expression inside curly braces ( {} ) Alternatively, we can use in order to construct XML elements cs336607
36 An Example In the following example, we add to each CD entitled t a link to the URL /showcd.jsp?title=t : cs336607
37
38 Using : /showcd.jsp?title= cs336607
39 On XSL Code Typically, an XSLT program can be written in several, very different ways Templates can sometime replace loops and vice versa Conditions can sometimes be replaced with XPath predicates (e.g., in the select attribute) and vice versa A matter of convenience and elegancy cs336607
40 On Recursive Templates It is not always possible to avoid recursive templates That is, use only the template of the root Suppose that we want to write an XSL stylesheet that generates a copy of the source document It is rather easy to do it when the structure of the source XML document is known Can we write an XSL stylesheet that does it for every possible XML document? Yes! (see next slide) cs336607
41 <xsl:stylesheet xmlns:xsl=" version="1.0"> Identity Transformation Stylesheet cs336607
42 Generating Valid XHTML By default, the documents that XSL stylesheets generate are not valid XHTML Next, we will show how XSL stylesheet can be changed in order to generate valid XHTML cs336607
43 <xsl:stylesheet version="1.0" xmlns:xsl=" cd catalog This is a cd catalog! The Original XSL Example cs336607
44 cd catalog This is a cd catalog! Uppercase tag name, unclosed element No DOCTYPE The Original Transformation Result cs336607
45 <xsl:stylesheet version="1.0" xmlns:xsl=" xmlns=" <xsl:output method="html" doctype-public= "-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system= " "/> … Modifying the XSL Example cs336607
<xsl:output method="xml|html|text|name" version="string" encoding="string" omit-xml-declaration="yes|no" standalone="yes|no" doctype-public="string" doctype-system="string" cdata-section-elements="namelist" indent="yes|no" media-type="string"/> cs
47 cd catalog This is a cd catalog! META is not inserted The Transformation Result cs336607
48 Some Other XSL Elements The element inserts free text in the output The creates a copy of the specified nodes (deep copy, i.e., copies the entire subtree) The creates a copy of the specified nodes (does not copy children or attributes) The element creates a comment node in the result tree The element defines a variable (local or global) that can be used within the program cs336607
Example Transform this list of number to be Sorted Alternatingly red and blue cs
cs
cs
cs Example of Using a Variable
Using Variables and Functions Using names, templates can be called directly and serve as functions It is possible to use variables and function in XSL to perform computations Returned values of functions can be set to variables v=f(3)
Computing Fibonacci Numbers 8 Given value Style sheet
if n<=2 return 1 f1=fib(n-1)f2=fib(n-2) return f1+f2
Fibonacci The Fibonacci number at position is val = the value in return fib(val)
Transforming XML to XHTML, in the Browser Use JavaScript Check what is the current browser before applying the transformation (different browsers use a slightly different DOM) cs
cs function loadXMLDoc(fname) { var xmlDoc; // code for IE if (window.ActiveXObject) { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); } // code for Mozilla, Firefox, Opera, etc. else if (document.implementation && document.implementation.createDocument) { xmlDoc=document.implementation.createDocument("","",null); } else { alert('Your browser cannot handle this script'); } xmlDoc.async=true; xmlDoc.load(fname); return(xmlDoc); } XML to XHTML in Browser
cs function displayResult() { xml=loadXMLDoc("cdcatalog.xml"); xsl=loadXMLDoc("cdcatalog.xsl"); // code for IE if (window.ActiveXObject) { ex=xml.transformNode(xsl); document.getElementById("example").innerHTML=ex; } // code for Mozilla, Firefox, Opera, etc. else if (document.implementation && document.implementation.createDocument) { xsltProcessor=new XSLTProcessor(); xsltProcessor.importStylesheet(xsl); resultDocument = xsltProcessor.transformToFragment(xml,document); document.getElementById("example").appendChild(resultDocument); } XML to XHTML in Browser
Applying Xalan from Java Programs The following example illustrate how to apply Xalan on a given XML and XSL from within a Java program cs
cs // Instantiate a DocumentBuilderFactory. javax.xml.parsers.DocumentBuilderFactory dfactory = javax.xml.parsers.DocumentBuilderFactory.newInstance(); // Use the DocumentBuilderFactory to provide access to a DocumentBuilder. javax.xml.parsers.DocumentBuilder dBuilder = dfactory.newDocumentBuilder(); // Use the DocumentBuilder to parse the XML input. org.w3c.dom.Document inDoc = dBuilder.parse("foo.xml"); 1.Generate a document builder 2.Parse the XML document
cs // Generate a Transformer. javax.xml.transform.Transformer transformer = tFactory.newTransformer(new javax.xml.transform.stream.StreamSource("foo.xsl")); // Create an empy DOMResult object for the output. javax.xml.transform.dom.DOMResult domResult = new javax.xml.transform.dom.DOMResult(); // Perform the transformation. transformer.transform(new javax.xml.transform.dom.DOMSource(inDoc), domResult); // Now you can get the output Node from the DOMResult. org.w3c.dom.Node node = domResult.getNode(); 1.Generate a transformer 2.Create an empty DOM result 3.Apply the transformation 4.Get the output node transformer.transform(new DOMSource(inDoc), new StreamResult(new FileOutputStream(“result.html"))); To produce HTML, replace
Links W3C Recommendation: tml tml cs