Download presentation
Presentation is loading. Please wait.
Published byBarrie Jordan Modified over 9 years ago
1
1 cs336607
2
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
3
4
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
5 XSLT Processors XSLT Processor cs336607
6
6 Web Page Layout Doc. Structure Data Web Pages – The Whole Picture XML XSLXHTML Style Knowledge CSS cs336607
7
7 Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 catalog.xml cs336607
8
8 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> cd catalog This is a cd catalog! Valid XML! Commands are XML elements with the namespace xsl Includes XHTML elements catalog.xsl cs336607
9
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
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
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
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
13 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> cd catalog This is a cd catalog! catalog1.xsl cs336607
14
14 Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 catalog1.xml cs336607
15
15 The Result cd catalog This is a cd catalog! In XALAN, automatically added to cs336607
16
16
17
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 match="cd[@country='UK']/artist" cs336607
18
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
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
20 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 cs336607 In XALAN and in IE (it yields an empty page in Firefox)
21
21 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> A cd! A cd! cs336607
22
22 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> A cd! A cd! Aretha: Lady Soul Aretha Franklin 9.90 cs336607
23
23 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:apply-templates select="catalog/cd[@country='UK']/artist"/> An artist! An artist! cs336607
24
24 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:apply-templates select="cd[@country='UK']/artist"/> An artist! cs336607 Does not match the context node
25
25 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:apply-templates mode=“first” select="catalog/cd[@country='UK']/artist"/> <xsl:apply-templates mode=“second” select="catalog/cd[@country='UK']/artist"/> An artist! The artist is coming back! cs336607
26
26
27
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
28 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> cd catalog CD catalog [ ] Currently selected element is the context (current) node Example 1 catalog2.xsl cs336607
29
29
30
30 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> cd catalog CD catalog Example 2 catalog3.xsl cs336607
31
31 : ( now on sale: $ ) Example 2 (cont.) Entities replace characters price<10 → price<10 cs336607
32
32 ( Special price! ) ( Good price! ) (Normal price.) Example 3 : Switch syntax for conditions cs336607
33
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
34 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> cd catalog CD catalog CDs are iterated in ascending order of the titles catalog4.xsl cs336607
35
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
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
37
38
38 Using : /showcd.jsp?title= cs336607
39
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
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
41 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Identity Transformation Stylesheet cs336607
42
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
43 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> cd catalog This is a cd catalog! The Original XSL Example cs336607
44
44 cd catalog This is a cd catalog! Uppercase tag name, unclosed element No DOCTYPE The Original Transformation Result cs336607
45
45 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> <xsl:output method="html" doctype-public= "-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system= " http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd "/> … Modifying the XSL Example cs336607
46
<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"/> cs33660746
47
47 cd catalog This is a cd catalog! META is not inserted The Transformation Result cs336607
48
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
49
Example Transform this list of number to be Sorted Alternatingly red and blue cs33660749
50
cs33660750
51
cs33660751
52
cs33660752 Example of Using a Variable
53
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)
54
Computing Fibonacci Numbers 8 Given value Style sheet
55
if n<=2 return 1 f1=fib(n-1)f2=fib(n-2) return f1+f2
56
Fibonacci The Fibonacci number at position is val = the value in return fib(val)
58
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) cs33660758
59
cs33660759 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
60
cs33660760 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
61
Applying Xalan from Java Programs The following example illustrate how to apply Xalan on a given XML and XSL from within a Java program cs33660761
62
cs33660762 // 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
63
cs33660763 // 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
64
Links W3C Recommendation: http://www.w3.org/TR/xslthttp://www.w3.org/TR/xslt http://www.xml.com/pub/a/2000/08/holman/index.h tml http://www.xml.com/pub/a/2000/08/holman/index.h tml http://www.w3schools.com/xsl/default.asp http://xml.apache.org/xalan-j/apidocs/ cs33660764
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.