Top Banner
IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al- Shorbagy
44

IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Dec 27, 2015

Download

Documents

Duane McKenzie
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

IS432Semi-Structured Data

Lecture 5:

XSLT

Dr. Gamal Al-Shorbagy

Page 2: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

XML Stylesheet Langugae

Extensible Stylesheet Language (XSL) Language for document transformation

Transformation Converting XML to another form

Formatting objectsLayout of XML document

Search document contents Navigating XML document tree nodes

http://www.codeproject.com/Articles/294380/Applying-XSLT-Stylesheet-to-an-XML-File-at-Runtime

Page 3: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

XML Stylesheet Language

XSL-FO

XSLT

XPathNavigating XML Document Formatting XML Document

Transforming XML Document

Page 4: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

CSS (Cascading Style Sheets)

• CSS = Style Sheets for HTML• HTML uses predefined tags, and the meaning

of each tag is well understood.• The <table> tag in HTML defines a table - and

a browser knows how to display it.• Adding styles to HTML elements are simple.

Telling a browser to display an element in a special font or color, is easy with CSS.

Page 5: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

XML Style Sheets

• XSL = Style Sheets for XML• XML does not use predefined tags (we can use

any tag-names we like), and therefore the meaning of each tag is not well understood.

• A< table> tag could mean an HTML table, a piece of furniture, or something else - and a browser does not know how to display it.

• XSL describes how the XML document should be displayed!

Page 6: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

XSL In Parts

• XSL - More Than a Style Sheet Language• XSL consists of three parts:– XSLT - a language for transforming XML

documents– XPath - a language for navigating in XML

documents– XSL-FO - a language for formatting XML

documents

Page 7: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

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

Page 8: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

XSLT = XSL Transformations

• XSLT is the most important part of XSL.• XSLT is used to transform an XML document

into another XML document– Or another type of document that is recognized by

a browser, like HTML and XHTML. – Normally XSLT does this by transforming each XML

element into an (X)HTML element.

Page 9: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

XSLT = XSL Transformations

• With XSLT you can add/remove elements and attributes to or from the output file. – You can also rearrange and sort elements, – Perform tests and make decisions about which

elements to hide and display, and a lot more.

• A common way to describe the transformation process is to say that XSLT transforms an XML source-tree into an XML result-tree.

Page 10: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Browsers Support for XSLT• Mozilla Firefox

– Firefox supports XML, XSLT, and XPath from version 3.• Internet Explorer

– Internet Explorer supports XML, XSLT, and XPath from version 6.– Internet Explorer 5 is NOT compatible with the official W3C XSL

Recommendation.• Google Chrome

– Chrome supports XML, XSLT, and XPath from version 1.• Opera

– Opera supports XML, XSLT, and XPath from version 9. Opera 8 supports only XML + CSS.

• Apple Safari– Safari supports XML and XSLT from version 3.

Page 11: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Example Study

• How to transform XML into XHTML using XSLT.

Page 12: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Transform XML into XHTML

1. Style Sheet Declaration2. Start with a Raw XML Document3. Create an XSL Style Sheet4. Link the XSL Style Sheet to the XML Document

Page 13: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Style Sheet Declaration

<xsl:transform version="1.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:stylesheet version="1.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

W3C XSLT Recommendation

The official W3C XSLT namespaceMust attribute, value pair

top

Page 14: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Raw XML Document<?xml version="1.0" encoding="ISO-8859-1"?>

< catalog><cd>

<title>Empire Burlesque</title><artist>Bob Dylan</artist><country>USA</country><company>Columbia</company><price>10.90</price><year>1985</year>

</cd>..

< /catalog>

Page 15: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

An XSL Style Sheet<?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>Title</th><th>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>

XML document declaration

This document is a Stylesheet

Page 16: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Linking XML with Stylesheet<?xml version="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>< catalog>

<cd><title>Empire Burlesque</title><artist>Bob Dylan</artist><country>USA</country><company>Columbia</company><price>10.90</price><year>1985</year>

</cd>..

< /catalog>

Page 17: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

XSL Templates

• An XSL style sheet consists of one or more set of rules that are called templates.

• A template contains rules to apply when a specified node is matched.

Page 18: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

XSL Templates

• The <xsl:template> Element– Used to build templates

• The match attribute – Associates a template with an XML element.– Also used to define a template for the entire XML

document. – The value of the match attribute is an XPath

expression • (i.e. match="/" defines the whole document).

Page 19: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

XSL Templates

• The <xsl:template> element defines a template.

• The match="/" attribute associates the template with the root of the XML source document.

• The content inside the <xsl:template> element defines some HTML to write to the output.

Page 20: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Xpath ExpressionsPath Expression Result

bookstore

/bookstore

bookstore/book

//book

bookstore//book

//@lang

<?xml version="1.0" encoding="ISO-8859-1"?><bookstore> <book> <title lang="eng">Harry Potter</title> <price>29.99</price> </book> <book> <title lang="eng“>XML 4 Dummies</title> <price>39.95</price> </book> <book> <title lang=“kor“>The Han River</title> <price>149.95</price> </book></bookstore>

Page 21: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Example XSL Style Sheet<?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>Title</th><th>Artist</th>

</tr><xsl:for-each select="catalog/cd">

<tr><td>.</td><td>.</td>

</tr></xsl:for-each>

</table> </body> </html> < /xsl:template>

< /xsl:stylesheet>

XML document declaration

This document is a Stylesheet

End of templateEnd of stylesheet

associates the template with the root of the XML source document

HTML to write to the output

Page 22: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Output

Page 23: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Stylesheet Nuts&Bolts

• The <xsl:value-of> Element–Extract the value of an XML element and

add it to the output stream of the transformation–The select attribute contains an XPath

expression

<tr> <td><xsl:value-of select="catalog/cd/title"/></td> <td><xsl:value-of select="catalog/cd/artist"/></td> </tr>

Page 24: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Example XSL Style Sheet<?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>Title</th><th>Artist</th>

</tr><xsl:for-each select="catalog/cd">

<tr><td><xsl:value-of select="catalog/cd/title"/></td><td><xsl:value-of select="catalog/cd/artist"/></td>

</tr></xsl:for-each>

</table> </body> </html> < /xsl:template>

< /xsl:stylesheet>

XML document declaration

This document is a Stylesheet

End of templateEnd of stylesheet

Selecting a specific element value

HTML to write to the output

Page 25: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Output

Page 26: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Stylesheet Nuts&Bolts

• The <xsl:for-each> Element– select every XML element of a specified element

list– Note: An XPath expression in value of select.

<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>

Page 27: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Example XSL Style Sheet<?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>Title</th><th>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>

XML document declaration

This document is a Stylesheet

End of templateEnd of stylesheet

Selecting each element in a list of elements

HTML to write to the output

Page 28: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Output

Page 29: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Filtering the Output using PredicatesPath Expression Result/bookstore/book[1]

/bookstore/book[last()]

/bookstore/book[last()-1]

/bookstore/book[position()<3]

//title[@lang]

//title[@lang=‘kor']

/bookstore/book[price>35.00]

/bookstore/book[price>35.00]/title

<?xml version="1.0" encoding="ISO-8859-1"?><bookstore> <book> <title lang="eng">Harry Potter</title> <price>29.99</price> </book> <book> <title lang="eng“>XML 4 Dummies</title> <price>39.95</price> </book> <book> <title lang=“kor“>The Han River</title> <price>149.95</price> </book></bookstore>

Page 30: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Example XSL Style Sheet<?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>Title</th><th>Artist</th>

</tr><xsl:for-each select=“catalog/cd[artist='Bob Dylan']“ >

<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>

XML document declaration

This document is a Stylesheet

End of templateEnd of stylesheet

Filtering output

HTML to write to the output

Page 31: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Output ?

Page 32: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Stylesheet Nuts&Bolts

• The <xsl:sort> Element– Sort the out put wrt select value– Value of select is ?.

<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>

Page 33: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Example XSL Style Sheet<?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>Title</th><th>Artist</th>

</tr><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>

</table> </body> </html> < /xsl:template>

< /xsl:stylesheet>

XML document declaration

This document is a Stylesheet

End of templateEnd of stylesheet

Sorting output

HTML to write to the output

Page 34: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Output

Page 35: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Stylesheet Nuts&Bolts

• Placement: The <xsl:if> element always inside the <xsl:for-each> element

• Attribute: The Value of test is an XPath expression.

<xsl:for-each select="catalog/cd"> <xsl:if test="price &gt; 10"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:if> </xsl:for-each>

Page 36: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Example XSL Style Sheet<?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>Title</th> <th>Artist</th> <th>Price</th>

</tr>

<xsl:for-each select=“catalog/cd“ > <xsl:if test=“price > 10”>

<tr><td><xsl:value-of select="title"/></td><td><xsl:value-of select="artist"/></td><td><xsl:value-of select=“price"/></td>

</tr> <xsl:if />

</xsl:for-each></table>

</body> </html> < /xsl:template>< /xsl:stylesheet>

predicate

HTML to write to the output

Page 37: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Output

Page 38: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Stylesheet Nuts&Bolts

• The <xsl:choose>, <xsl:when>, and <xsl:otherwise>– a multiple conditional test against the XML file, add

<xsl:choose> <xsl:when test="expression"> ... some output ... </xsl:when> <xsl:otherwise> ... some output .... </xsl:otherwise></xsl:choose>

Page 39: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Stylesheet Nuts&Bolts <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <xsl:choose> <xsl:when test="price &gt; 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> </tr> </xsl:for-each>

Page 40: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Output

Page 41: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

<xsl:apply-templates><xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <xsl:apply-templates/> </body> </html></xsl:template>

<xsl:template match="cd"> <p> <xsl:apply-templates select="title"/> <xsl:apply-templates select="artist"/> </p></xsl:template> <xsl:template match="title">

Title: <span style="color:#ff0000"> <xsl:value-of select="."/></span> <br /></xsl:template>

<xsl:template match="artist"> Artist: <span style="color:#00ff00"> <xsl:value-of select="."/></span> <br /></xsl:template>

Page 42: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Output

Page 43: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

References

• W3 Schools– http://www.w3schools.com/

• Online XSLT Test Tool– http://xslttest.appspot.com/

Page 44: IS432 Semi-Structured Data Lecture 5: XSLT Dr. Gamal Al-Shorbagy.

Thanks