extensible stylesheet language (xsl)
DESCRIPTION
Extensible Stylesheet Language (XSL). 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) - PowerPoint PPT PresentationTRANSCRIPT
1cs336607
2
XSLXSL 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
3cs336607
4
XSLTXSLT is a language for transforming XML
documents into other XML documentsFor example, XHTML, RSS, KML, GML,
MathML, WMLCan also transform XML to text documents,
e.g., SQL programsAn XSLT program is itself an XML document
(called an XSL stylesheet) that describes the transformation process for input documents
cs336607
5
Text
WML
DTD
XSLT Processors
XML
XSL
XSLT Processor
XHTML
cs336607
6
Web PageWeb Page
LayoutLayout
Doc. Doc. StructureStructure
DataData
Web Pages – The Whole Picture
XMLXML
XSLXSL
XHTMLXHTML
StyleStyle
KnowledgeKnowledge
CSSCSS
cs336607
7
<?xml version="1.0" encoding="ISO-8859-1"?><catalog> <cd country="UK"> <title>Dark Side of the Moon</title> <artist>Pink Floyd</artist> <price>10.90</price> </cd> <cd country="UK"> <title>Space Oddity</title> <artist>David Bowie</artist> <price>9.90</price> </cd> <cd country="USA"> <title>Aretha: Lady Soul</title> <artist>Aretha Franklin</artist> <price>9.90</price> </cd> </catalog>
catalog.xmlcatalog.xmlcs336607
8
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head><title>cd catalog</title></head> <body><h1>This is a cd catalog!</h1></body> </html> </xsl:template></xsl:stylesheet>
Valid XML!Commands are XML elements
with the namespace xslIncludes XHTML elements
catalog.xslcatalog.xslcs336607
9
Applying XSL Stylesheets to XMLThere are several ways of applying an XSL stylesheet to an XML document:Directly applying an XSLT processor to the XML
document and the XSL stylesheetCalling an XSLT processor from within a programAdding 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 JavaYou 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 itThe current source node is processed by applying a
template that matches this nodeWhen 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
TemplatesA template has the form
<xsl:template match="pattern"> ...</xsl:template>
The content of a template consists of XML elements (e.g., XHTML) and text that are
copied to the resultXSL elements (<xsl:…>) that are actually
instructionsThe pattern syntax is a subset of XPath
cs336607
13
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head><title>cd catalog</title></head> <body><h1>This is a cd catalog!</h1></body> </html> </xsl:template></xsl:stylesheet>
catalog1.xslcs336607
14
<?xml version="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/xsl"
href="catalog1.xsl"?><catalog> <cd country="UK"> <title>Dark Side of the Moon</title> <artist>Pink Floyd</artist> <price>10.90</price> </cd> <cd country="UK"> <title>Space Oddity</title> <artist>David Bowie</artist> <price>9.90</price> </cd> <cd country="USA"> <title>Aretha: Lady Soul</title> <artist>Aretha Franklin</artist> <price>9.90</price> </cd> </catalog> catalog1.xml
cs336607
15
The Result<html> <head> <META http-equiv="Content-Type" content="text/html;
charset=UTF-8"> <title>cd catalog</title> </head> <body> <h1>This is a cd catalog!</h1> </body></html>
In XALAN, automatically added to <head>
cs336607
cs336607 16
17
Examples of Match Attributesmatch="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
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:
<xsl:apply-templates select="xpath"/>In xpath, the current processed node is used as
the context nodeWithout the select attribute, this
instruction processes all the children of the current node (including text nodes)
<xsl:apply-templates><xsl:apply-templates>
cs336607
19
Default TemplatesXSL provides implicit built-in templates
that match every element and text nodes
Templates we write always override these built-in templates (when they match)
<xsl:template match="/|*"><xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()|@*"><xsl:value-of select="."/>
</xsl:template>
cs336607
20
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"></xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?> 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
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="cd"> <h2>A cd!</h2> </xsl:template></xsl:stylesheet>
<h2>A cd!</h2> <h2>A cd!</h2> <h2>A cd!</h2>
cs336607
22
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="cd[@country='UK']"> <h2>A cd!</h2></xsl:template></xsl:stylesheet>
<h2>A cd!</h2> <h2>A cd!</h2> Aretha: Lady Soul Aretha Franklin9.90
cs336607
23
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:apply-templates select="catalog/cd[@country='UK']/artist"/> </xsl:template> <xsl:template match="artist"> <h2>An artist!</h2> </xsl:template></xsl:stylesheet>
<h2>An artist!</h2> <h2>An artist!</h2>
cs336607
24
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:apply-templates select="cd[@country='UK']/artist"/> </xsl:template> <xsl:template match="artist"> <h2>An artist!</h2> </xsl:template></xsl:stylesheet>
cs336607
Does not match the context node
25
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:apply-templates mode=“first” select="catalog/cd[@country='UK']/artist"/> <xsl:apply-templates mode=“second” select="catalog/cd[@country='UK']/artist"/> </xsl:template>
<xsl:template mode=“first” match="artist"> <h2>An artist!</h2> </xsl:template> <xsl:template mode=“second” match="artist"> <h2>The artist is coming back!</h2> </xsl:template></xsl:stylesheet>
cs336607
cs336607 26
27
The Most Frequently Used Elements of XSL<xsl:value-of select="xpath"/>
This element extracts the value of a node from the nodelist located by xpath
<xsl:for-each select="xpath"/>This element loops over all the nodes in the
node list located by xpath
<xsl:if test="cond"/>, <xsl:if test="xpath"/>, etc.This element is for conditional processing
cs336607
28
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><head><title>cd catalog</title></head> <body> <h1>CD catalog</h1> <ul> <xsl:for-each select="catalog/cd">
<li><xsl:value-of select="title"/> [<xsl:value-of select="artist"/>]</li> </xsl:for-each> </ul></body></html> </xsl:template></xsl:stylesheet>
Currently selected element is the context (current) node
Example 1
catalog2.xslcs336607
cs336607 29
30
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><head><title>cd catalog</title></head> <body> <h1>CD catalog</h1> <ul> <xsl:for-each select="catalog/cd">
<li><xsl:apply-templates select="."/></li> </xsl:for-each> </ul></body></html> </xsl:template>
Example 2
catalog3.xslcs336607
31
<xsl:template match="cd"> <b><xsl:value-of select="artist"/></b>: <xsl:value-of select="title"/> <xsl:if test="price<10"> (<em>now on sale: $<xsl:value-of select="price"/> </em>) </xsl:if> </xsl:template></xsl:stylesheet>
Example 2 (cont.)
Entities replace characters price<10 → price<10
cs336607
32
<xsl:choose> <xsl:when test="price < 9"> (<em>Special price!</em>) </xsl:when> <xsl:when test="price>9 and price<=10"> (<i>Good price!</i>) </xsl:when> <xsl:otherwise> (Normal price.) </xsl:otherwise></xsl:choose>
Example 3
<xsl:choose>:
Switch syntax for conditions
cs336607
33
The <xsl:sort> ElementThe <xsl:sort> element is used to sort the
list of nodes that are looped over by the <xsl:for-each> element
Thus, the <xsl:sort> must appear inside the <xsl:for-each> element
The looping is done in sorted order
cs336607
34
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><head><title>cd catalog</title></head> <body> <h1>CD catalog</h1> <ul> <xsl:for-each select="catalog/cd">
<xsl:sort select="title"/> <li><xsl:value-of select="title"/></li> </xsl:for-each> </ul></body></html> </xsl:template></xsl:stylesheet>
CDs are iterated in ascending order of the titles
catalog4.xslcs336607
35
Setting Values in AttributesThe <xsl:value-of> element cannot be used
within attribute valueHowever, we can insert expressions into
attribute values, by putting the expression inside curly braces ({})
Alternatively, we can use <xsl:element> in order to construct XML elements
cs336607
36
An ExampleIn the following example, we add to each
CD entitled t a link to the URL /showcd.jsp?title=t
<xsl:template match="cd"> <b><xsl:value-of select="artist"/></b>: <a href="/showcd.jsp?title={./title}">
<xsl:value-of select="title"/> </a>
</xsl:template>
cs336607
cs336607 37
38
Using <xsl:element>
<xsl:template match="cd"> <b><xsl:value-of select="artist"/></b>: <xsl:element name="a"> <xsl:attribute name="href"> /showcd.jsp?title=<xsl:value-of select="title"/> </xsl:attribute> <xsl:value-of select="title"/> </xsl:element> </xsl:template>
cs336607
39
On XSL CodeTypically, an XSLT program can be written in
several, very different waysTemplates can sometime replace loops and
vice versaConditions 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 TemplatesIt is not always possible to avoid recursive
templatesThat is, use only the template of the root
Suppose that we want to write an XSL stylesheet that generates a copy of the source documentIt is rather easy to do it when the structure of the
source XML document is knownCan we write an XSL stylesheet that does it for
every possible XML document?Yes! (see next slide)
cs336607
41
<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>
<xsl:template match="*"> <xsl:element name="{name()}"> <xsl:for-each select="@*"> <xsl:attribute name="{name()}"> <xsl:value-of select="."/> </xsl:attribute> </xsl:for-each> <xsl:apply-templates/> </xsl:element> </xsl:template>
</xsl:stylesheet>
Identity TransformationStylesheet
cs336607
42
Generating Valid XHTMLBy default, the documents that XSL
stylesheets generate are not valid XHTMLNext, we will show how XSL stylesheet can
be changed in order to generate valid XHTML
cs336607
43
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head><title>cd catalog</title></head> <body><h1>This is a cd catalog!</h1></body> </html> </xsl:template></xsl:stylesheet>
The Original XSL Example
cs336607
44
<html> <head> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>cd catalog</title> </head> <body> <h1>This is a cd catalog!</h1> </body></html>
Uppercase tag name, unclosed element
No DOCTYPE
The Original Transformation Result
cs336607
45
<?xml version="1.0" encoding="ISO-8859-1"?><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"/> <xsl:template match="/"> …
Modifying the XSL Example
cs336607
<xsl:output/><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"/>
cs336607 46
47
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>cd catalog</title> </head> <body> <h1>This is a cd catalog!</h1> </body></html> META is not inserted
The Transformation Result
cs336607
48
Some Other XSL ElementsThe <xsl:text> element inserts free text in
the outputThe <xsl:copy-of select="xpath"> creates
a copy of the specified nodes (deep copy, i.e., copies the entire subtree)
The <xsl:copy select="xpath"> creates a copy of the specified nodes (does not copy children or attributes)
The <xsl:comment> element creates a comment node in the result tree
The <xsl:variable> element defines a variable (local or global) that can be used within the program
cs336607
ExampleTransform this list of number to be
SortedAlternatingly red and blue
cs336607 49
cs336607 50
cs336607 51
cs336607 52
Example of Using a Variable
Using Variables and FunctionsUsing names, templates can be called
directly and serve as functionsIt is possible to use variables and function in
XSL to perform computationsReturned values of functions can be set to
variables<xsl:variable name=“v"> <xsl:call-template name=“f"> <xsl:with-param name=“n" select=“3"/> </xsl:call-template></xsl:variable>
v=f(3)
Computing Fibonacci Numbers
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="fib.xsl"?>
<val>8
</val>
Given value
Style sheet
<?xml version="1.0"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template name="fib"> <xsl:param name="n"/> <xsl:choose> <xsl:when test="$n < 2"> <xsl:value-of select="1"/> </xsl:when> <xsl:otherwise> <xsl:variable name="f1"> <xsl:call-template name="fib"> <xsl:with-param name="n" select="$n -1"/>
</xsl:call-template> </xsl:variable> <xsl:variable name="f2"> <xsl:call-template name="fib"> <xsl:with-param name="n" select="$n -2"/>
</xsl:call-template> </xsl:variable> <xsl:value-of select="$f1+$f2"/> </xsl:otherwise> </xsl:choose></xsl:template>
if n<=2 return 1
f1=fib(n-1)
f2=fib(n-2)
return f1+f2
<xsl:template match="/"> <html><head><title>Fibonacci</title></head> <body> <xsl:variable name="val"> <xsl:value-of select="/val"/> </xsl:variable> The Fibonacci number at position <xsl:value-of select="$val"/> is <xsl:call-template name="fib"> <xsl:with-param name="n" select="$val"/> </xsl:call-template> </body> </html> </xsl:template></xsl:stylesheet>
val = the value in <val> </val>
return fib(val)
Transforming XML to XHTML, in the BrowserUse JavaScriptCheck what is the current browser before
applying the transformation (different browsers use a slightly different DOM)
cs336607 58
cs336607 59
<html> <head> <script> 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
cs336607 60
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); } } </script> </head> <body id="example" onLoad="displayResult()"> </body> </html>
XML to XHTML in Browser
Applying Xalan from Java ProgramsThe following example illustrate how to apply
Xalan on a given XML and XSL from within a Java program
cs336607 61
cs336607 62
// 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
cs336607 63
// 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 transformer2. Create an empty DOM
result3. Apply the transformation4. Get the output node
transformer.transform(new DOMSource(inDoc), new StreamResult(new FileOutputStream(“result.html")));
To produce HTML,replace
LinksW3C Recommendation:
http://www.w3.org/TR/xslthttp://www.xml.com/pub/a/2000/08/holman/
index.htmlhttp://www.w3schools.com/xsl/default.asphttp://xml.apache.org/xalan-j/apidocs/
cs336607 64