Professional Documents
Culture Documents
Session 7
Session 7
Session 7
rA
pt
ec
h
C
en
te
rU
Module 7
se
More on XSLT
O
nl
y
Module Overview
y
nl
O
In this module, you will learn about:
se
XPath
XPath Expressions and Functions
rU
Working with different Styles
te
en
C
h
ec
pt
rA
Fo
y
nl
O
In this first lesson, XPath, you will learn to:
se
Define and describe XPath.
Identify nodes according to XPath.
rU
List operators used with XPath.
te
Describe the types of matching
en
C
h
ec
pt
rA
Fo
y
nl
O
XPath can be considered as a query language like SQL.
se
Extracts information from an XML document.
Language for retrieving information from a XML document.
rU
Used to navigate through elements and attributes in an XML
document.
te
Allows identifying parts of an XML document.
en
Provides a common syntax for features shared by Extensible
Stylesheet Language Transformations (XSLT) and XQuery.
C
h
ec
pt
rA
Fo
y
nl
O
XSLT
se
Language for transforming XML documents into XML, HTML or
text.
rU
XQuery
Builds on XPath.
te
en
C
h
ec
pt
rA
Fo
y
nl
O
Provides single syntax that you can use for:
se
Queries
Addressing
rU
Patterns
Concise, simple, and powerful.
te
Benefits:
en
y
nl
O
An XML document is viewed as a tree.
se
Each part of the document is represented as a node.
rU
Nodes
te
Root
en
A single root node that contains all other nodes in the tree.
Element
C
y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
O
Attribute
se
Each element node has an associated set of attribute nodes.
Element is the parent of each of these attribute nodes.
rU
te
en
node too.
Fo
y
nl
O
Processing instruction
se
Processing instruction node exists for
rU
every processing instruction except for any
processing instruction within the
te
document type declaration.
en
The processing instruction node has a parent
C
node and it may be the child node too.
h
Namespace
ec
namespace nodes.
rA
y
nl
O
An XPath query operates on a well-formed XML document after it
has been parsed into a tree structure.
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
O
An XPath expression returns a node set, a boolean, a string, or
se
a number.
XPath provides basic floating point arithmetic operators and
rU
te
en
Operator Description
/ Child operator; selects immediate children of the left-side collection.
//
C
Recursive descent; searches for the specified element at any depth
h
. Indicates the current context
ec
y
nl
O
se
Expression Refers to
rU
Author/FirstName All <FirstName> elements within an
<Author> element of the current context node.
BookStore//Title All <Title> elements one or more levels deep
te
in the <BookStore> element.
en
BookStore/*/Title All <Title> elements that are grandchildren of
<BookStore> elements.
C
BookStore//Book/Excerpt//Work All <Work> elements anywhere inside
<Excerpt> children of <Book> elements,
h
anywhere inside the <BookStore> element.
ec
y
nl
O
XPath can create of the patterns.
se
The match attribute of the xsl:template element
rU
supports a complex syntax.
Allows to express exactly which nodes to be
te
matched.
en
The select attribute of xsl:apply-templates,
C
xsl:value-of, xsl:for-each, xsl:copy-of,
h
and xsl:sort supports a superset of the syntax.
ec
and which nodes not to be selected.
rA
Fo
y
nl
O
Matching by name
se
The source element is identified by its name, using the match attribute.
The value given to the match attribute is called the pattern.
rU
Code Snippet
Matches all greeting elements in
te
<xsl:template match = “Greeting”>
the source document.
Matching by ancestry
en
Code Snippet
C
h
<xsl:template match = “P//EM”> Matches any ‘EM’ element that
has ‘P’ as an ancestor.
ec
The most basic pattern contains a single element name which matches all
elements with that name.
rA
Code Snippet
Fo
y
nl
O
Matching the root
se
Enables all descendant nodes to inherit the properties on the root of document.
Uses a single forward slash to represent the root.
rU
Code Snippet
te
<xsl:template match = “/”> Selects the root pattern.
en
Matching by attribute
C
As in CSS, a match can be made using the element’s ancestry.
Syntax
h
Uses square
ec
Code Snippet
rA
y
nl
O
In this second lesson, XPath Expressions and
se
Functions, you will learn to:
rU
State and explain the various XPath expressions
and functions.
te
List the node set functions.
en
List the boolean functions.
C
State the numeric functions.
h
ec
y
nl
O
Statements that can extract useful information from
se
the XPath tree.
rU
Instead of just finding nodes, one can count them,
add up numeric values, compare strings, and more.
te
Are like statements in a functional programming
en
language.
C
Evaluates to a single value.
h
ec
pt
rA
Fo
y
nl
O
Node-set
se
An unordered group of nodes from the input document that match
an expression’s criteria.
rU
Boolean
XSLT allows any kind of data to be transformed into a boolean.
te
Often done implicitly when a string or a number or a node-set
is used where a boolean is expected.
en
Number
arithmetic. C
Numeric values useful for counting nodes and performing simple
h
Numbers like 43 or –7000 that look like integers are stored as
ec
doubles.
Non-number values, such as strings and booleans, are converted
pt
String
A sequence of zero or more Unicode characters.
Fo
y
nl
O
XPath defines various functions required for XPath 2.0, XQuery
se
1.0 and XSLT 2.0.
The different functions are Accessor, AnyURI, Node, Error
rU
te
en
Can be used to refine XPath queries and enhance the
programming power and flexibility of XPath.
C
Each function in the function library is specified using a
h
function prototype that provides the return type, function
ec
y
nl
O
Take a node-set argument.
Return a node-set or information about a particular node within a
se
node-set.
Are name(), local-name(), namespace-uri() and root().
rU
Syntax
te
where,
en
name(): Returns name of current node or
the first node in specified node set.
y
nl
Code and Schema
O
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
Stylesheet
O
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
O
Formatted Output
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
boolean(arg)
O
Returns a boolean value for a number, string, or node-set.
se
Syntax
rU
fn:boolean(arg)
te
Code Snippet
en
<ul>
<li><b>boolean(0)</b> = <xsl:value-of select=”boolean(0)”/></li>
C
<li><b>boolean(1)</b> = <xsl:value-of select=”boolean(1)”/></li>
<li><b>boolean(-100)</b> = <xsl:value-of select=”boolean(-100)”/></li>
h
<li><b>boolean(‘hello’)</b> = <xsl:value-of select=”boolean(‘hello’)”/></li>
ec
<li><b>boolean(‘’)</b> = <xsl:value-of select=”boolean(‘’)”/></li>
<li><b>boolean(//book)</b> = <xsl:value-of select=”boolean(//book)”/></li>
</ul>
pt
rA
boolean(0) = false
boolean(1) = true
boolean(-100) = true
Fo
boolean(‘hello’) = true
boolean(‘’) = false
boolean(//book) = false
@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 25 of 51
Boolean Functions 2-5
y
nl
O
not(arg)
se
The sense of an operation can be reversed by using the
not() function.
rU
Syntax
te
en
fn:not(arg)
C
h
ec
pt
rA
Fo
y
nl
O
not(arg)
se
The sense of an operation can be reversed by using the
not() function.
rU
Code Snippet
te
<xsl:template match=”PRODUCT[not(position()=1)]”>
en
<xsl:value-of select=”.”/>
</xsl:template>
C
h
The template rule selects all product elements that are not the first child of their parents.
ec
Code Snippet
pt
<xsl:template match=”PRODUCT[position()!=1]”>
rA
<xsl:value-of select=”.”/>
</xsl:template>
Fo
The same template rule could be written using the not equal operator != instead.
@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 27 of 51
Boolean Functions 4-5
y
nl
O
true()
se
Returns the boolean value true.
rU
Syntax
te
fn:true()
en
Code Snippet
C
h
<xsl:value-of select=”true()”/>
ec
pt
true
rA
Fo
y
nl
O
false()
se
Returns the boolean value true.
rU
Syntax
te
fn:false()
en
Code Snippet
C
<xsl:value-of select=”false() or false()”/>
h
<xsl:value-of select=”true() and false()”/>
ec
true
false
Fo
false
y
nl
O
Return strings or numbers.
Can be used with comparison operators in filter patterns such as:
se
number(arg)
rU
ceiling(num)
floor(num)
round(num)
te
en
Syntax
where,
C number(arg): Returns numeric value of
argument. Argument could be a boolean, a string,
h
or a node-set.
ec
y
nl
O
Stylesheet(1-3)
se
<?xml version=”1.0”?>
<?xml-stylesheet type=”text/xsl” href=”Number.xsl”?>
rU
<xsl:stylesheet version=”1.0” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>
<xsl:output method=”html”/>
<xsl:template match=”/”>
<html>
te
<body>
<h3>Numeric Functions</h3>
en
<ul>
<li>
<b>number(‘1548’)</b> =
C
<xsl:value-of select=”number(‘1548’)”/>
</li>
h
<li>
<b>number(‘-1548’)</b> = <xsl:value-of select=”number(‘-1548’)”/>
ec
</li>
<li>
pt
<li>
<b>number(‘226.38’ div ‘1’)</b> = <xsl:value-of select=”number(‘226.38’
div ‘1’)”/>
</li>
Fo
</ul>
y
nl
O
Stylesheet(2-3)
se
<ul>
<li>
rU
<b>ceiling(2.5)</b> = <xsl:value-of select=”ceiling(2.5)”/>
</li>
<li>
<b>ceiling(-2.3)</b> = <xsl:value-of select=”ceiling(-2.3)”/>
te
</li>
<li>
en
<b>ceiling(4)</b> = <xsl:value-of select=”ceiling(4)”/>
</li>
</ul>
<ul>
<li> C
h
<b>floor(2.5)</b> = <xsl:value-of select=”floor(2.5)”/>
</li>
ec
<li>
<b>floor(-2.3)</b> = <xsl:value-of select=”floor(-2.3)”/>
pt
</li>
<li>
rA
y
nl
O
Stylesheet(3-3)
se
<ul>
<li>
rU
<b>round(3.6)</b> = <xsl:value-of select=”round(3.6)”/>
</li>
<li>
<b>round(3.4)</b> = <xsl:value-of select=”round(3.4)”/>
te
</li>
<li>
en
<b>round(3.5)</b> = <xsl:value-of select=”round(3.5)”/>
</li>
<li>
C
<b>round(-0.6)</b> = <xsl:value-of select=”round(-0.6)”/>
</li>
h
<li>
<b>round(-2.5)</b> = <xsl:value-of select=”round(-2.5)”/>
ec
</li>
</ul>
pt
</body>
</html>
rA
</xsl:template>
</xsl:stylesheet>
Fo
y
nl
O
Output
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
O
String functions are used to:
se
Evaluate
rU
Format and manipulate string arguments
Convert an object to a string
te
en
Different String functions are:
string(arg) C
h
compare()
ec
concat()
pt
substring()
rA
Fo
y
nl
O
Syntax
se
where,
rU
string(arg): Returns string value of
the argument. The argument could be
a number, boolean, or node- set.
te
translate(): Returns first argument
en
string with occurrences of characters in
second argument string replaced by
y
nl
O
Code Snippet
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
O
Stylesheet
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
O
Output
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
O
A portion of an XML document that is not a complete
node or set of nodes.
se
The only allowed operation in a result tree fragment
rU
is on a string.
The operation on the string may involve first
te
converting the string to a number or a boolean.
en
Result tree fragment is an additional data type other
C
than four basic XPath data types, such as, string,
number, boolean, node-set.
h
ec
operators on Result tree fragments.
Fo
y
nl
O
In this last lesson, Working with different Styles,
se
you will learn to:
rU
Explain how to switch between styles.
te
HTML using XSLT.
en
C
h
ec
pt
rA
Fo
y
nl
O
Transformation is one of the most important and useful
techniques for working with XML.
se
XML can be transformed by changing its structure, its markup,
rU
te
The most common reason to transform XML is to extend the
reach of a document into new areas by converting it into a
en
presentational format.
Uses of transformation: C
h
Formatting a document to create a high-quality presentational
ec
format.
Changing one XML vocabulary to another.
pt
another way.
Fo
y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
O
Takes two inputs:
se
An XSLT stylesheet to govern the transformation process.
An input document called the source tree.
rU
te
and caching it as a look-up table.
en
XPath locates the parts of XML document such as Element
C
nodes, Attribute nodes and Text nodes.
For each node the XSLT processes, it will look in the table for
h
the best matching rule to apply.
ec
Starting from the root node, the XSLT engine finds rules,
pt
y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
Step 1
O
Creates a normal XML document.
se
Code Snippet
rU
<?xml version=”1.0” encoding=”UTF-8”?>
Step 2
Add the following lines.
te
Code Snippet
en
<?xml version=”1.0” encoding=”ISO-8859-1”?>
C
<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0” >
...
</xsl:stylesheet>
h
Step 3
ec
Set it up to produce HTML-compatible output.
The <xsl:output> tag should be used with either “text” or “html” to output
pt
Code Snippet
Fo
<xsl:stylesheet>
<xsl:output method=”html”/>
...
</xsl:stylesheet>
@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 46 of 51
Transforming XML using XSLT 2-2
y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
O
Example code of transforming XML documents into HTML using XSLT
processor
se
Code Snippet
rU
where,
te
Company: The root Company
element contains Product
en
child elements.
y
nl
O
Stylesheet
se
rU
te
en
C
h
ec
pt
rA
Output
Fo
y
nl
O
XPath
se
Notation for retrieving information from a document.
rU
Extensible Stylesheet Language Transformations (XSLT)
and XQuery.
te
Have seven types of node as Root, Element, Attribute,
Text, Comment, Processing instruction and Namespace.
en
Used in the creation of the patterns.
C
XPath Expressions and Functions
h
ec
Node-sets.
pt
Booleans.
rA
Numbers.
Strings.
Fo
y
nl
O
XPath Expressions and Functions (contd...)
se
Functions defined for XPath are Accessor, AnyURI,
Node, Error and Trace, Sequence, Context,
rU
Boolean, Duration/Date/Time, String, QName and
Numeric.
te
A Result tree fragment is a portion of an XML document
that is not a complete node or set of nodes.
en
C
Working with different styles
Transformation is one of the most important and useful
h
techniques for working with XML.
ec
processor also.
Fo