xslt (extensible stylesheet language transformation) 1

Post on 14-Dec-2015

227 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

XSLT(eXtensible Stylesheet Language Transformation)

1

XSLT

•XSLT is a language for transforming XML documents into HTML documents or to other XML documents.

•An XSL style sheet is a set of transformation instructions for converting a source XML document to a target document.

2

XMLDocument

XSL StyleSheet 2

Output from Style

Sheet 1

Output fromStyle

Sheet 2

XSL StyleSheet 1

Parser

XSLT Processor

3 3

table.xsl

bar.xsl

art.xsl

XSLT

•Two tree structures are involved in transforming an XML document using XSLT

source tree (usually the XML file) result tree (the result of the transformation either html or

other XML file)

•For XSLT to function, the source (XML) tree must be properly structured.

XML document must be validated against a DTD to make sure the structure is correct.

•XSL documents have the extension .xsl

•All major browsers have support for XML and XSLT.

4

Example

<?xml version="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/xsl" href="studentlist.xsl"?><student_list> <student> <name>Sarah</name> <major>ISC</major> <gpa>3.0</gpa> </student> <student> <name>Fatma</name> <major>AAD</major> <gpa>2.5</gpa> </student> <student> <name>Aseel</name> <major>ISC</major> <gpa>3.3</gpa> </student></student_list> 5

<?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> <table border="1"> <tr bgcolor="#9acd32"> <th>Name</th> <th>Major</th> </tr> <xsl:for-each select="student_list/student"> <tr> <td><xsl:value-of select="name"/></td> <td><xsl:value-of select="major"/></td> </tr> </xsl:for-each> </table> </body> </html></xsl:template></xsl:stylesheet>

XML File XSTL File

Example

6

XML file without XSLT Output after applying the XSLT to the XML file

XSLT Declaration

•The XSTL file begins with the XML declaration<?xml version=“1.0” encoding ="UTF-8"?>    

•The root element XSLT document is <xsl:stylesheet>

•The correct way to declare an XSL style sheet according to the W3C XSLT Recommendation is<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

7

<?xml version="1.0"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"> <html> <body> <table border="1">

.

.</xsl:template></xsl:stylesheet>

Link the XSLT File to the XML Document

The XML document should include the XSLT style sheet reference.

8

<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="studentlist.xsl"?><student_list> <student> <name>Sarah</name> <major>ISC</major> <gpa>3.0</gpa> </student> <student> <name>Fatma</name>

.

.

.

.</student_list>

XSLT - <xsl:template> Element

•The <xsl:template> element is used to build templates.

•The match attribute is used to associate a template with an XML element.

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

9

<?xml version="1.0"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"> <html> <body> <table border="1">

.

.</xsl:template></xsl:stylesheet>

XSLT - <xsl:template> Element

10

<?xml version="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/xsl" href="studentlist.xsl"?><student_list> <student> <name>Sarah</name> <major>ISC</major> <gpa>3.0</gpa> </student> <student> <name>Fatma</name> <major>AAD</major> <gpa>2.5</gpa> </student> <student> <name>Aseel</name> <major>ISC</major> <gpa>3.3</gpa> </student></student_list>

XML File Here the template is associated with the root element <student_list>

<xsl:template match="/">

The XSTL processor will start fromthe root element in the XML file

XSLT - <xsl:value-of> Element

11

<?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> <table border="1"> <tr bgcolor="#9acd32"> <th>Name</th> <th>Major</th> </tr> <tr> <td><xsl:value-of select="student_list/student/name"/></td> <td><xsl:value-of select="student_list/student/major"/></td> </tr></table> </body> </html></xsl:template></xsl:stylesheet>

XSLT - <xsl:value-of> Element

•The XSL <xsl:value-of> element is used to extract the value of an XML element and add it to the output of the transformation.

<xsl:value-of select="student_list/student/name"/><xsl:value-of select="student_list/student/major"/>

•Notice that only one line of data was copied from the XML document to the output.

Usually the first instance of the element is

Selected.12

NOTE: the forward slash (/) is to specify a sub-element (child element)

XSLT - The <xsl:for-each> Element

•The XSL <xsl:for-each> element can be used to select every XML element of a specific type.

13

<xsl:template match="/"> <html> <body>

.

.<xsl:for-each select="student_list/student"> <tr> <td><xsl:value-of select="name"/></td> <td><xsl:value-of select="major"/></td> </tr></xsl:for-each>

.

.</xsl:template></xsl:stylesheet>

Select every <student> element in the XML document (similar to a loop)

For each <student> element print the content of the1. <name> element2. <major> element

XSLT - The <xsl:for-each> Element

14

<?xml version="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/xsl" href="studentlist.xsl"?><student_list> <student> <name>Sarah</name> <major>ISC</major> <gpa>3.0</gpa> </student> <student> <name>Fatma</name> <major>AAD</major> <gpa>2.5</gpa> </student> <student> <name>Aseel</name> <major>ISC</major> <gpa>3.3</gpa> </student></student_list>

XML File

<xsl:for-each select="student_list/student">

<xsl:value-of select="name"/><xsl:value-of select=“major"/>

XSLT- Filtering the Output

•Relational and logical operators can be used in the <xsl:for-each> element to filter the output from the XML file.

•Legal relational operators are: =  (equal to) != (not equal to) &lt; (less than), &lt;= (less than or equal) &gt; (greater than), &gt;= (greater than or equal)

•Logical operators are: and or

15

XSLT- Filtering the Output

•Print the names and GPAs of students whose (GPA > 3.0).

16

<xsl:template match="/"> <html> <body>

.

.<xsl:for-each select="student_list/student[gpa &gt; 3.0]"> <tr> <td><xsl:value-of select=“name"/></td> <td><xsl:value-of select=“gpa"/></td> </tr></xsl:for-each>

.

.</xsl:template></xsl:stylesheet>

XSLT- Filtering the Output Examples

•Print the names and GPAs of students in ISC.

•Print the names, majors, and GPAs, of AAD students and students with gpa greater than or equal to 3.2.

17

top related