34
1 eXtensible Stylesheet Language XSLT Elements Functions XPATH

1 eXtensible Stylesheet Language XSLT Elements Functions XPATH

  • View
    253

  • Download
    1

Embed Size (px)

Citation preview

1

eXtensible Stylesheet Language

XSLTElementsFunctions XPATH

2

XSLXSL

eXtensible Stylesheet Language

Transformation

Structure of XSL

XSLT

XSL Elements

Variable

XPATH

Navigating

XPath Expressions

Templates

<apply-templates-select>

<xsl:apply-templates>

Ex. <apply-templates>

Template in External File

XPATH //

<value-of-select>

<for-each select>

<sort>

<if>

Multiple Conditional tests

<choose>

XPath operators

XPath Functions

count()

sum()

format-number()

{}

XS-FO

XSL Editors

3

XSL = eXtensible Stylesheet XSL = eXtensible Stylesheet LanguageLanguage

The World Wide Web Consortium (W3C) started to develop XSL because there was a need for an XML-based Stylesheet Language.

XSL consists of three parts:1. XSLT - a language for transforming XML documents2. XPath - a language for navigating in XML documents3. XSL-FO - a language for formatting XML documents

4

TransformationTransformation

XSL may be used to generate either XHTML, XML, SVG, SMIL, or text, on browser or printed, oral ….

XSLT Processor= Transformer

XSL

XML Output

5

XSLT = XSLT XSLT = XSLT TransformationTransformation

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.

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.

6

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="xml" media-type="text/html" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" cdata-section-elements="script style" indent="yes" encoding="ISO-8859-1"/><xsl:template match="/"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" > <head> <title> XSL: XSLT element, XPath Expressions and Functions</title> </head> <body>

<h2> In the company <xsl:value-of select="phonebook/@company" /> there are <xsl:value-of select="count(/phonebook/person)"/> persons. </h2>

<xsl:for-each select="phonebook/person"> <xsl:sort order="ascending" select="surname“ /> <xsl:value-of select="surname"/></td> <xsl:choose> <xsl:when test="phone/@confidential='yes'"> <---<br /> </xsl:when> <xsl:otherwise> <xsl:value-of select="phone/homenumber"/><br /> </xsl:otherwise> </xsl:choose> </xsl:for-each>

Structure Structure of XSLof XSL

XSL style sheet is an XML document itself, it begins with the XML declaration: <?xml version="1.0" encoding="ISO-8859-1"?>.

Element <xsl:stylesheet>, defines that this document is an XSLT style sheet document (along with the version number and XSLT namespace attributes).

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.

7

Element Description

apply-templates Applies a template rule to the current element or to the current element's child nodes

attribute Adds an attribute

choose Used in conjunction with <when> and <otherwise> to express multiple conditional tests

comment Creates a comment node in the result tree

copy Creates a copy of the current node (without child nodes and attributes)

copy-of Creates a copy of the current node (with child nodes and attributes)

decimal-format Defines the characters and symbols to be used when converting numbers into strings

element Creates an element node in the output document

for-each Loops through each node in a specified node set

if Contains a template that will be applied only if a specified condition is true

number Determines the integer position of the current node and formats a number

otherwise Specifies a default action for the <choose> element

output Defines the format of the output document

param Declares a local or global parameter

processing-instruction Writes a processing instruction to the output

sort Sorts the output

stylesheet Defines the root element of a style sheet

template Rules to apply when a specified node is matched

text Writes literal text to the output

transform Defines the root element of a style sheet

value-of Extracts the value of a selected node

variable Declares a local or global variable

when Specifies an action for the <choose> element

XSL XSL ElemenElementsts

http://www.w3schools.com/xsl/xsl_w3celementref.asp

8

VariableVariable

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/> <xsl:variable name="pi" select="'3.142857142857'"/> <xsl:template match="/" > <HTML> <HEAD> <TITLE>Value of Pi</TITLE> </HEAD> <BODY> The value of pi is <xsl:value-of select="$pi"/> </BODY> </HTML> </xsl:template>

This could be usefull sometimes even if no9t used in our exacmples.You can create a variable outside of <xsl:template match="/">. Then the variable is global.

9

XSLT Uses XPath

XSLT uses XPath to find information in an XML document.

XPath is used to navigate through elements and attributes in XML documents.

10

Extracting and Extracting and navigatingnavigating•Extracting values with XSLT:

–use the <xsl:value-of select="…"/> XSL element

•Navigating with XPath:

–The slash ("/") indicates parent/child relationship

–A slash at the beginning of the path indicates that it is an absolute path, starting from the top of the XML document

/cdcatalog/cd/title"Start from the top of the XML document, go to the cdcatalog element, from there go to the cd element, and from there go to the title element."

/ Parent – child relationship// Selects nodes in the document from the current node that matches the selection no matter where they are @ Selects attributes

11

XPath ExpressionsXPath Expressions

XPath uses path expressions to select nodes in an XML document. The node is selected by following a path or steps. The most useful path expressions are listed below:

ExpressionDescription

nodename Selects all child nodes of the node

/ Selects from the root node

// Selects nodes in the document from the current node that match the selection no matter where they are

. Selects the current node

.. Selects the parent of the current node

@ Selects attributes

12

TemplatesTemplates

XSLT uses Templates

The <xsl:template> element contains rules to apply when a specified node is matched.

The match attribute is used to associate the template with an XML element. The match attribute can also be used to define a template for a whole branch of the XML document (i.e. match="/" defines the whole document).

13

<xsl:apply-templates <xsl:apply-templates select> select> The <xsl:apply-templates> element applies a template to the current element or to

the current element's child nodes.If we add a select attribute to the <xsl:apply-templates> element it will process only the child element that matches the value of the attribute. We can use the

select attribute to specify the order in which the child nodes are processed. <?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="/catalog"> <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>

</xsl:stylesheet>

14

<xsl:apply-templates> <xsl:apply-templates> Applies a template rule to the current element or to the

current element's child nodes.

15

Ex. Ex. <xsl:app<xsl:apply-ly-templatetemplates> s>

16

Template in External File

** w3c.html<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> <xsl:template name="Validation">

<p> <a href="http://validator.w3.org/"> <img style="border:0;width:88px;height:31px" src="http://validator.w3.org/images/vxhtml10" alt="Valid XHTML 1.0!" /> </a></p>

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

In xx.xsl<xsl:include href="w3c.html" />..<xsl:call-template name="Validation" />

17

<xsl:template match="school">

....

<h1> EVITech </h1>

<h2> Espoo Univercity of Applied Sciences </h2>

<p>

<img alt="EVTEK" src="evtek.jpg"/>

</p>

<xsl:apply-templates select="//unit"/>

</xsl:template>

<xsl:template match="unit"> <xsl:for-each select="text"> <h3><xsl:value-of select="name"/> </h3>

<xsl:for-each select="para"> <p> <xsl:value-of select="content"/> </p> </xsl:for-each> </xsl:for-each> </xsl:template></xsl:stylesheet>

Xpath Xpath ////

See EvtekExtra

18

<xsl:value-of-select><xsl:value-of-select>

Extracts the value of a selected element

<xsl:value-of> Element<xsl: value-of-select=“title”/>

The <xsl:value-of> element can be used to select the value of an XML element and add it to the output stream of the transformation.

or attribute

<xsl:value-of select="catalog/@music" />

19

<xsl:for-each select><xsl:for-each 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>

Loops through each node in a specified node set.The value of the select attribute is an XPath expression. An XPath expression works like navigating a file system; where a forward slash (/) selects subdirectories.

20

<xsl:sort> with <xsl:for-<xsl:sort> with <xsl:for-each>each>

<xsl:for-each select="catalog/cd"> <!-- NOTE THAT SORTING IS DONE BY A NUMBER --> <!-- The data type must be specified number -->

<!– Two keys given to sorting: first price, second title--> <xsl:sort data-type="number" order="descending" select="ageLimit" /> <xsl:sort order="ascending" select="title" / <tr bgcolor="#bbbbbb"> <td align="center"><xsl:value-of select="title"/></td> <td align="center"><xsl:value-of select="artist"/></td> <td align="center"><xsl:value-of select="country"/></td> <td align="center"><xsl:value-of select="company"/></td> <td align="center"><xsl:value-of select="price"/></td> </tr> </xsl:for-each>

Sorts the output.

21

<xls:sort> with <xsl:apply-templates><table>

<xsl:apply-templates select="student" > <xsl:sort order="ascending"

select="surname" /> <xsl:sort order="ascending"

select="firstname" /> </xsl:apply-templates> </table>

22

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

<!-- To put a conditional if test against the content of the file, add an <xsl:if> element -->

<!-- The value of the required test attribute contains the expression to be evaluated.-->

<xsl:if test="price&gt;'9'">

<tr bgcolor="#bbbbbb">

<td align="center"><xsl:value-of select="title"/></td>

<td align="center"><xsl:value-of select="artist"/></td>

<td align="center"><xsl:value-of select="country"/></td>

<td align="center"><xsl:value-of select="company"/></td>

</tr>

</xsl:if>

</xsl:for-each>

<xsl:if><xsl:if>

• express single conditional tests:

Contains a template that will be applied only if a specified condition is true

23

Multiple conditional Multiple conditional teststests

• express multiple conditional tests:

<xsl:choose> <xsl:when test='something> [action] </xsl:when> <xsl:when test='something'> [action] </xsl:when> <xsl:otherwise> [action] </xsl:otherwise></xsl:choose>

The first xsl:when statement thatevaluates to true is executed. Ifnone evaluates to true then thexsl:otherwise statement is executed.

Used in conjunction with <when> and <otherwise> to express multiple conditional tests

24

<xsl:choose><xsl:choose>

xsl:for-each select="catalog/cd"> <tr bgcolor="#00ffff"> <xsl:choose> <xsl:when test="price&lt;'9' and grade&gt;='7'">

<td align="center" bgcolor="#11ff11"> <xsl:value-of select="title"/></td> </xsl:when> <xsl:otherwise>

<td align="center"><xsl:value-of select="title"/></td> </xsl:otherwise> </xsl:choose>

<td align="center"><xsl:value-of select="artist"/></td> <xsl:choose> ……..

25

XPath OperatorsXPath OperatorsDescription Example Return value

+ Addition 6 + 4 10

- Subtraction 6 - 4 2

* Multiplication 6 * 4 24

div Division 8 div 4 2

= Equal price=9.80 true if price is 9.80

!= Not equal price!=9.80 true if price is 9.90

< Less than price<9.80 true if price is 9.00

<= Less than or equal to price<=9.80 true if price is 9.00

> Greater than price>9.80 true if price is 9.90

>= Greater than or equal to price>=9.80 true if price is 9.90

or or price=9.80 or price=9.70

true if price is 9.80

and and price>9.00 and price<9.90

true if price is 9.80

mod Modulus (division remainder)

5 mod 2 1

< > = <= >= !=Must be written as&lt; &gt; = &lt;= &gt;= !=&lt; &gt; = &lt;= &gt;= !=

26

XSLT functionsXSLT functions1. Functions with Numeric ValuesAbsolute value abs(-3.14) Result: 3.14

Returns the smallest integer that is greaterceiling(3.14) Result: 4

Returns the largest integer that is not greater floor(3.14) Result: 3

Rounds to the nearest integerround(3.14) Result: 3

round-half-to-even(0.5) Result: 0 round-half-to-even(1.5) Result: 2round-half-to-even(2.5) Result: 2

3. Functions on Strings

4. Functions on Booleans

2. Functions on Durations, Dates and Timeyear-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10"))Result:

day-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10"))Result: 10 ….See in more details: http://www.w3schools.com/xpath/xpath_functions.asp

5. Functions on Errors

6. Functions on Sequences

7. Functions on Nodes...

27

count()count()

count(set of node) returns an integer representing the numberof nodes (i.e., XML elements) in the set.

Example.

Number of cds = <xsl:value-of select="count(/cdcatalog/cd)"/>

28

sum()sum()

sum(set of node) returns an idecimal point number representing the sum of nodes (i.e., XML elements) in the set.

Example where sum is converted to integer:

There are <xsl:value-of select="count(/catalog/cd)"/>cds in this catalog. The price of the whole catalog is <xsl:value-of select = "round(sum(/catalog/cd/price))"/> ecus.

29

format-number()format-number()

Average price is calculated by summing all the prices and dividing the result by the number of cds. The output is formatted to two digits after decimal point.

Average price of a cd is

<xsl:value-of select="format-number(sum(/catalog/cd/price) div count(/catalog/cd),

'0.00')"/>

ecus.

30

{ } = value-of inside { } = value-of inside bracketsbrackets

<img src="{course/logo}" alt="Metropolia"/>

Value of an attribute (” ”) in xsl is written inside { }

31

Mod() <xsl:element> <xsl:for-each select="//title"><xsl:element name="tr"> <xsl:choose>

<xsl:when test="position() mod 2 = 0"> <xsl:attribute name="class">blue</xsl:attribute></xsl:when><xsl:otherwise>

<xsl:attribute name="class">red</xsl:attribute></xsl:otherwise></xsl:choose>

<td><xsl:value-of select="../artist"/></td> … <td><xsl:value-of select="position()"/></td> <td><xsl:value-of select="(position() mod 2)"/></td> </xsl:element> </xsl:for-each> </table>

css.blue {background-color:#3399FF;}.red {background-color:#FF6666;}

32

<xsl:element>The <xsl:element> element is used to create an element node

in the output document.

Exc. Create a "singer" element that contains the value of each artist element:

<?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="/">  <xsl:for-each select="catalog/cd">    <xsl:element name="singer">      <xsl:value-of select="artist" />    </xsl:element>    <br />  </xsl:for-each></xsl:template>

</xsl:stylesheet>

33

<xsl:attribute> Element

The <xsl:attribute> element is used to add attributes to

elements.

Exc. Add a source attribute to the picture element and fill it with values from "images/name" :

<picture>  <xsl:attribute name="source">    <xsl:value-of select="images/name" />  </xsl:attribute></picture>

34

Altova Visual Stylesheet Altova Visual Stylesheet DesignerDesigner