Date post: | 01-Jan-2016 |
Category: |
Documents |
Upload: | maximillian-cameron |
View: | 219 times |
Download: | 0 times |
168493: XML and Web Services (II/2546)
2
XSL - The Style Sheet of XML
XML does not use predefined tags (we can use any tags we want)
<table> could mean an HTML table, a piece of furniture, or something else
XSL: something in addition to the XML document that describes how the document should be displayed
168493: XML and Web Services (II/2546)
3
What is XSLT? XSLT transforms an XML document
into another XML document, such as an XHTML document
XSLT can Add new elements into the output file Remove elements Rearrange and sort elements Test and make decisions about which
elements to display
168493: XML and Web Services (II/2546)
4
How Does XSLT Work? XSLT transforms an XML
source tree into an XML result tree
XSLT uses XPath to define parts of the source document that match one or more predefined templates
168493: XML and Web Services (II/2546)
5
How Does XSLT Work? When a match is found, XSLT will
transform the matching part of the source document into the result document
The parts of the source document that do not match a template will end up unmodified in the result document
168493: XML and Web Services (II/2546)
6
Style Sheet Declaration The root element that declares the
document to be an XSL sheet is <xsl:stylesheet> or <xsl:transform>
The correct way to declare an XSL style sheet according to the W3C XSL Recommendation is:
<xsl:stylesheet version=“1.0”?xmlns:xsl=“
http://www.w3.org/1999/XSL/Transform”>
168493: XML and Web Services (II/2546)
7
XSL Uses Templates An XSL style sheet consists of a
set of rules called templates Each <xsl:template> element
contains rules to apply when a specified node is matched
The match attribute is used to associate the template with a set of XML elements
168493: XML and Web Services (II/2546)
8
XSL Uses Templates (Cont.)
The match attribute can also be used to define a template for a whole branch of the XML document Match = “/” defines the whole document What is another way to select the entire
document? An XSL processor parses an XML
source and tries to find a matching template rule. If it does, instructions inside matching
template are evaluated
168493: XML and Web Services (II/2546)
9
Sample XSL File<?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> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th align="left">Title</th> <th
align="left">Artist</th> </tr>
<xsl:for-each select="catalog/cd">
<tr> <td><xsl:value-of
select="title"/></td> <td><xsl:value-of
select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html></xsl:template></xsl:stylesheet>
168493: XML and Web Services (II/2546)
10
The first lines of the XSL file
<?xml version=“1.0” encoding=“ISO-8859-1”?> Since the style sheet is an XML document itself, the
document begins with an XML declaration <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> The <xsl:stylesheet> tag defines the start of the
style sheet Every XSL file needs to specify the XSL namespace
so that the parser knows which version of XSLT to use
168493: XML and Web Services (II/2546)
11
Namespace in XSL<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match=“/”> The namespace prefix xsl: is used in the
rest of the XSL file to identify XSL processing statements
If a statement is not prefixed with xsl:, then it’s simply copied to the output without being processed. This is the way to add HTML statements to the output
168493: XML and Web Services (II/2546)
12
The <xsl:template> Element
Before processing can begin, the part of the XML document with the transformation must be selected with an XPath expression
The selected section of the document is called a node and is normally selected with the match operator
168493: XML and Web Services (II/2546)
13
The <xsl:value-of> Element
The <xsl:value-of> element extracts the value of a selected node
Example:<td><xsl:value-of
select="catalog/cd/title"/></td> Result: <td>Hide your heart</td>
168493: XML and Web Services (II/2546)
14
The <xsl:for-each> Element
The <xsl:for-each> element allows you to do looping in XSL
It selects every XML element of a specified node set
<xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr></xsl:for-each>Selects every ‘catalog/cd’ element
168493: XML and Web Services (II/2546)
15
Filtering the Output We can filter the output from an
XML file by adding a criterion to the select attribute in the <xsl:for-each> element
<xsl:for-each select=“catalog/cd[artist=‘Bonnie Tyler’]”>
168493: XML and Web Services (II/2546)
16
The <xsl:sort> Element The <xsl:sort> element is used
to sort the output To output the XML file as an
XHTML file, and sort it at the same time, simply add a sort element inside the for-each element in the XSL file
168493: XML and Web Services (II/2546)
17
The <xsl:sort> Element <xsl:for-each select="catalog/cd">
<xsl:sort select="artist"/> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr>
</xsl:for-each> The select attribute indicates what
XML elements to sort on
168493: XML and Web Services (II/2546)
18
The <xsl:if> Element The <xsl:if> element contains a
template that will be applied only if a specified condition is true<xsl:if test=“price > 10 “>
some output …</xsl:if>
The value of the required test attribute contains the expression to be evaluated
168493: XML and Web Services (II/2546)
19
The <xsl:choose> Element
The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests
If no <xsl:when> element is true, and no <xsl:otherwise> element is present, nothing is created
168493: XML and Web Services (II/2546)
20
The <xsl:choose> Element
<xsl:choose><xsl:when test=“price > 10”>
<td bgcolor=“#ff00ff”><xsl:value-of
select=“artist”/></td></xsl:when>
<xsl:otherwise><td><xsl:value-of
select=“artist”></td></xsl:otherwise>
</xsl:choose>
168493: XML and Web Services (II/2546)
21
The <xsl:apply-templates> Element
The <xsl:apply-templates> element applies a template rule to the current element or to the current element’s child nodes
The <xsl:apply-templates> element is always found inside a template body
168493: XML and Web Services (II/2546)
22
<xsl:apply-templates> (Cont.)
<xsl:template match=“cd”><p><xsl:apply-templates
select=“title”/><xsl:apply-templates
select=“artist”/></p></xsl:template>
168493: XML and Web Services (II/2546)
23
<xsl:apply-templates/> The <xsl:apply-templates/>: An
instruction to apply templates to the children of the current nodes
<xsl:template match="/"> <html>
<body> <h2>My CD Collection</h2>
<xsl:apply-templates/> …
</xsl:template>
168493: XML and Web Services (II/2546)
24
The <xsl:element> Element
The <xsl:element> element allows an element to be created with a computed name
It generates elements in time of processing
<xsl:element name=“course”>168493</xsl:element>
168493: XML and Web Services (II/2546)
25
The <xsl:attribute> Element
The <xsl:attribute> element can be used to add attributes to result elements whether created by Literal result elements in the style or Instructions such as xsl:element
<xsl:attribute name=“align”>center</xsl:attribute>
168493: XML and Web Services (II/2546)
26
The <xsl:copy> Element The <xsl:copy> element provides
an easy way of copying the current node
The namespace nodes of the current node are automatically copied
It may have a use-attribute-set attribute to specify and also copy attributes for copied element
168493: XML and Web Services (II/2546)
27
<xsl:copy> (Cont.)<xsl:template match=“h1”>
<xsl:copy use-attribute-sets=“H1”>
<xsl:value-of select=“.”/></xsl:copy>
</xsl:template> Example<h1 align=“center”>Greeting</h1>
168493: XML and Web Services (II/2546)
28
The <xsl:copy-of> element
The xsl:copy-of element can be used to insert a result tree fragment into the result tree, without first converting it to string as xsl:value-of does
But when the result is neither a node-set nor a result tree fragment, the result is converted to a string and then inserted into the result tree
168493: XML and Web Services (II/2546)
29
The <xsl:copy-of> Element
<xsl:template match=“/”><xsl:copy-of select=“/html”/>
</xsl:template>XML: <html><h1>Hello</h1></html> Output:<html><h1>Hello</h1></html>
168493: XML and Web Services (II/2546)
30
The <xsl:output> Element
The <xsl:output> element allows stylesheet authors to specify how they wish the result tree to be output
If an XSLT processor outputs the result tree, it should do so as specified by the <xsl:output> element
However, it is not required to do so
168493: XML and Web Services (II/2546)
31
The <xsl:output> Element
<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method="html"/> <xsl:template match="/"> <xsl:copy-of select="/html"/> </xsl:template></xsl:stylesheet>
168493: XML and Web Services (II/2546)
32
The <xsl:output> Element
XML source<html>
<hr></hr><hr/>
</html>
Output (in HTML)<html>
<hr><hr>
</html>
168493: XML and Web Services (II/2546)
33
The <xsl:output> Element
<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method=“xml"/> <xsl:template match="/"> <xsl:copy-of select="/html"/> </xsl:template></xsl:stylesheet>
168493: XML and Web Services (II/2546)
34
The <xsl:output> Element
XML source<html>
<hr></hr><hr/>
</html>
Output (in XML)<html>
<hr/><hr/>
</html>
168493: XML and Web Services (II/2546)
35
The <xsl:number> Element
The <xsl:number> element is used to insert a formatted number into the result tree
The number to be inserted may be specified by an expression
The value attribute contains an expression
If no value attribute is specified, the xsl:number element inserts a number based on the position of the current node
168493: XML and Web Services (II/2546)
36
<xsl:number> (Cont.) <xsl:number value=“250000”
grouping-separator=“.”/>Output: 250.000 <xsl:number value=“250000”
grouping-size=“2”/>Output: 25,00,00
168493: XML and Web Services (II/2546)
37
The <xsl:text> Element Literal data characters may be
wrapped in an <xsl:text> element
This wrapping may change what whitespace characters are stripped but does not affect how the characters are handled by the XSLT processor
168493: XML and Web Services (II/2546)
38
The <xsl:output> Element
XML source<html>
<hr></hr><hr/><hr/>
</html>
Output<html>
<hr><hr>
</html>
168493: XML and Web Services (II/2546)
39
The <xsl:variable> Element
The <xsl:variable> specify a variable which is a name that may be bound to a value
The value to which a variable is bound (the value of the variable) can be an object of any of the types that can be returned by expressions