xslt. xsl comprises of –xslt: is a language for transforming xml documents into other xml...

148
XSLT

Upload: daniel-lawrence

Post on 30-Dec-2015

257 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT

Page 2: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT• XSL comprises of

– XSLT: Is a language for transforming XML documents into other XML documents

– FO: XML vocabulary for specifying formatting

• XSL specifies the styling of an XML document by using:– XSLT to describe how the document is transformed

into another XML document– The transformed XML document uses the formatting

vocabulary

Page 3: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT X’formations• A transformation in the XSLT language is

expressed as a well-formed XML document

• A transformation may include both:– Elements that are defined by XSLT elementsXSLT-defined elements belong to XSLT Namespace

– Elements that are not defined by XSLT

Page 4: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT X’formations• A style sheet contains a set of template rules• A template rule has two parts:

– Pattern which is matched against nodes in the source tree (uses XPath here)

– Template which can be instantiated to form part of the result tree

• Style sheets can be applied to wide class of documents that have similar source tree structures

Page 5: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT X’formation Step 1• XSLT transformations describe rules for

transforming source tree into a result tree

• Transformation is achieved by associating patterns with templates

• Result tree is constructed by finding template rule for root node, instantiating its template

Page 6: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT X’formation Step 2• Root Node template:

– Can contain elements that specify literal result element structure

– Can also contain elements from the XSLT namespace that are instructions for creating result tree fragments

Page 7: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT X’formation Step 2

• Instructions can select and process descendant source elements

• Processing a descendant element creates a result tree fragment by finding the applicable template rule and instantiating its template

• Elements are only processed when they have been selected by the execution of an instruction

Page 8: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT X’formation Step 3• A template is instantiated to create part of the result tree

• A template is always instantiated with respect to a current node and a current node list– Only a few instructions change the current node list or the

current node

• When a template is instantiated, each instruction is executed and replaced by the result tree fragment that it creates

• The result tree is separate from the source tree

Page 9: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT X’formation Step 3

• The structure of the result tree can be completely different from the structure of the source tree

Page 10: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT X’formation Step 3

• In constructing the result tree, elements from the source tree can be filtered and reordered, and arbitrary structure can be added

Page 11: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Example

Where the result tree structure is

independent of source tree structure, a style

sheet can often consist of only a single

template

Page 12: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Example – Input Data<sales> <division id="North"> <revenue>10</revenue> <growth>9</growth> <bonus>7</bonus> </division> <division id="South"> <revenue>4</revenue> <growth>3</growth> <bonus>4</bonus> </division></sales>

Page 13: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Example – Style Sheet<xsl:stylesheet version="1.0“ xmlns:xsl=

http://www.w3.org/1999/XSL/Transform><xsl:template match="/">

<html>

<head><title>Sample</title></head>

<body>

<xsl:for-each select="sales/division"> <B><xsl:value-of select=“@id” /> </xsl:for-each> </body> </html>

</xsl:template>

Page 14: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Example – Output<html>

<head><title>Sample</title></head>

<body>

<B>North</B>

<B>South</B>

</body>

</html>

Page 15: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Extensibility

• XSLT provides two "hooks" for extending the language:– Extending the set of instruction elements used

in templates– Extending the set of functions used in XPath

expressions

Page 16: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Structure

Page 17: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Name Space• XSLT namespace has the URI

http://www.w3.org/1999/XSL/Transform

• Prefix of xsl: is used for referring to elements in the XSLT namespace

• Any extension of additional elements, attributes must be in a separate namespace

Page 18: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Style Sheet Element• Stylesheet is represented by an

xsl:stylesheet element in an XML document

• Must have a version attribute, indicating the version of XSLT

• An element occurring as child of an xsl:stylesheet element is called a top-level element

Page 19: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Literal Result Element• Simplified syntax for style sheets

consisting of only single template for root node

• Such style sheets may consist of just a literal result element

Page 20: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Example – Style Sheet<xsl:stylesheet version="1.0“xmlns:xsl=http://www.w3.org/1999/XSL/Transformxmlns="http://www.w3.org/TR/xhtml1/strict">

<xsl:template match="/"> <html> <head><title>Expense Report Summary</title> </head> <body> <p>Total Amount: <xsl:value-of

select="expense-report/total"/></p> </body> </html> </xsl:template></xsl:stylesheet>

Page 21: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Example – Simplified<html xsl:version="1.0" xmlns:xsl=

http://www.w3.org/1999/XSL/Transformxmlns="http://www.w3.org/TR/xhtml1/strict"> <head> <title>Expense Report Summary</title></head> <body><p>Total Amount: <xsl:value-of select="expense-

report/total"/></p> </body> </html>

Page 22: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Literal Result Element

• Literal result element that is the document element of a style sheet must have an xsl:version attribute

• Literal result element used as a style sheet cannot contain top-level elements

Page 23: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Forward Compatible Processing• Forwards-compatible mode enabled

– If xsl:stylesheet element version attribute is not equal to 1.0

– If xsl:stylesheet is a literal result element that has xsl:version attribute whose value is not equal to 1.0

– If xsl:stylesheet is a literal result element that does not have an xsl:version attribute and that is the document element of a stylesheet using the simplified syntax

Page 24: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Forward Compatible Processing• If an element is processed in forwards-

compatible mode:– If it is top-level element & no XSLT 1.0 support, then

element ignored– If it is element in a template & no XSLT 1.0 support,

then:• if element not instantiated, no error signaled• if element instantiated, the XSLT must perform fallback

– if the element has attribute that XSLT 1.0 does not support, attribute must be ignored

Page 25: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Forward Compatible Processing

• What if style sheet depends crucially on a top-level element introduced by a version of XSL after 1.0?

• Stylesheet can use xsl:message element with terminate="yes" to ensure XSLT processors implementing earlier versions of XSL will not silently ignore the top-level element

Page 26: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Forward Compatible Processing<xsl:stylesheet version="1.5“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:important-new-1.1-declaration/> <xsl:template match="/"> <xsl:choose> <xsl:when test="system-property('xsl:version') &lt; 1.1"> <xsl:message terminate="yes"> <xsl:text>Sorry, this stylesheet requires XSLT 1.1.</xsl:text> </xsl:message> </xsl:when> </xsl:choose> </xsl:template> ...

</xsl:stylesheet>

Page 27: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Expressions• XSLT uses the expression language defined by

XPath • Expressions are used in XSLT for

– selecting nodes for processing– specifying conditions for different ways of processing

a node– generating text to be inserted in the result tree

• An expression must match the XPath production Expr

Page 28: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Expressions• Occur as value of certain attributes on:

– XSLT-defined elements – Within curly braces in attribute value

templates

Page 29: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Processing Model• A list of source nodes is processed to create a

result tree fragment

• The result tree is constructed by processing a list containing just the root node

• A list of source nodes is processed by appending the result tree structure created by processing each of the members of the list in order

Page 30: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Processing Model• A node is processed by finding all the template

rules with patterns that match the node, and choosing the best amongst them

• The chosen rule's template is then instantiated with the node as the current node and with the list of source nodes as the current node list

• A template typically contains instructions that select an additional list of source nodes for processing

Page 31: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Processing Model

• The process of matching, instantiation and selection is continued recursively until no new source nodes are selected for processing

Page 32: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Patterns• Template rules identify the nodes to which they

apply by using a pattern

• Specifies a set of conditions on a node

• A node that satisfies the conditions matches the pattern

• Patterns are also used for:– Numbering – Declaring keys

Page 33: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Pattern Syntax• Syntax for patterns is a subset of the

syntax for expressions

• Location paths that meet certain restrictions can be used as patterns

• An expression that is also a pattern always evaluates to an object of type node-set.

Page 34: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Pattern Syntax• A Pattern is a set of location path patterns

separated by |

• Location path pattern is a location path whose steps all use only the child or attribute axes

• Patterns may use the // operator as well as the / operator

• Location path patterns can also start with an id or key function call with a literal argument

Page 35: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Pattern Syntax

• Predicates in a pattern can use arbitrary expressions just like predicates in a location path

• A pattern with one or more | separated alternatives matches if any one of the alternative matches

Page 36: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Pattern Syntax• A pattern that consists of a sequence of

Step Patterns separated by / or // is matched from right to left

– Such a pattern only matches if the rightmost StepPattern matches and a suitable element matches the rest of the pattern

Page 37: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Pattern Syntax• When [] is present:

– Then the first PredicateExpr in a Pattern is evaluated with the node being matched as the context node and the siblings of the context node that match the NodeTest as the context node list

– If the node being matched is an attribute node, then the context node list is all the attributes that have the same parent as the attribute being matched and that match the NameTest

Page 38: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Pattern Syntax - Exampleappendix//ulist/item[position()=1]

This matches if and only if:• Current node is an item element

• Evaluating the PredicateExpr position()=1 with the node as context node and the siblings of the node that are item elements as the context node list yields true

• Current Node has a parent that matches appendix//ulist; this will be true if the parent is a ulist element that has an appendix ancestor element

Page 39: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Patterns - ExerciseWhat do the following patterns stand for?

• Para, * , chapter|appendix • appendix//para• text(), /• node(), id("W11")• *[position()=1 and self::para]• para[last()=1], para[last()=1]• items/item[position()>1] • item[position() mod 2 = 1] • @*

Page 40: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Defining Template Rules• Template rule is specified with the xsl:template

element

• match attribute is a Pattern that identifies the source node or nodes to which the rule applies

• match attribute is required unless the xsl:template element has a name attribute (Named Templates)

• Content of the xsl:template element is the template that is instantiated when the template rule is applied.

Page 41: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Template Rules - Example

<xsl:template match="emph"> <fo:inline-sequence font-weight="bold"> <xsl:apply-templates/> </fo:inline-sequence></xsl:template>

Page 42: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Named Templates• Templates can be invoked by name

• An xsl:template element with a name attribute specifies a named template

• If xsl:template element has name attribute, it may, but need not, also have match attribute

• An xsl:call-template invokes a template by name– Has required name attribute that identifies the template to be

invoked

Page 43: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Applying Template Rules• Processes all of the children of the current

node• select attribute used to process nodes

selected by expression instead of processing all children

– select expression must evaluate to a node-set– selected set of nodes is processed in document

order unless sorting specified

Page 44: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Applying Template Rules Eg.<xsl:template match="chapter"> <fo:block> <xsl:apply-templates/> </fo:block></xsl:template>

<xsl:template match="author-group"><fo:inline-sequence> <xsl:apply-templates select="author"/></fo:inline-sequence></xsl:template>

Page 45: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Applying Template Rules Eg.<xsl:template match="employee"> <fo:block> Employee <xsl:apply-templates select="name"/>belongs to group <xsl:apply-templates select="ancestor::department/group"/> </fo:block></xsl:template>

<xsl:template match="product"> <table> <xsl:apply-templates select="sales/domestic"/> </table> <table> <xsl:apply-templates select="sales/foreign"/> </table></xsl:template>

Page 46: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Applying Template Rules Eg.Anything funny in this?

<xsl:template match="foo">

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

</xsl:template>

Page 47: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Exercise 1• Exercise on

– Defining Template Rules– Applying Template Rules

Page 48: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Built In Template Rules• To allow recursive processing to continue in the

absence of a successful pattern match by an explicit template rule in the style sheet

<xsl:template match="*|/"> <xsl:apply-templates/></xsl:template>

<xsl:template match="text()|@*"> <xsl:value-of select="."/></xsl:template>

<xsl:template match="processing-instruction()|comment()"/>

Page 49: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Result Tree

This section describes instructions that directly

create nodes in the result tree

Page 50: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Literal Result Elements• In a template, an element that does not belong to the

XSLT namespace and that is not an extension element is instantiated to create an element node with the same name

• The content of the element is a template, which is instantiated to give the content of the created element node

• The created element node will have the attribute nodes that were present on the element node in the stylesheet tree

Page 51: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Literal Result Elements• Attribute value of a literal result element is

interpreted as an attribute value template

• Attribute value can contain expressions contained in curly braces ({})

Page 52: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Exercise 2• Exercise on

– Literal Result Element

Page 53: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Name Space Alias• Namespace URI in style sheet tree that is used to

specify a namespace URI in the result tree is called a literal namespace URI

• xsl:namespace-alias element to declare that one namespace URI is an alias for another namespace URI

• stylesheet-prefix attribute specifies the namespace URI that will appear in the style sheet

• result-prefix attribute specifies the corresponding namespace URI that will appear in the result tree

• Default namespace (as declared by xmlns) may be specified by using #default

Page 54: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Name Space AliasWhen literal result elements are being used tocreate element, attribute, or namespace nodesthat use the XSLT namespace URI, the style sheetmust use an alias<xsl:stylesheet version="1.0“xmlns:xsl=http://www.w3.org/1999/XSL/Transformxmlns:fo=http://www.w3.org/1999/XSL/Formatxmlns:axsl="http://www.w3.org/1999/XSL/TransformAlias">

<xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>

<xsl:template match="/"> <axsl:stylesheet> <xsl:apply-templates/> </axsl:stylesheet></xsl:template> </xsl:stylesheet>

Page 55: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Exercise 3• Exercise on

– Name Space Alias

Page 56: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Creating Elements• xsl:element allows an element to be created with a

computed name

• Name of element to be created is specified by a required name attribute, optional namespace attribute

• Content of xsl:element is a template for the attributes and children of the created element

• name, namespace attribute is interpreted as an attribute value template

Page 57: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Creating Attributes• xsl:attribute element used to add attributes to result

elements

• Attribute name is specified by a required name, optional namespace attribute

• Instantiating an xsl:attribute adds an attribute node to the containing result element node

• Content of the xsl:attribute element is a template for the value of the created attribute

• name, namespace attribute is interpreted as an attribute value template

Page 58: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Creating Attributes• The following are all errors

– Adding an attribute to an element after children have been added to it

– Adding an attribute to a node that is not an element

– Creating nodes other than text nodes during the instantiation of the content of the xsl:attribute element

Page 59: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Exercise 4• Exercise on

– Creating elements using <xsl:element>– Creating attributes using <xsl:attribute>

Page 60: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Named Attribute Sets• xsl:attribute-set defines a named set of attributes

• name attribute specifies the name of the attribute set

• xsl:attribute-set consists of zero or more xsl:attribute elements that specify the attributes in the set

• Attribute sets are used by specifying use-attribute-sets attribute on xsl:element, xsl:copy, xsl:attribute-set elements

Page 61: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Named Attribute Sets• Value of the use-attribute-sets attribute is a

whitespace-separated list of names of attribute sets

• Specifying use-attribute-sets attribute is equivalent to adding xsl:attribute for each of the attributes in each of the named attribute sets to the beginning of the content of the element with the use-attribute-sets attribute

• Attribute sets can also be used by specifying an xsl:use-attribute-sets attribute on a literal result element

Page 62: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Named Attribute Sets

• Attributes of attribute sets can be overridden by attributes on the literal result element

• Each time attribute set is used, template within each xsl:attribute element in an xsl:attribute-set element is instantiated

Page 63: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Named Attribute Sets

<xsl:template match="chapter/heading"> <fo:block quadding="start" xsl:use-attribute-sets="title-

style"> <xsl:apply-templates/> </fo:block></xsl:template>

<xsl:attribute-set name="title-style"> <xsl:attribute name="font-size">12pt</xsl:attribute> <xsl:attribute name="font-weight">bold</xsl:attribute></xsl:attribute-set>

Page 64: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Exercise 5• Exercise on

– Creating attribute sets using <xsl:atribute-set>

Page 65: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Creating Text• Template can also contain text nodes

• Each text node in a template remaining after whitespace stripping will create a text node with the same string-value in the result tree

• Adjacent text nodes in the result tree are automatically merged

• Literal data characters may also be wrapped in an xsl:text element

Page 66: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Creating PIs• xsl:processing-instruction is instantiated to create a

processing instruction node

• Instruction content of xsl:processing-instruction is template for the string-value of the processing instruction node

• Instruction requires name attribute to specify name of processing instruction

• Value of name attribute is interpreted as attribute value template

Page 67: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Creating PIs<xsl:processing-instruction name="xml- stylesheet"> href="book.css“type="text/css“</xsl:processing-instruction>

would result in

<?xml-stylesheet href="book.css" type="text/css"?>

Page 68: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Creating Comments• xsl:comment element is instantiated to create a

comment node in the result tree

• Content of xsl:comment is template for the string-value of the comment node

<xsl:comment>This file is automatically generated. Donot edit!</xsl:comment>

would create

<!--This file is automatically generated. Do not edit!-->

Page 69: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Exercise 6• Exercise on

– Creating text using <xsl:text>– Creating comments using <xsl:comment>– Creating PI using <xsl:processing-instruction>

Page 70: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Copying• xsl:copy provides an easy way of copying the current node

• Instantiating xsl:copy creates a copy of the current node

• Attributes, children of node are not automatically copied

• Content of xsl:copy is template for attributes, children of created node

• Content is instantiated only for nodes of types that can have attributes or children

– Root nodes, Element nodes

Page 71: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Copying• xsl:copy element may have use-attribute-sets attribute

– is used only when copying element nodes

• When current node = root node, xsl:copy will not create root node, but will use content template

<xsl:template name="apply-templates-copy-lang"> <xsl:for-each select="@xml:lang"> <xsl:copy/> </xsl:for-each> <xsl:apply-templates/></xsl:template>

Page 72: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Exercise 7• Exercise on

– Copying using <xsl:copy>

Could do this exercise with exercise 21 at

end

Page 73: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Computed Generated Text• xsl:value-of is instantiated to create a text node in the result

tree

• The required select attribute is an expression– Expression is evaluated, resulting object is converted to a string – The string specifies the string-value of the created text node

<xsl:template match="person"><p> <xsl:value-of select="@given-name"/><xsl:text>

</xsl:text> <xsl:value-of select="@family-name"/></p></xsl:template>

Page 74: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Exercise 8• Exercise on

– Inserting data using <xsl:value-of>

Page 75: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Attribute Value Templates• Attribute value can be interpreted as an attribute value

template

• In such an attribute value, expression can be used by surrounding it with curly braces ({})

• Attribute value template is instantiated by replacing the expression by result of evaluating the expression and converting the resulting object to a string

• Curly braces are not recognized in an attribute value unless the attribute is specifically stated to be an attribute value template

Page 76: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Attribute Value TemplatesSource:<photograph> <href>headquarters.jpg</href> <size width="300"/></photograph>

Style Sheet:<xsl:variable name="image-dir">/images</xsl:variable><xsl:template match="photograph"> <img src="{$image-dir}/{href}“ width="{size/@width}"/></xsl:template>

Result:<img src="/images/headquarters.jpg" width="300"/>

Page 77: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Exercise 9• Exercise on

– Identifying Attribute Value Templates

Page 78: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Repetition• When result has known regular structure, it is

useful to be able to specify directly the template for selected nodes

• xsl:for-each instruction contains a template, which is instantiated for each node selected by the expression specified by the select attribute

• select attribute is required, expression must evaluate to a node-set

Page 79: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Repetition• Template is instantiated with selected node as

the current node, list of all selected nodes as the current node list

• The nodes are processed in document order, unless a sorting specification is present

Page 80: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Repetition<customers><customer><name>...</name> <order>...</order></customer></customers> ……<xsl:template match="/"><html> <head><title>Customers</title></head><body><table> <tbody><xsl:for-each select="customers/customer"><tr><th><xsl:apply-templates select="name"/></th><xsl:for-each select="order"> <td><xsl:apply-templates/></td></xsl:for-each></tr></xsl:for-each></tbody></table></body></html></xsl:template>

Page 81: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Exercise 10• Exercise on

– Producing repeatable, regular result using <xsl:for-each>

Page 82: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Numbering• xsl:number element is used to insert a formatted number

into the result tree

• value attribute contains an expression which specifies number to be inserted

• level attribute specifies levels of source tree to consider: single (default), multiple, any

• count attribute is a pattern specifying nodes to be counted at those levels

• from attribute is a pattern that specifies where counting starts

Page 83: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Numbering• format attribute is used for formatting sequence of numbers

produced

– format token 1 generates the sequence 1 2 ... 10 11 12 ...,

– format token 01 generates the sequence 01 02 ... 09 10 11 12 ... 99 100 101

– format token A generates the sequence A B C ... Z AA AB

– format token a generates the sequence a b c ... z aa ab ac....

– format token i generates the sequence i ii iii iv v vi vii viii ix x

– format token I generates the sequence I II III IV V VI VII VIII IX X

Page 84: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

NumberingWhen level="single“:

• Goes up to first node in ancestor-or-self axis that matches the count pattern

• Constructs a list of length one containing one plus the number of preceding siblings of that ancestor that match the count pattern

• If the from attribute is specified, then the only ancestors that are searched are those that are descendants of the nearest ancestor that matches the from pattern

Page 85: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

NumberingWhen level="multiple“:

• Constructs a list of all ancestors of the current node in document order followed by the element itself

• Selects from the list those nodes that match the count pattern

• Maps each node in the list to one plus the number of preceding siblings of that node that match the count pattern

• If from attribute specified, only ancestors searched are those that are descendants of the nearest ancestor that matches the from pattern

Page 86: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

NumberingWhen level="any“• Constructs a list of length one containing the number

of nodes that match the count pattern

• It involves a set containing the current node and all nodes at any level of the document that are before the current node in document order

• If from attribute is specified, only nodes after the first node before the current node that match the from pattern are considered

Page 87: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Numbering• Following would number the items in an ordered list:

<xsl:template match="ol/item"> <fo:block> <xsl:number/> <xsl:text>. </xsl:text> <xsl:apply-templates/> </fo:block><xsl:template>

Page 88: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

NumberingFollowing two rules would number title elements.

– Chapters are numbered 1, 2, 3– Sections in chapters are numbered 1.1, 1.2, 1.3;

<xsl:template match="title"><fo:block><xsl:number level="multiple" count="chapter|

section|subsection“format="1.1 "/><xsl:apply-templates/></fo:block></xsl:template>

Page 89: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

NumberingFollowing example numbers notes sequentially

within a chapter:

<xsl:template match="note"> <fo:block> <xsl:number level="any" from="chapter"

format="(1) "/> <xsl:apply-templates/> </fo:block></xsl:template>

Page 90: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Exercise 11• Exercise on

– Numbering repeatable, regular result using <xsl:number>

Page 91: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Conditional Processing xsl:if• 2 instructions supporting conditional processing in template:

– xsl:if: provides simple if-then conditionality– xsl:choose: supports selection of one choice when there are

several possibilities

• xsl:if element has a test attribute, which specifies an expression

• Content of xsl:if is a template

• test expression is evaluated, resulting object is converted to boolean

• If the result is true, then the content template is instantiated; otherwise, nothing is created

Page 92: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Conditional Processing xsl:if

In the following, the names in a group of

names are formatted as a comma separated list:

<xsl:template match="namelist/name"> <xsl:apply-templates/> <xsl:if test="not(position()=last())">,

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

Page 93: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Exercise 12• Exercise on

– Conditional Processing using <xsl:if>

Page 94: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Condnl. Processing xsl:choose• xsl:choose element selects one among a number of possible

alternatives

• Consists of a sequence of xsl:when elements followed by an optional xsl:otherwise element

• Each xsl:when element has a single attribute, test, which specifies an expression

• The content of the xsl:when , xsl:otherwise elements is a template

• When xsl:choose element is processed, each of xsl:when elements is tested

Page 95: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Condnl. Processing xsl:choose

• The content of the first, and only the first, xsl:when element whose test is true is instantiated

• If no xsl:when is true, the content of the xsl:otherwise element is instantiated

Page 96: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Condnl. Processing xsl:chooseFollowing example enumerates items in anordered list depending on depth to whichordered lists are nested

<xsl:template match="orderedlist/listitem"> <xsl:variable name="level" select="count(ancestor::orderedlist) mod 3"/> <xsl:choose> <xsl:when test='$level=1'> <xsl:number format="i"/> </xsl:when> <xsl:when test='$level=2'><xsl:number format="a"/> </xsl:when> <xsl:otherwise><xsl:number format="1"/> </xsl:otherwise> </xsl:choose><xsl:text>. </xsl:text><xsl:apply-templates/></xsl:template>

Page 97: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Exercise 13• Exercise on

– Conditional Processing using <xsl:choose>, <xsl:variable>

Page 98: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Sorting• Sorting is specified by adding xsl:sort• xsl:sort added as children of

– xsl:apply-templates – xsl:for-each

• When xsl:sort is instantiated by xsl:apply-templates or xsl:for-each, the current node list consists of the complete list of nodes being processed in sorted order

Page 99: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Sorting• xsl:sort has a select attribute whose value is an

expression

• select expression is evaluated with current node, complete list of nodes being processed in unsorted order as the current node list

• Resulting object is converted to string, used as the sort key for that node

• Default value of the select attribute is .– Causes string-value of the current node to be used as the sort

key

Page 100: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Sorting• order attribute specifies order of strings to be

sorted (ascending [default], descending order)

• data-type attribute specifies the data type of the strings (text [default], number)

Page 101: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Sorting<employees><employee><name><given>James</given><family>Clark</family></name> ……. </employee></employees>

The list of employees sorted by name could be generated using:

<xsl:template match="employees"> <ul> <xsl:apply-templates select="employee"> <xsl:sort select="name/family"/> <xsl:sort select="name/given"/> </xsl:apply-templates> </ul></xsl:template>

Page 102: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Exercise 14• Exercise on

– Sorting using <xsl:sort>

Page 103: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Variables, Parameters• Variable is a name that may be bound to a value

• Two elements used to bind variables:– xsl:variable– xsl:param

• Value specified on xsl:param variable is default value

• When the template within which the xsl:param element occurs is invoked, parameters may be passed that are used in place of the default values

• xsl:variable , xsl:param have required name attribute specifying name of variable

Page 104: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Result Tree Fragments• Is an additional data-type into the expression language

• Variable may be bound to a result tree fragment

• Represent a fragment of the result tree

• A result tree fragment is treated equivalently to a node-set that contains just a single root node

• Not permitted to use the /, //, and [] operators on result tree fragments

Page 105: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Variable, Parameter ValuesValue of the variable specified three alternative ways• Variable-binding element has select attribute

– Value of select attribute must be an expression– Value of variable is the object that results from evaluating expression

• Content of the variable-binding element specifies value– Content is a template– Value is result tree fragment equivalent to a node-set containing

• Single root node • Children (sequence of nodes produced by instantiating template)• Error if a member of the sequence of nodes created by instantiating the

template is attribute or namespace node

• Variable-binding element has empty content, does not have a select attribute

– Value of the variable is an empty string

Page 106: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Variable, Parameter Values<xsl:variable name="n">2</xsl:variable> ... <xsl:value-of select="item[$n]"/>

VERSUS

<xsl:variable name="n" select="2"/> ...<xsl:value-of select="item[$n]"/>

Anything wrong with the first case?

Page 107: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

xsl:copy – Var., Para. Values• xsl:copy-of can be used to insert a result tree fragment

into the result tree

• xsl:copy-of does not convert result tree fragment to string as xsl:value-of does

• Required select attribute contains an expression

• When result of evaluating expression is:– a result tree fragment, the complete fragment is copied into the

result tree– a node-set, all the nodes in the set are copied in document

order into the result tree

Page 108: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Variables, Parameters• xsl:variable, xsl:param allowed as top-level

elements• A top-level variable-binding element declares a global

variable that is visible everywhere

<xsl:variable name="para-font-size">12pt</xsl:variable>

<xsl:template match="para"> <fo:block font-size="{$para-font-size}"> <xsl:apply-templates/> </fo:block></xsl:template>

Page 109: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Var., Para. within templates• xsl:variable, xsl:param are also allowed in

templates• xsl:variable is allowed anywhere within a

template that an instruction is allowed• xsl:param is allowed as a child at the

beginning of an xsl:template element

Page 110: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Passing para’s. to templates• Parameters are passed to templates using xsl:with-

param

• Required name attribute specifies name of the parameter

• xsl:with-param allowed within xsl:call-template, xsl:apply-templates

• Value of parameter is specified in same way as xsl:variable and xsl:param

Page 111: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Passing para’s. to templates<xsl:template name="numbered-block"> <xsl:param name="format">1. </xsl:param> <fo:block><xsl:number format="{$format}"/> <xsl:apply-templates/> </fo:block></xsl:template>

<xsl:template match="ol//ol/li"> <xsl:call-template name="numbered-block"> <xsl:with-param name="format">a. </xsl:with-param> </xsl:call-template></xsl:template>

Page 112: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Exercise 15• Exercise on

– Parameterization using <xsl:call-template>, <xsl:with-param>

Page 113: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Function Library

• There are additional XSLT-specific additions to the core XPath function library

– node-set document (object, node-set?)

• Refer to XSLT Function Manual for details

Page 114: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Keys• Keys provide a way to work with documents containing

implicit cross-reference structure

• Limitations of XML (ID, IDREF) approach:– ID attributes must be declared as such in the DTD– Cannot be separate independent sets of unique IDs– ID of element can only be specified in an attribute cannot be

specified by content of element, child element– ID is constrained to be an XML name (no spaces)– An element can have at most one ID– At most one element can have a particular ID

Page 115: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Keys• Key is a triple containing:

– Node which has the key– Name of the key – Value of the key

• Style sheet declares a set of keys for each document using the xsl:key element

• Key member with node x, name y,value z, means that node x has key with name y and value z

• Key is a kind of generalized ID, which is not subject to the same limitations as an XML ID

Page 116: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Keys• Key value for an element may be specified in:

– Attribute– Child element– Content

• XPath expression used to specify where to find the value for a particular named key

• Multiple keys possible in a document with same node, same key name, but different key values

• Multiple keys possible in a document with the same key name, same key value, but different nodes

Page 117: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Keys• xsl:key element is used to declare keys• name attribute specifies name of the key• xsl:key gives information about keys of any

node that matches the pattern specified in the match attribute

• use attribute is an expression specifying the values of the key

Page 118: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

KeysA node x has a key with name y,value z if andonly if there is an xsl:key element such that:

• x matches pattern specified in the match attribute of the xsl:key element

• name attribute of the xsl:key element is equal to y

• when expression specified in the use attribute of xsl:key element is evaluated with x as the current node, with node list containing just x as the current node list resulting in an object u, then either z is equal to the result of converting u to a string or u is a node-set and z is equal to the string-value of one or more of the nodes in u

Page 119: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Keys• Function: node-set key(string, object)

– First argument specifies the name of the key

– Returns a node-set containing the nodes in the same document as the context node that have a value for the named key equal to second argument

Page 120: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

KeysA function library uses a prototype element to

define functions:

<prototype name="key" return-type="node-set">

<arg type="string"/><arg type="object"/></prototype>

A function element to refer to function names is:

<function>key</function>

Page 121: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

KeysStyle sheet could generate hyperlinks between thereferences and definitions as follows

<xsl:key name="func" match="prototype“ use="@name"/> <xsl:template match="function"> <b><a href="#{generate-id(key('func',.))}"> <xsl:apply-templates/> </a></b></xsl:template>

<xsl:template match="prototype"> <p><a name="{generate-id()}"> <b>Function: </b> ... </a></p></xsl:template>

Page 122: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Exercise 16• Exercise on

– Cross Reference between reference and definition using <xsl:key>, generate-id()

Page 123: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Messages• xsl:message instruction sends a message in

a way that is dependent on the XSLT processor

• Content of xsl:message instruction is a template

• If terminate attribute = yes:– XSLT processor will terminate processing after

sending message

Page 124: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Message LocalizationMessages for a language L are stored in an XMLfile resources/L.xml:<messages> <message name="problem"> A problem was detected. </message></messages>

<xsl:param name="lang" select="en"/><xsl:variable name="messages" select="document(concat('resources/',$lang,'.xml'))/messages"/> <xsl:template name="localized-message"> <xsl:param name="name"/> <xsl:message> <xsl:value-of select="$messages/message[@name=$name]"/> </xsl:message> </xsl:template><xsl:template name="problem"><xsl:call-template name="localized-message"/><xsl:with-param name="name">problem</xsl:with-param></xsl:call-template></xsl:template>

Page 125: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Exercise 17• Exercise on

– Messages using:– <xsl:message>, <xsl:variable>, <xsl:param>,

<xsl:call-template> – document() function

Page 126: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Extensions• Two kinds of extensions

– Extension elements– Extension functions

• Element extension mechanism allows namespaces to be designated as extension namespaces

• Elements from extension namespace occuring in a template are treated as instructions rather than literal result element

• Namespace determines the semantics of the instruction.

Page 127: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Extensions• Extension namespace designated by using

extension-element-prefixes attribute on:– xsl:stylesheet– xsl:extension-element-prefixes attribute on literal result

element – extension element

• Value of both these attributes is a white space-separated list of namespace prefixes

• If XSLT processor does not have an implementation of a particular extension element encountered, processor must perform fallback

Page 128: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Fallback• XSLT processor performs fallback for instruction

element, if:– Instruction element has one or more xsl:fallback children,

content of each is instantiated in sequence– Otherwise, an error must be signaled

• Content of xsl:fallback element is a template

• Following used to explicitly control style sheet behavior for unavailable elements, functions

– boolean element-available(string)– boolean function-available(string)

Page 129: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Output• XSLT processor outputs result tree as specified by xsl:output

• xsl:output element is only allowed as a top-level element

• method attribute used to identify method to be used for outputting the result tree

• method attribute values could be: xml, html, text

• If document element = html– output method is html– Else output method = xml

Page 130: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Combining Style sheets• 2 mechanisms to combine style sheets:

– Inclusion mechanism: allows style sheets to be combined without changing the semantics of the style sheets being combined

– Import mechanism: allows style sheets to override each other

Page 131: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Style sheet Inclusion• Style sheet may include another one using

xsl:include– href attribute = URI reference identifying the

style sheet to be included

• xsl:include element is only allowed as a top-level element

Page 132: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Style sheet Inclusion• Inclusion works at the XML tree level

• Resource located by href attribute value is parsed as an XML document

• Children of the including xsl:stylesheet element replace xsl:include element in the including document

Page 133: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Style sheet Import• Style sheet may import another one using

xsl:import– href attribute = URI reference identifying the style

sheet to be included

• Importing same as including except that definitions, template rules in the importing style sheet take precedence over template rules and definitions in the imported style sheet

Page 134: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Style sheet Import• xsl:import element is only allowed as

top-level element

• xsl:import element children must precede all other element children of an xsl:stylesheet element, including any xsl:include element children

Page 135: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Style sheet Import• When xsl:include used to include style

sheet, any xsl:import elements in the included document are moved up in the including document to after any existing xsl:import elements in the including document

Page 136: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Exercise 18• Exercise on

– <xsl:include>, <xsl:import>

Page 137: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Embedding Style sheets• 2 forms of embedding are possible:

– XSLT stylesheet may be textually embedded in a non-XML resource

– xsl:stylesheet element may occur in an XML document other than as the document element

Page 138: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Embedding Style sheets<?xml-stylesheet type="text/xml" href="#style1"?><!DOCTYPE doc SYSTEM "doc.dtd"><doc> <head> <xsl:stylesheet id="style1" version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:import href="doc.xsl"/> <xsl:template match=“id('foo‘)"> <B><xsl:apply-templates/></B> </xsl:template> <xsl:template match="xsl:stylesheet"> <!-- ignore --> </xsl:template> </xsl:stylesheet> </head> <body><para id="foo"> ... </para></body></doc>

Page 139: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Exercise 19• Exercise on

– Embedding Style sheets

Page 140: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

White Space Stripping• After source, style sheet trees are constructed, but

before XSLT processing, some text nodes are stripped

• A text node is preserved if any of the following apply:– The element name of the parent of the text node is in the set

of whitespace-preserving element names

– The text node contains at least one non-whitespace character

– An ancestor element of the text node has an xml:space attribute with a value of preserve, and no closer ancestor element has xml:space with a value of default

Page 141: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

White Space Stripping• For style sheets, the set of whitespace-preserving

element names consists of– xsl:text

• For source documents, the set of whitespace-preserving element names is specified by (top-level):– xsl:strip-space– xsl:preserve-space

Both have attributes whose values are a whitespace-separated list of Elements

Page 142: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Overriding Template Rules• A template rule used to override a template

rule in an imported style sheet can use xsl:apply-imports to invoke the overridden template rule

• xsl:apply-imports processes the current node using only template rules that were imported into the style sheet element containing the current template rule

Page 143: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Overriding Template Rules<xsl:template match="example"> <pre><xsl:apply-templates/></pre></xsl:template>……<xsl:import href="doc.xsl"/> <xsl:template match="example"> <div style="border: solid red"> <xsl:apply-imports/> </div> </xsl:template>

Combined Effect:<div style="border: solid red"><pre>...</pre></div>

Page 144: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Conflict Resolution• If source node matches more than one

template rule then:– All matching template rules with lower

priority than matching template rule(s) with the highest priority are eliminated• Priority given by priority attribute on template rule• Else, default priority is also used

Page 145: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Conflict Resolution• Default priority computed as follows:

• Priority = 0: – If pattern has form of QName preceded by child/ attribute axis

OR – If pattern has form of processing-instruction (literal) preceded

by child/ attribute axis

• Priority = -0.25:– If pattern has form NCName:* preceded by child/ attribute axis

• Priority = -0.5:– If pattern only has NodeTest preceded by child/ attribute axis

• Otherwise, the priority is 0.5

Page 146: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

Modes• Allow an element to be processed multiple times, each time producing

a different result

• xsl:template, xsl:apply-templates have an optional mode attribute

• If xsl:template does not have a match attribute, it must not have a mode attribute

• If xsl:apply-templates has mode attribute, it applies only to those template rules (xsl:template) with mode attribute with the same value

• If xsl:apply-templates does not have mode attribute, it applies only to those template rules (xsl:template) that do not have a mode attribute

Page 147: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Exercise 20• Exercise on

– Average of Numbers

Page 148: XSLT. XSL comprises of –XSLT: Is a language for transforming XML documents into other XML documents –FO: XML vocabulary for specifying formatting XSL

XSLT Exercise 21• Exercise on

– xsl:copy– xsl:copy-of– xsl:sort– msxsl:node-set()