xslt aug ’10 – dec ‘10. aug’10 – dec ’10 1 xslt extensible stylesheet language...

56
XSLT XSLT Aug ’10 – Dec ‘10

Upload: elfreda-shanon-simpson

Post on 16-Jan-2016

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

XSLTXSLT

Aug ’10 – Dec ‘10

Page 2: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 1

XSLT

Extensible Stylesheet Language Transformation

Declarative programming language written in XML to convert XML to some other output

Often output is XML or HTML

XSLT uses XPath to select parts of the source XML document that are used in the result document

An XSLT file consists of a number of templates that match specific nodes in the XML being processed.

Page 3: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Need for XSLT

XSLT is a very important XML application in many XML workflows

XSLT is important because the way in which XML is stored needs to be changed before it is used

XML might need to be presented to end users in a convenient format

XSLT plays a key role in converting XML to its presentation formats and restructuring XML

Aug’10 – Dec ’10 2

Page 4: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Uses of XSLT

Aug’10 – Dec ’10 3

Restructuring XML

Restructuring is useful when two companies need to exchange XML documents electronically but have differences in the structure of basic documents

XSLT can copy selected parts of the source XML unchanged into the result document or can create new elements or attributes in the result document.

The names of elements and attributes can be changed.

Elements or attributes present in the source document can be selectively omitted from the result document.

Presenting XML content

XML is often presented as HTML or XHTML

XSLT is used to transform select parts of XML document for display

Page 5: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 4

Accepts an XML document, applies XSLT stylesheet to it and Accepts an XML document, applies XSLT stylesheet to it and produces another document which can be XML, HTML or plain textproduces another document which can be XML, HTML or plain text

Creates an in-memory tree representation of the source document, Creates an in-memory tree representation of the source document, according to the XPath data model called the source treeaccording to the XPath data model called the source tree

XSLT processor processes the source tree according to the XSLT processor processes the source tree according to the templates contained in the stylesheet and result tree is createdtemplates contained in the stylesheet and result tree is created

The creation of result tree from source tree is called The creation of result tree from source tree is called transformationtransformation

After creating result tree, a process called serialization takes placeAfter creating result tree, a process called serialization takes place

Serialization creates a familiar serialized XML document from the Serialization creates a familiar serialized XML document from the result treeresult tree

XSLT Processor

Page 6: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 5

Procedural ProgrammingProcedural Programming

Tell the computer what to do step by stepTell the computer what to do step by step

Define function, assign variable, iterate through loop etc.Define function, assign variable, iterate through loop etc.

Declarative ProgrammingDeclarative Programming

Tell the computer what to achieveTell the computer what to achieve

Resembles SQLResembles SQL

Specify what the XSLT processor is to create when it comes across a Specify what the XSLT processor is to create when it comes across a pattern in the source treepattern in the source tree

XSLT is also a functional language – one that relies on functions to XSLT is also a functional language – one that relies on functions to accept and return dataaccept and return data

Procedural versus Declarative Programming

Page 7: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 6

People.xmlPeople.xml

<People><Person><Name>Winston Churchill</Name><Description>Winston Churchill was a mid 20th century British politician who became famous as Prime Minister during the Second World War.</Description></Person><Person><Name>Indira Gandhi</Name><Description>Indira Gandhi was India’s first female prime minister and was assassinated in 1984.</Description></Person><Person><Name>John F. Kennedy</Name><Description>JFK, as he was affectionately known, was a United States president who was assassinated in Dallas, Texas.</Description></Person> </People>

Foundational XSLT Elements

Page 8: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 7

People.xsltPeople.xslt

<xsl:stylesheetxmlns:xsl=”http://www.w3.org/1999/XSL/Transform”version=”1.0” ><xsl:template match=”/”><html><head><title>Information about<xsl:value-of select=”count(/People/Person)” /> people.</title></head><body><h3>Information about<xsl:value-of select=”count(/People/Person)” /> people.</h3><br /><xsl:apply-templates select=”/People/Person” /></body></html></xsl:template><xsl:template match=”Person”><h3><xsl:value-of select=”Name” /></h3><p><xsl:value-of select=”Description” /></p><br /></xsl:template> </xsl:stylesheet>

Foundational XSLT Elements

Page 9: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 8

People.htmlPeople.html

<html><head><meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8”><title>Information about 3 people.</title></head><body><h3>Information about 3 people.</h3><br><h3>Winston Churchill</h3><p>Winston Churchill was a mid 20th Century British politician who became famous as Prime Minister during the Second World War.</p><br><h3>Indira Gandhi</h3><p>Indira Gandhi was India’s first female prime minister and was assassinated in 1984.</p><br><h3>John F. Kennedy</h3><p>JFK, as he was affectionately known, was a United States President who was assassinated in Dallas, Texas.</p><br></body></html>

Foundational XSLT Elements

Page 10: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 9

Stylesheet, People.xslt, creates a simple HTML web page, Stylesheet, People.xslt, creates a simple HTML web page, People.html, which contains the name and description information People.html, which contains the name and description information about the politiciansabout the politicians

Foundational XSLT Elements

Page 11: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 10

Every XSLT element has as its document element, either an Every XSLT element has as its document element, either an xsl:stylesheet element or an xsl:transform elementxsl:stylesheet element or an xsl:transform element

The xsl:stylesheet element is semantically similar to xsl:transform The xsl:stylesheet element is semantically similar to xsl:transform elementelement

The start tag of the xsl:stylesheet element has a mandatory version The start tag of the xsl:stylesheet element has a mandatory version attributeattribute

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

xsl:stylesheet element must also have a namespace declaration for xsl:stylesheet element must also have a namespace declaration for the XSLT namespacethe XSLT namespace

The XSLT namespace has the URI The XSLT namespace has the URI http://www.w3.org/1999/XSL/Transformhttp://www.w3.org/1999/XSL/Transform..

The <xsl:stylesheet> Element

Page 12: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 11

An XSLT processor looks in a stylesheet for an xsl:template element An XSLT processor looks in a stylesheet for an xsl:template element that has a match attribute with value of /that has a match attribute with value of /

<xsl:template match=”/”><html><head><title>Information about<xsl:value-of select=”count(/People/Person)” /> people.</title></head><body><h3>Information about<xsl:value-of select=”count(/People/Person)” /> people.</h3><br /><xsl:apply-templates select=”/People/Person” /></body></html></xsl:template>

The <xsl:template> Element

Page 13: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 12

Every time XSLT processor finds a node in the source tree that is a Every time XSLT processor finds a node in the source tree that is a root node, the structure corresponding to the content of this template root node, the structure corresponding to the content of this template is added to the result tree.is added to the result tree.

There is only one root node in an XPath model, so the nodes are added There is only one root node in an XPath model, so the nodes are added only once to the result tree.only once to the result tree.

Many of the elements in the template that match the root node are Many of the elements in the template that match the root node are likely to be HTML/XHTML elements. These elements are added to the likely to be HTML/XHTML elements. These elements are added to the result tree literally, and so are called literal result elements. result tree literally, and so are called literal result elements.

The template also contains several elements from the XSLT The template also contains several elements from the XSLT namespace. Those elements are called instructions.namespace. Those elements are called instructions.

A frequentlyA frequently used instruction is the xsl:apply-templates element.used instruction is the xsl:apply-templates element.

The <xsl:template> Element

Page 14: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 13

The xsl:apply-templates element causes the XSLT processor to look for The xsl:apply-templates element causes the XSLT processor to look for matching nodes in the source tree.matching nodes in the source tree.

<xsl:apply-templates select=”/People/Person” />

The nodes to be looked for are specified by the XPath location pathThe nodes to be looked for are specified by the XPath location path

/People/Person, which specifies Person element nodes that are child /People/Person, which specifies Person element nodes that are child nodes of a People element node, which is, in turn, a child node of the nodes of a People element node, which is, in turn, a child node of the root node.root node.

The XSLT processor then looks for a template that matches such a The XSLT processor then looks for a template that matches such a Person element nodePerson element node

Each time the XSLT processor finds a Person element node thatEach time the XSLT processor finds a Person element node that

corresponds to the location path /People/Person, the content of this corresponds to the location path /People/Person, the content of this template is processed and content is added to the result tree.template is processed and content is added to the result tree.

Because three such nodes exist, the content specified by the template Because three such nodes exist, the content specified by the template is added to the result tree three times.is added to the result tree three times.

The <xsl:apply-templates> Element

Page 15: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 14

<xsl:apply-templates select=”/People/Person” />

<People><Person><Name>Winston Churchill</Name><Description>Winston Churchill was a mid 20th century British politician who became famous as Prime Minister during the Second World War.</Description></Person><Person><Name>Indira Gandhi</Name><Description>Indira Gandhi was India’s first female prime minister and was assassinated in 1984.</Description></Person><Person><Name>John F. Kennedy</Name><Description>JFK, as he was affectionately known, was a United States president who was assassinated in Dallas, Texas.</Description></Person> </People>

<xsl:template match=”Person”><h3><xsl:value-of select=”Name” /></h3><p><xsl:value-of select=”Description” /></p><br /></xsl:template>

The <xsl:apply-templates> Element

Page 16: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 15

XSLT provides a number of ways to use information from the source XSLT provides a number of ways to use information from the source tree. tree.

A frequently used XSLT instruction to achieve this is the xsl:value-of A frequently used XSLT instruction to achieve this is the xsl:value-of elementelement

The xsl:value-of elementThe xsl:value-of element

Provides the value of a part of the source tree that represents the Provides the value of a part of the source tree that represents the source XML document.source XML document.

The xsl:value-of element has a mandatory select attribute whose path The xsl:value-of element has a mandatory select attribute whose path is an XPath location path is an XPath location path

Getting information from the source tree

Page 17: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 16

<title>Information about <xsl:value-of select=”count(/People/Person)” /> people.</title>

in the stylesheet is replaced byin the stylesheet is replaced by

<title>Information about 3 people.</title> in the result document

<xsl:template match=”Person”><h3><xsl:value-of select=”Name” /></h3>

<p><xsl:value-of select=”Description” /></p><br /></xsl:template>

The xsl:value-of elements are replaced in the result document by text corresponding, respectively, to the Name element node and the Description element node that are child nodes of the Person elementnode that matches the value of the match attribute of the xsl:template element.

The <xsl:value-of> element

Page 18: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 17

<xsl:template match=”Person”><h3><xsl:value-of select=”Name” /></h3>

<p><xsl:value-of select=”Description” /></p><br /></xsl:template>

The value of the select attribute is the relative location path Name, The value of the select attribute is the relative location path Name, which matches a Name element node that is a child node of the context which matches a Name element node that is a child node of the context nodenode.

When the template that matches the pattern Person is instantiated, When the template that matches the pattern Person is instantiated, the context node is define by the select attribute of the xsl:apply-the context node is define by the select attribute of the xsl:apply-templates elementtemplates element The relative location path Name inThe relative location path Name in

<h3><xsl:value-of select=”Name” /></h3>

Could bewritten as the following absolute location pathCould bewritten as the following absolute location path

/People/Person/Name

The <xsl:value-of> Element

Page 19: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 18

Simplest XSLT element that extracts information from the source treeSimplest XSLT element that extracts information from the source tree

It selects the value of a node-set, which might be only a single node It selects the value of a node-set, which might be only a single node specified by the location path that is the value of the select attribute of specified by the location path that is the value of the select attribute of the xsl:value-of elementthe xsl:value-of element

If there is more than one node in the node set, then the xsl:value-of If there is more than one node in the node set, then the xsl:value-of element uses the value of the first node in document order only, not the element uses the value of the first node in document order only, not the value of all nodesvalue of all nodes

particularly useful when producing output for presentationparticularly useful when producing output for presentation

can also be used when XML is being restructured can also be used when XML is being restructured

The <xsl:value-of> element

Page 20: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 19

Copies a node to the result treeCopies a node to the result tree

It does not copy any descendant nodesIt does not copy any descendant nodes

If the context node is element node, it does cause any attribute nodes If the context node is element node, it does cause any attribute nodes to be copiedto be copied

This can be useful when you want to use an element but change the This can be useful when you want to use an element but change the structure of its content or add or remove attributes from it.structure of its content or add or remove attributes from it.

The <xsl:copy> element

Page 21: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 20

Persons.xml

<Persons><Person><FirstName>Jill</FirstName><LastName>Harper</LastName></Person><Person><FirstName>Claire</FirstName><LastName>Vogue</LastName></Person><Person><FirstName>Paul</FirstName><LastName>Cathedral</LastName></Person></Persons>

The <xsl:copy> element

Page 22: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 21

Persons.xslt

<xsl:stylesheetxmlns:xsl=”http://www.w3.org/1999/XSL/Transform”version=”1.0” ><xsl:template match=”/”><Persons><xsl:apply-templates select=”/Persons/Person” /></Persons></xsl:template><xsl:template match=”Person”><xsl:copy><xsl:attribute name=”FirstName”><xsl:value-of select=”FirstName”/></xsl:attribute><xsl:attribute name=”LastName”><xsl:value-of select=”LastName”/></xsl:attribute></xsl:copy></xsl:template></xsl:stylesheet>

The <xsl:copy> element

Page 23: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 22

The <xsl:copy> element

Page 24: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 23

xsl:copy element is used when the context node is the Person element xsl:copy element is used when the context node is the Person element nodenode

A node that is same as the context node is added to the result treeA node that is same as the context node is added to the result tree

In this example, a Person node is added to the result tree, but its child In this example, a Person node is added to the result tree, but its child nodes FirstName and LastName are not copiednodes FirstName and LastName are not copied

If the serialization is done at this point without adding attribute nodes, If the serialization is done at this point without adding attribute nodes, then the output would look like this:then the output would look like this:

<Persons><Person /><Person /><Person />

</Persons>

The <xsl:copy> element

Page 25: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 24

Reversing the process using xsl:copy elementReversing the process using xsl:copy element

Removes attributes and adds elementsRemoves attributes and adds elements

<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0” ><xsl:template match=”/”><Persons><xsl:apply-templates select=”/Persons/Person” /></Persons></xsl:template><xsl:template match=”Person”><xsl:copy><xsl:element name=”FirstName”><xsl:value-of select=”@FirstName”/></xsl:element><xsl:element name=”LastName”><xsl:value-of select=”@LastName”/></xsl:element></xsl:copy></xsl:template></xsl:stylesheet>

The <xsl:copy> element

Page 26: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 25

Deep copy takes placeDeep copy takes place

A node together with all its attribute nodes and descendant nodes, is A node together with all its attribute nodes and descendant nodes, is copied to the result treecopied to the result tree

The <xsl:copy-of> element

PurchaseOrder.xml

<PurchaseOrder><From>Example.org</From><To>XMML.com</To><Address><Street>234 Any Street</Street><City>Any Town</City><State>MO</State><ZipCode>98765</ZipCode></Address><!-- Other purchase order information would go here. --></PurchaseOrder>

Page 27: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 26

<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0” ><xsl:template match=”/”><Invoice>

<xsl:apply-templates select=”/PurchaseOrder/To” /><xsl:apply-templates select = “/PurchaseOrder/From” /><xsl:apply-templates select=”/PurchaseOrder/Address” />

<xsl:comment>The rest of the Invoice would go here.</xsl:comment></Invoice></xsl:template><xsl:template match=”To”>

<xsl:element name=”From”><xsl:value-of select=”.” /></xsl:element></xsl:template><xsl:template match=”From”>

<xsl:element name=”To”><xsl:value-of select=”.” /></xsl:element></xsl:template><xsl:template match=”Address”>

<xsl:copy-of select=”.” /></xsl:template></xsl:stylesheet>

The <xsl:copy-of> element

Page 28: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 27

OutputOutput

<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?>

<Invoice><Invoice>

<From>XMML.com</From><From>XMML.com</From>

<To>Example.org</To><To>Example.org</To>

<Address><Address>

<Street>234 Any Street</Street><Street>234 Any Street</Street>

<City>Any Town</City><City>Any Town</City>

<State>MO</State><State>MO</State>

<ZipCode>98765</ZipCode><ZipCode>98765</ZipCode>

</Address></Address>

<!--The rest of the Invoice would go here.--><!--The rest of the Invoice would go here.-->

</Invoice></Invoice>

The <xsl:copy-of> element

Page 29: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 28

<xsl:template match=”/”><Invoice>

<xsl:apply-templates select=”/PurchaseOrder/To” /><xsl:apply-templates select = “/PurchaseOrder/From” /><xsl:apply-templates select=”/PurchaseOrder/Address” />

<xsl:comment>The rest of the Invoice would go here.</xsl:comment></Invoice>

<xsl:template match=”To”><xsl:element name=”From”><xsl:value-of select=”.” /></xsl:element>

</xsl:template>

<xsl:template match=”From”><xsl:element name=”To”><xsl:value-of select=”.” /></xsl:element>

</xsl:template>

<xsl:template match=”Address”><xsl:copy-of select=”.” />

</xsl:template>

The <xsl:copy-of> element

Page 30: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 29

XSLT can be used to produce XML, HTML, or text output. XSLT can be used to produce XML, HTML, or text output.

The developer makes a choice among these options by using the The developer makes a choice among these options by using the method attribute of the xsl:output element.method attribute of the xsl:output element.

XML output is the default, and it is not necessary to specify XML as an XML output is the default, and it is not necessary to specify XML as an output method.output method.

To explicitly specify output, To explicitly specify output,

<xsl:output method=”xml” /><xsl:output method=”xml” />

<xsl:output method=”html” /><xsl:output method=”html” />

<xsl:output method=”text” /><xsl:output method=”text” />

In XSLT 1.0, there is no way to specify XHTML output although this has In XSLT 1.0, there is no way to specify XHTML output although this has been added to version 2.0been added to version 2.0

The <xsl:output> element

Page 31: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 30

Tests whether a boolean condition is true or falseTests whether a boolean condition is true or false

If the condition is true, then the content of <xsl:if> is instantiatedIf the condition is true, then the content of <xsl:if> is instantiated

If it is false, then nothing inside the <xsl:if> element is added to the If it is false, then nothing inside the <xsl:if> element is added to the result treeresult tree

The <xsl:if> element

Conditional Processing

<xsl:if>

<xsl:choose>

Page 32: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 31

Characters.xml

<Characters><Character age=”99”>Julius Caesar</Character><Character age=”23”>Anne Boleyn</Character><Character age=”41”>George Washington</Character><Character age=”45”>Martin Luther</Character><Character age=”800”>Methuselah</Character><Character age=”119”>Moses</Character><Character age=”50”>Asterix the Gaul</Character></Characters>

To test whether the age of some characters exceeds the upper age To test whether the age of some characters exceeds the upper age limit, 110limit, 110

The <xsl:if> example

Page 33: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 32

Characters.xslt

<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0” ><xsl:template match=”/”><html><head><title>Age check on Characters.</title></head><body><h3>The recorded age is unusually high. Please check original data.</h3><xsl:apply-templates select=”/Characters/Character” /></body></html></xsl:template><xsl:template match=”Character”><xsl:if test=”@age &gt; 110 “ ><p><b><xsl:value-of select=”.” /></b> is older than expected.Please check if this character’s age, <b><xsl:value-of select=”@age” /></b>, is correct.</p></xsl:if></xsl:template></xsl:stylesheet>

The <xsl:if> example

Page 34: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 33

Output

The <xsl:if> example

Page 35: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 34

Similar to <xsl:if>, but with a few key differencesSimilar to <xsl:if>, but with a few key differences

One xsl:choose element can test for more than one condition and One xsl:choose element can test for more than one condition and add different nodes to the result tree based on which condition is add different nodes to the result tree based on which condition is true.true.

An xsl:choose element can have a default template to add to the An xsl:choose element can have a default template to add to the result tree if none of the conditions are trueresult tree if none of the conditions are true

The xsl:choose element has specific subelements that are The xsl:choose element has specific subelements that are necessary for it to work, while you can put any well-formed necessary for it to work, while you can put any well-formed

elements you want inside of an xsl:if elementelements you want inside of an xsl:if element

The <xsl:choose> element

Conditional Processing

Page 36: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 35

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

The xsl:choose element must contain one or more xsl:when The xsl:choose element must contain one or more xsl:when elements and can contain only one optional xsl:otherwise element elements and can contain only one optional xsl:otherwise element (which must occur after all of the xsl:when elements). (which must occur after all of the xsl:when elements).

If the xsl:choose element only contains one xsl:when element, If the xsl:choose element only contains one xsl:when element, then it behaves just like the xsl:if element. then it behaves just like the xsl:if element.

When faced with three or more choices, this element behaves like When faced with three or more choices, this element behaves like an if-then-else statement (or a Select Case)an if-then-else statement (or a Select Case)

Each xsl:when element is examined in the order of occurrenceEach xsl:when element is examined in the order of occurrence

The <xsl:choose> element

Conditional Processing

Page 37: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 36

If a test expression returns true, the code contained in the element is If a test expression returns true, the code contained in the element is executed and the <xsl:choose> element is exitedexecuted and the <xsl:choose> element is exited

If the xsl:choose element only contains one xsl:when element, then it If the xsl:choose element only contains one xsl:when element, then it behaves just like the xsl:if element behaves just like the xsl:if element

<xsl:choose> example<xsl:choose> example

<poem author="jm" year="1667"> <poem author="jm" year="1667"> <verse>Seest thou yon dreary Plain, forlorn and wild, </verse> <verse>Seest thou yon dreary Plain, forlorn and wild, </verse> <verse>The seat of desolation, void of light,</verse> <verse>The seat of desolation, void of light,</verse>

<verse>Save what the glimmering of these livid flames</verse> <verse>Save what the glimmering of these livid flames</verse> <verse>Casts pale and dreadful?</verse> <verse>Casts pale and dreadful?</verse>

</poem></poem>

The <xsl:choose> element

Conditional Processing

Page 38: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 37

The <xsl:choose> element

<xsl:template match="poem"> <xsl:choose>

<xsl:when test="@year < 1638"> The poem is one of Milton's earlier works.

</xsl:when> <xsl:when test="@year < 1650">

The poem is from Milton's middle period. </xsl:when> <xsl:when test="@year < 1668">

The poem is one of Milton's later works. </xsl:when> <xsl:when test="@year < 1675">

The poem is one of Milton's last works. </xsl:when> <xsl:otherwise>

The poem was written after Milton's death. </xsl:otherwise>

</xsl:choose> </xsl:template>

Page 39: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 38

The <xsl:choose> element

Output

The poem is one of Milton's later works.

Although the poem element's year value of "1667" also makes the last xsl:when element's test expression ("@year &lt; 1675") true, the XSLT processor will not continue checking for more true test expressions after it finds one.

Page 40: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 39

Allows all nodes in a node-set to be processed according to the XSLTAllows all nodes in a node-set to be processed according to the XSLT

instructions nested inside the xsl:for-each elementinstructions nested inside the xsl:for-each element

The <xsl:for-each> element establishes the context for iteration. The <xsl:for-each> element establishes the context for iteration.

The XSLT transformation instructions within this loop are to be applied The XSLT transformation instructions within this loop are to be applied to the selected nodes. to the selected nodes.

Each source element selected by <xsl:for-each> becomes a new Each source element selected by <xsl:for-each> becomes a new context against which any pattern matching within the <xsl:for-each> context against which any pattern matching within the <xsl:for-each> occurs.occurs.

<xsl:for-each <xsl:for-each

select = Expression >select = Expression >

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

The <xsl:for-each> element

Page 41: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 40

Objects.xmlObjects.xml

<?xml version=”1.0”?><?xml version=”1.0”?>

<Objects><Objects>

<Object name=”Car”><Object name=”Car”>

<Characteristic>Hard</Characteristic><Characteristic>Hard</Characteristic>

<Characteristic>Shiny</Characteristic><Characteristic>Shiny</Characteristic>

<Characteristic>Has 4 wheels</Characteristic><Characteristic>Has 4 wheels</Characteristic>

<Characteristic>Internal Combustion Engine</Characteristic><Characteristic>Internal Combustion Engine</Characteristic>

</Object></Object>

</Objects></Objects>

<xsl:for-each> Example

Page 42: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 41

Object.xsltObject.xslt

<?xml version=”1.0”?><?xml version=”1.0”?>

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

<xsl:template match=”/”><xsl:template match=”/”>

<html><html>

<head><head>

<title>Object Characteristics</title><title>Object Characteristics</title>

</head></head>

<body><body>

<h3>Characteristics of <xsl:value-of select=”Objects/Object/@name” /></h3><h3>Characteristics of <xsl:value-of select=”Objects/Object/@name” /></h3>

<xsl:apply-templates select=”/Objects/Object” /><xsl:apply-templates select=”/Objects/Object” />

</body></body>

</html></html>

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

<xsl:template match=”Object”><xsl:template match=”Object”>

<ul><ul>

<xsl:for-each select=”Characteristic”><xsl:for-each select=”Characteristic”>

<li><xsl:value-of select=”.” /></li><li><xsl:value-of select=”.” /></li>

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

</ul></ul>

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

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

<xsl:for-each> Example

Page 43: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 42

OutputOutput

<xsl:for-each> Example

Characteristics of Car

• Hard • Shiny • Has 4 wheels • Internal Combustion Engine

Page 44: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 43

<?xml version="1.0"?> <?xml version="1.0"?>

<?xml-stylesheet href="foreach.xsl" ?> <?xml-stylesheet href="foreach.xsl" ?>

<customers> <customers>

<customer> <customer>

<name>John Smith</name> <name>John Smith</name>

<address>123 Oak St.</address> <address>123 Oak St.</address>

<state>WA</state> <state>WA</state>

<phone>(206) 123-4567</phone> <phone>(206) 123-4567</phone>

</customer> </customer>

<customer> <customer>

<name>Zack Zwyker</name> <name>Zack Zwyker</name>

<address>368 Elm St.</address> <address>368 Elm St.</address>

<state>WA</state> <state>WA</state>

<phone>(206) 423-4537</phone> <phone>(206) 423-4537</phone>

</customer> </customer>

<customer> <customer>

<name>Albert Aikens</name> <name>Albert Aikens</name>

<address>368 Elm St.</address> <address>368 Elm St.</address>

<state>WA</state> <state>WA</state>

<phone>(206) 423-4537</phone> <phone>(206) 423-4537</phone>

</customer> </customer>

</customers></customers>

<xsl:for-each> Example

Page 45: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 44

<?xml version="1.0"?> <?xml version="1.0"?>

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

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

<HTML> <HTML>

<BODY> <BODY>

<TABLE> <TABLE>

<xsl:for-each select="customers/customer"> <xsl:for-each select="customers/customer">

<xsl:sort select="state" order="descending"/> <xsl:sort select="state" order="descending"/> <xsl:sort select="name"/> <xsl:sort select="name"/>

<TR> <TD><xsl:value-of select="name" /></TD> <TR> <TD><xsl:value-of select="name" /></TD> <TD><xsl:value-of select="address" /></TD> <TD><xsl:value-of select="address" /></TD> <TD><xsl:value-of select="phone" /></TD> <TD><xsl:value-of select="phone" /></TD> </TR> </TR>

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

</TABLE> </TABLE>

</BODY></BODY>

</HTML> </HTML>

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

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

<xsl:for-each> Example

Page 46: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 45

OutputOutput

Albert Aikens 368 Elm St. (206) 423-4537 Albert Aikens 368 Elm St. (206) 423-4537

John Smith 123 Oak St. (206) 123-4567John Smith 123 Oak St. (206) 123-4567

Zack Zwyker 368 Elm St. (206) 423-4537Zack Zwyker 368 Elm St. (206) 423-4537

<xsl:for-each> Example

Page 47: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 46

Used to specify sort order for node-setsUsed to specify sort order for node-sets

Defines sort keyDefines sort key

Sort key determines the order in which selected nodes are processedSort key determines the order in which selected nodes are processed

A sort can be based on more than one <xsl:sort> elementA sort can be based on more than one <xsl:sort> element

The xsl:sort element can be used together with the xsl:apply-templates The xsl:sort element can be used together with the xsl:apply-templates element and the xsl:for-each elementelement and the xsl:for-each element

<xsl:sort<xsl:sort  case-order="upper-first" | "lower-first"  case-order="upper-first" | "lower-first"  data-type="number" "qname" | "text"  data-type="number" "qname" | "text"  order="ascending" | " descending"  order="ascending" | " descending"  select="expression"  select="expression">>

</xsl:sort</xsl:sort

The <xsl:sort> element

Page 48: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 47

<?xml version=”1.0”?><Objects><Object name=”Car”><Characteristic>Hard</Characteristic><Characteristic>Shiny</Characteristic><Characteristic>Has 4 wheels</Characteristic><Characteristic>Internal Combustion Engine</Characteristic></Object><Object name=”Orange”><Characteristic>Fruit</Characteristic><Characteristic>Juicy</Characteristic><Characteristic>Dimpled skin</Characteristic><Characteristic>Citrus</Characteristic></Object><Object name=”Giraffe”><Characteristic>Tall</Characteristic><Characteristic>Four legs</Characteristic><Characteristic>Big spots</Characteristic><Characteristic>Mammal</Characteristic></Object><Object name=”Prawn Cracker”><Characteristic>Crisp</Characteristic><Characteristic>Savoury</Characteristic><Characteristic>Off white</Characteristic><Characteristic>Edible</Characteristic></Object></Objects>

The <xsl:sort> element

Page 49: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 48

<?xml version=”1.0”?><xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0” ><xsl:template match=”/”><html><head><title>Object Characteristics</title></head><body><xsl:apply-templates select=”/Objects/Object” ><xsl:sort select=”@name” /></xsl:apply-templates></body></html></xsl:template><xsl:template match=”Object”><h3>Characteristics of <xsl:value-of select=”@name” /></h3><ul><xsl:for-each select=”Characteristic”><xsl:sort select=”.” order=”descending” /><li><xsl:value-of select=”.” /></li></xsl:for-each></ul></xsl:template></xsl:stylesheet>

The <xsl:sort> element

Page 50: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 49

Characteristics of Car• Shiny • Internal Combustion Engine • Has 4 wheels • Hard

Characteristics of Giraffe• Tall • Mammal • Four legs • Big spots

Characteristics of Orange• Juicy • Fruit • Dimpled skin • Citrus

Characteristics of Prawn Cracker• Savoury • Off white • Edible • Crisp

The <xsl:sort> element

Page 51: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 50

Allows variables and parameters to be specified by the xsl:variable and Allows variables and parameters to be specified by the xsl:variable and xsl:param elements, respectively.xsl:param elements, respectively.

Both variables and parameters are referenced using $VariableName or Both variables and parameters are referenced using $VariableName or $ParameterName syntax.$ParameterName syntax.

<?xml version=”1.0”?><?xml version=”1.0”?>

<Ages><Ages>

<Person name=”Peter” age=”21” /><Person name=”Peter” age=”21” />

<Person name=”Angela” age=”12” /><Person name=”Angela” age=”12” />

<Person name=”Augustus” age=”92” /><Person name=”Augustus” age=”92” />

<Person name=”George” age=”44” /><Person name=”George” age=”44” />

<Person name=”Hannah” age=”30” /><Person name=”Hannah” age=”30” />

</Ages></Ages>

XSLT Variables and Parameters

Page 52: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 51

<?xml version=”1.0”?><xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0”><xsl:param name=”person” /><xsl:template match=”/”><html><head><title>Finding an age using an XSLT parameter</title></head><body><xsl:apply-templates select=”/Ages/Person[@name=$person]” /></body></html></xsl:template><xsl:template match=”Person”><p>The age of <xsl:value-of select=”$person” /> is <xsl:value-ofselect=”@age”/> </p></xsl:template></xsl:stylesheet>

XSLT Variables and Parameters

Page 53: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 52

Passing parameter from command linePassing parameter from command line

java -jar saxon8.jar -o Ages.html Ages.xml Ages.xslt person=”Hannah”

<html><head><meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8”><title>Finding an age using an XSLT parameter</title></head><body><p>The age of Hannah is 30</p></body></html>

XSLT Variables and Parameters

Page 54: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 53

XSLT VariablesXSLT Variables

Variables behave the same way as parameters but one differenceVariables behave the same way as parameters but one difference

Parameters can be passed into a transformation from outsideParameters can be passed into a transformation from outside

Variables are defined inside an XSLT stylesheetVariables are defined inside an XSLT stylesheet

Variables can be global or localVariables can be global or local

<xsl:variable name="color" select=“ ‘red’ “ /><xsl:variable name="color" select=“ ‘red’ “ />

<xsl:variable name="color" select=‘ “red” ' /><xsl:variable name="color" select=‘ “red” ' />

XSLT Variables and Parameters

Page 55: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 54

call-template is used to call a template by namecall-template is used to call a template by name

Named templates are identified by a name attributeNamed templates are identified by a name attribute

<xsl:template name=” TemplateName”><!-- The template content goes here. --></xsl:template>

<xsl:call-template name=” TemplateName” />

<xsl:call-template name=” TemplateName” ><xsl:with-param name=” ParameterName” /><!-- More <xsl:with-param> elements can go here. --></xsl:call-template>

<xsl:template name=” TemplateName”><xsl:with-param name=” ParameterName” /><!-- Rest of template goes here. --></xsl:template>

Named Templates and the <xsl:call-template> Element

Page 56: XSLT Aug ’10 – Dec ‘10. Aug’10 – Dec ’10 1 XSLT  Extensible Stylesheet Language Transformation  Declarative programming language written in XML to convert

Aug’10 – Dec ’10 55

document()document()

key()key()

format-number()format-number()

generate-id()generate-id()

XSLT Funtions