SlideShare a Scribd company logo
Part 5
XSLT
The Limitations of CSS
When presenting XML data on a browser, we use CSS files,
however CSS files have the following limitations:
● You can only display element values (you cannot display attribute
values).
● You cannot add additional text.
● You cannot control the content of the XML file, you can only display it
as it is.
● Difficult to display images or insert links.
XSL
XSL stands for “Extensible Stylesheet Language”. XSL specifies
how the XML document will be presented. XSL composed in to
two parts with each part acting as a separate language:
1. XSLT (XSL Transformations
2. XSL-FO (XSL- Formatting Objects)
 XSL allows you to transform your XML data into a variety of
formats, including HTML, XHTML, Portable Document Format
(PDF), Rich Text Format (RTF), and even a new XML document.
XSLT
Features:
 An XSLT stylesheet is an XML document (with elements and attributes), and
it is used to transform the contents of an XML document into another
document format.
 The extension of an XSLT file is .xsl
 An XSLT processor transforms an XML document into another output
format (ex: XML, XHTML, RTF). by default, an XSLT processor renders
the result document as an XML file.
 An XSLT processor is included in some browsers, including Internet
Explorer and Firefox. It can also take place on a server.
* In this course, we will use a browser to transform XML to XML with formatting (the default transformation option).
XSLT Processor
Creating an XSLT StyleSheet
Attaching an XSLT StyleSheet
<?xml-stylesheet type=”text/xsl” href=”url” ?>
 Recall that when we applied a CSS file to an XML file we added the following
processing instruction: <?xml-stylesheet type=”text/css” href=”style.css” ?>
XPath
 The path to reach a certain node in the xml document.
 In XSLT, the content of an XML document is organized into nodes:
o Document node
o Processing instruction nodes
o Element nodes
o Attribute nodes
o Text nodes
 The following are not considered as nodes:
 The XML declaration.
 A CDATA section.
 A DOCTYPE declaration (ex: internal DTD).
Xml part5
XSLT Elements
1- The “template” element
1- The “template” element
 XSLT applys style to the XML document by applying templates to XML tags using the
match attribute. Inside the template element, we use:
1- HTML elements to apply formatting
2- XSLT elements to select elements from the XML document and control
the content of the resulting XML document.
 The match attribute allows you to select the XML elements which you want to apply
the style or template to.
 The match attribute contains XPath expressions to determine which element you want
to apply style to.
* note: In this course, we will match the template to the root element only.
2- The “value-of” element
 To insert a node’s value into the result document, you use the
XSLT element
<xsl:value-of select="expression" />
where expression is an XPath expression that identifies the node
from the source document’s node tree.
<?xml version=“1.0”?>
<?xml-stylesheet type=“text/xsl” href=“customers.xsl”?>
<customers>
<customer custID="b123">
<name title="Mr.">Ahmed</name>
<address>40 Kent st.</address>
<phone>3456123</phone>
</customer>
<customer custID="C136">
<name title="Mr.">Khaled</name>
<address>14 bronson st.</address>
<phone>3456123</phone>
</customer>
<customer custID="D234">
<name title="Mrs.">Mona</name>
<address>17 Aberdeen st.</address>
<phone>3451111</phone>
</customer>
</customers>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<html>
<head> Customers </head>
<body style="background-color: LightGray ; font-family: Cambria">
<h1> this is a list of customers: </h1>
<p><xsl:value-of select="customers/customer/name"/></p>
<p><xsl:value-of select="customers/customer/address"/></p>
<p><xsl:value-of select="customers/customer/phone"/></p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Xml part5
The “value-of” element
The value-of element extracts a value from the XML document. Since we have
3 customers in our example, only the first value was extracted.
If we specifically wanted the second customer, the Xpath expression would be:
customers/customer[2]
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<html>
<head> Customers </head>
<body style="background-color: LightGray ; font-family: Cambria">
<h1> this is a list of customers: </h1>
<p><xsl:value-of select="customers/customer[2]/name"/></p>
<p><xsl:value-of select="customers/customer[2]/address"/></p>
<p><xsl:value-of select="customers/customer[2]/phone"/></p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
3- The “for-each” element
3-The “for-each” element
 The for-each element is like a loop, it allows you to loop through multiple
occurrences of an element.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<html>
<head> Customers </head>
<body style="background-color: LightGray ; font-family: Cambria">
<xsl:for-each select="customers/customer">
<p><xsl:value-of select="name"/><br/>
<xsl:value-of select="address"/><br/>
<xsl:value-of select="phone"/> </p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Xml part5
Look at the screen first, then try to guess the
code
Xml part5
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<head> Customers </head>
<body style="background-color: LightGray ; font-family: Cambria">
<xsl:for-each select="customers/customer">
<p>Name: <xsl:value-of select="name"/><br/>
Address: <xsl:value-of select="address"/><br/>
Phone: <xsl:value-of select="phone"/> </p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Xml part5
<xsl:template match="/">
<html>
<body style="background-color: LightGray ; font-family: Cambria">
<table border="1" cellpadding="8">
<tr>
<th>Name</th>
<th>Address</th>
<th>Phone</th>
</tr>
<xsl:for-each select="customers/customer">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="address"/></td>
<td><xsl:value-of select="phone"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
What about attributes?
So far, we used XSLT to display elements in our XML file.
How do we display the attributes?
@attribute
root-element/child-element/@attribute
<?xml version=“1.0”?>
<?xml-stylesheet type=“text/xsl” href=“customers.xsl”?>
<customers>
<customer custID="b123">
<name title="Mr.">Ahmed</name>
<address>40 Kent st.</address>
<phone>3456123</phone>
</customer>
<customer custID=" C136">
<name title="Mr.">Khaled</name>
<address>14 bronson st.</address>
<phone>3456123</phone>
</customer>
<customer custID="D234">
<name title="Mrs.">Mona</name>
<address>17 Aberdeen st.</address>
<phone>3451111</phone>
</customer>
</customers>
<xsl:template match="/">
<html>
<body style="background-color: LightGray ; font-family: Cambria">
<table border="1" cellpadding="8">
<tr>
<th>Name</th>
<th>Address</th>
<th>Phone</th>
</tr>
<xsl:for-each select="customers/customer">
<tr>
<td><xsl:value-of select="name/@title"/>
<xsl:value-of select="name"/></td>
<td><xsl:value-of select="address"/></td>
<td><xsl:value-of select="phone"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
Xml part5
<table border="1" cellpadding="8">
<tr>
<th>Customer ID</th>
<th>Name</th>
<th>Address</th>
<th>Phone</th>
</tr>
<xsl:for-each select="customers/customer">
<tr>
<td><xsl:value-of select="@custID"/></td>
<td><xsl:value-of select="name/@title"/><xsl:value-of
select="name"/></td>
<td><xsl:value-of select="address"/></td>
<td><xsl:value-of select="phone"/></td>
</tr>
</xsl:for-each>
</table>
Xml part5
Using the Double-forward Slash “//”
 The Path to a certain node begins with the root node and proceeds down to the
selected node:
root-node/child-node/sub-child-node
To avoid listing all of the levels of a node tree, use a double forward slash
(//):
//selected-node
 And for attributes: //@attribute
Using the Double-forward Slash “//”
When we have more than one occurrence of the same element, XPath
will return the first one it finds.
If we want to specify which occurrence we want, we specify it:
//selected-node[3]
If we want to loop over all occurrences, we use the “for-each” element.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<head> Customers </head>
<body style="background-color: LightGray ; font-family: Cambria">
<h1> this is a list of customers: </h1>
<p><xsl:value-of select="customers/customer/name"/></p>
<p><xsl:value-of select="customers/customer/address"/></p>
<p><xsl:value-of select="customers/customer/phone"/></p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<html>
<head> Customers </head>
<body style="background-color: LightGray ; font-family: Cambria">
<h1> this is a list of customers: </h1>
<p><xsl:value-of select="//name"/></p>
<p><xsl:value-of select="//address"/></p>
<p><xsl:value-of select="//phone"/></p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Here we used “//” to abbreviate the XPath expression.
The Context Node
 When XPath stops at a certain node, we call it the “context node”. We can refer
to it using the “.” symbol.
 Example:
<xsl:for-each select=“//selected-node”>
<xsl:value-of select=“.”/>
</xsl:for-each>
This code will print all the text contained within the context node.
 If the context node contains child elements, all their values will be printed.
(Attribute values will not be printed, only child element values will print out)
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="//Magazine">
<xsl:value-of select="."/><br/>
</xsl:for-each>
</body>
</html>
</xsl:template>
XSLT Conditional Elements:
“If” and “Choose”
XSLT Conditional Elements: If Element
 The If element syntax:
<xsl:if test=“ test “>
…
</xsl:if>
 Where test is an XPath expression that is either true or false. If the test is
true, the XSLT style commands are generated by the processor; otherwise,
nothing is done.
<customers>
<customer custID="b123">
<name title="Mr.">Khaled</name>
<address>40 Kent st.</address>
<phone>3456123</phone>
<gender>male</gender>
</customer>
<customer custID="C136">
<name title="Mrs.">Mona</name>
<address>14 Bronson st.</address>
<phone>3456123</phone>
<gender>female</gender>
</customer>
<customer custID="b123">
<name title="Mr.">Ahmed</name>
<address>40 Kent st.</address>
<phone>3456123</phone>
<gender>male</gender>
</customer>
<customer custID="D234">
<name title="Mrs.">lama</name>
<address>17 Aberdeen st.</address>
<phone>3451111</phone>
<gender>female</gender>
</customer>
</customers>
notice that we
added a new
element “gender”
<xsl:for-each select="//customer">
<xsl:if test="gender = 'female'">
ID: <xsl:value-of select="@custID"/><br/>
Name: <xsl:value-of select="name/@title"/>
<xsl:value-of select="name"/><br/>
Address: <xsl:value-of select="address"/><br/>
Phone: <xsl:value-of select="phone"/> <br/><br/>
</xsl:if>
</xsl:for-each>
Xml part5
XSLT Conditional Elements: Choose Element
<xsl:choose>
<xsl:when test=“ test “>
…
</xsl:when>
<xsl:otherwise>
…
</xsl:otherwise>
</xsl:choose>
<xsl:for-each select="//customer">
<xsl:choose>
<xsl:when test="gender = 'female'">
<div style="color:red">
ID: <xsl:value-of select="@custID"/><br/>
Name: <xsl:value-of select="name/@title"/>
<xsl:value-of select="name"/><br/>
Address: <xsl:value-of select="address"/><br/>
Phone: <xsl:value-of select="phone"/></div>
</xsl:when>
<xsl:otherwise>
<div style="color:blue">
ID: <xsl:value-of select="@custID"/><br/>
Name: <xsl:value-of select="name/@title"/>
<xsl:value-of select="name"/><br/>
Address: <xsl:value-of select="address"/><br/>
Phone: <xsl:value-of select="phone"/></div>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
XSLT Conditional Elements: Choose Element
XSLT Conditional Elements: Choose Element
Conditional Operators
• Comparisons can be made between numbers, text strings, attribute
nodes, element nodes, or text nodes.
Conditional Operators Examples
 Example 1 :
day &lt; 5
you write it as
5 > day
 Comparison tests can be combined using the “and” and “or” operators. For example:
day > 2 and day &lt; 5
(tests whether the value of the day element lies between 2 and 5.)
 Similarly, the expression
@symbol = "AA" or @symbol = "UCL”
(tests whether the value of the symbol attribute is equal to “AA” or “UCL”.)
Working with Predicates
 A predicate is part of a location path that tests for a condition and references the
node set that fulfills that condition. The general syntax for a predicate is
node [expression]
 Example:
 name[3] Select the third name element in customers.
 name[@title = ‘Mr.’]( match all the name who title is Mr.)
 sName[@symbol = "AA" or @symbol="UCL”] (matches all sName elements
whose symbol attribute is equal to either “AA” or “UCL”.)
Predicates and Using XPath Functions
A predicate can contain XPath functions:
 last() returns the last node in the node tree
Example: book[last()] returns the last book
 position() returns the position value of the node
Example: book[position()>=2 and position()&lt;=5] returns
the second, third, fourth and fifth book
*note: book[position()=2] is equivalent to book[2]
More XPath Functions
Numeric Functions:
sum()
count()
String Functions
concat(string 1, string 2, string 3,…)
 Combines string1, string2, string3, ... into a single text string.
Count() & Sum() XPath Functions
Count() & Sum() XPath Functions
Count() & Sum() XPath Functions
<table border="1" cellpadding="8">
<tr>
<th>Name</th>
<th>Customer ID</th>
<th>Address</th>
<th>Phone</th>
</tr>
<xsl:for-each select="//customer">
<tr>
<td><xsl:value-of select="concat(name/@title,' ',name)"/></td>
<td><xsl:value-of select="@custID"/></td>
<td><xsl:value-of select="address"/></td>
<td><xsl:value-of select="phone"/></td>
</tr>
</xsl:for-each>
</table>
Using Concat() XPath Functions
Using Concat() XPath Functions
Working with Mathematical Operators
You can use mathematical operators inside the Xpath expressions to calculate new
values based on the element or attribute values from the XML document.
<shoppingList>
<product>
<name> milk</name>
<price>7</price>
<quantity>2</quantity>
</product>
<product>
<name> eggs</name>
<price>10</price>
<quantity>1</quantity>
</product>
<product>
<name>cheese</name>
<price>5</price>
<quantity>3</quantity>
</product>
</shoppingList>
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="//product">
<xsl:value-of select="name"/><br/>
<xsl:value-of select="price*quantity"/><br/><br/>
</xsl:for-each>
total number of items=<xsl:value-of select=“sum(//quantity)”/>
number of products:<xsl:value-of select=“count(//product)”/>
</body>
</html>
</xsl:template>
number of products: 3
Sorting Nodes:
Using the “sort” Element
Sorting Nodes
 To sort nodes, use the XSLT element “sort” with the “for-each” element.
<xsl:sort select=“expression” data-type=“type” order=“type”/>
 The select attribute determines the Criteria under which the context node is sorted.
 The data-type attribute indicates the type of data (text or number).
 The order attribute indicates the direction of sorting (ascending or descending).
<breakfast_menu>
<order orderID="M24">
<name type="sweet">Belgian Waffles</name>
<price>5.95</price>
</order>
<order orderID="Q33“>
<name type="sweet">Strawberry Belgian Waffles</name>
<price>7.95</price>
</order>
<order orderID="K50“>
<name type="salty">Eggs Benedict</name>
<price>6.95</price>
</order>
<order orderID="B12“>
<name type="sweet">Berry-Berry Belgian Waffles</name>
<price>8.95</price>
</order>
<order orderID="G56“>
<name type="sweet">French Toast with Maple Syrup</name>
<price>4.50</price>
</order>
<order orderID="K23“>
<name type="salty">Homestyle Breakfast</name>
<price>6.95</price>
</order>
</breakfast_menu>
<h3>Orders sorted by name:</h3>
<xsl:for-each select="//order">
<xsl:sort select="name" data-type="text" order="ascending"/>
<xsl:value-of select="name"/>
<br/>
</xsl:for-each>
Default Values
 The default value for the select attribute is the context node. Therefore, if you do not include
the select attribute, XSLT processors assume you want to sort the values of the context
node.
 The default value for the data-type attribute is text.
 The default value for the order attribute is ascending.
<h3>Orders sorted by name:</h3>
<xsl:for-each select="//name">
<xsl:sort/>
<xsl:value-of select="."/>
<br/>
</xsl:for-each>
Relying on the default attribute values will return the same output:
<h3>Orders sorted by price, descending:</h3>
<xsl:for-each select="//order">
<xsl:sort select="price" data-type="number" order="descending"/>
<xsl:value-of select="name"/>: <xsl:value-of select="price"/>
<br/>
</xsl:for-each>
<h3>Orders sorted by ID:</h3>
<xsl:for-each select="//order">
<xsl:sort select="@orderID" data-type="text"/>
<xsl:value-of select="@orderID"/>: <xsl:value-of select ="name"/>
<br/>
</xsl:for-each>
<h3>Orders sorted by type, then name:</h3>
<xsl:for-each select="//order">
<xsl:sort select="name/@type"/>
<xsl:sort select="name"/>
<xsl:value-of select="name/@type"/>: <xsl:value-of select="name"/>
<br/>
</xsl:for-each>
Here we used a double sort: first sort based on the type, then sort based on the name.
Element Attribute
xsl:stylesheet xmlns:xsl , version
xsl:template match
xsl:value-of select
xsl:for-each select
xsl:if test
xsl:choose -
xsl:when test
xsl:otherwise -
xsl:sort select , data-type , order
XSLT root element:
<?xml version= "1.0" encoding= "UTF-8"?>
<xsl:stylesheet xmlns:xsl= "http://www.w3.org/1999/XSL/Transform"
version= "1.0">
</xsl:stylesheet>
Linking an XML file to a StyleSheet:
<?xml-stylesheet type= "text/xsl" href= "customers.xsl"?>
Schema root element:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs= "http://www.w3.org/2001/XMLSchema" >
</xs:schema>
 Linking an XML file to a XML Schema
<students xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation= "studentsSchema.xsd“ >

More Related Content

PPTX
Xml part4
PPTX
Xml part2
PPTX
Xml part3
PPTX
Xml part1
PDF
Xml schema
PPTX
XML Schema
PPTX
Xml schema
PDF
XSD Incomplete Overview Draft
Xml part4
Xml part2
Xml part3
Xml part1
Xml schema
XML Schema
Xml schema
XSD Incomplete Overview Draft

What's hot (20)

PPT
Xml schema
PPTX
Introduction to XSLT
PPTX
XML DTD and Schema
PPTX
PPTX
PPTX
XML Schemas
DOCX
Introduction to xml schema
PPT
Xml Schema
PPT
02 xml schema
PPTX
PPT
Xsd examples
PPT
PPTX
Xml schema
PDF
Xsd tutorial
PDF
XSLT. Basic.
PPTX
Xslt tutorial
PPT
PDF
Transforming xml with XSLT
PPTX
Introduction to SQL
PPTX
Xml basics
Xml schema
Introduction to XSLT
XML DTD and Schema
XML Schemas
Introduction to xml schema
Xml Schema
02 xml schema
Xsd examples
Xml schema
Xsd tutorial
XSLT. Basic.
Xslt tutorial
Transforming xml with XSLT
Introduction to SQL
Xml basics
Ad

Viewers also liked (20)

PPTX
Xml part 6
PPT
Web Services
PPTX
Unleashing the Power of XSLT: Catalog Records in Batch
PPTX
The Mystical Principles of XSLT: Enlightenment through Software Visualization
PPTX
Applying an IBM SOA Approach to Manual Processes Automation
PPTX
XML - Displaying Data ith XSLT
PDF
SOA Governance and WebSphere Service Registry and Repository
ODP
Interoperable Web Services with JAX-WS
PPT
Open Id, O Auth And Webservices
PDF
XSLT for Web Developers
PPT
Web Services
PDF
Web services
PPT
WebService-Java
PPT
Siebel Web Service
PPTX
PPT
RESTful services
PDF
Java web services using JAX-WS
PPTX
XSLT
PDF
OAuth 2.0 with IBM WebSphere DataPower
PDF
SOAP-based Web Services
Xml part 6
Web Services
Unleashing the Power of XSLT: Catalog Records in Batch
The Mystical Principles of XSLT: Enlightenment through Software Visualization
Applying an IBM SOA Approach to Manual Processes Automation
XML - Displaying Data ith XSLT
SOA Governance and WebSphere Service Registry and Repository
Interoperable Web Services with JAX-WS
Open Id, O Auth And Webservices
XSLT for Web Developers
Web Services
Web services
WebService-Java
Siebel Web Service
RESTful services
Java web services using JAX-WS
XSLT
OAuth 2.0 with IBM WebSphere DataPower
SOAP-based Web Services
Ad

Similar to Xml part5 (20)

DOC
DOC
PPT
PPTX
XSLT - Extensible StyleSheet Language Transformations.pptx
PPT
PDF
Service Oriented Architecture - Unit II
PPT
XSLT.ppt
PPT
Week 12 xml and xsl
PDF
XSL- XSLT.pdf
PPTX
PPTX
XSLT presentation
PDF
Xsl xslt
PPT
Learning XSLT
PPTX
XSL - XML STYLE SHEET
PPT
C:\fakepath\xsl final
PPT
Rendering XML Documents
PPT
Xml p5 Lecture Notes
PPTX
transforming xml using xsl and xslt
PPTX
eXstensible Sylesheet Language_simple.pptx
PPT
5 xsl (formatting xml documents)
XSLT - Extensible StyleSheet Language Transformations.pptx
Service Oriented Architecture - Unit II
XSLT.ppt
Week 12 xml and xsl
XSL- XSLT.pdf
XSLT presentation
Xsl xslt
Learning XSLT
XSL - XML STYLE SHEET
C:\fakepath\xsl final
Rendering XML Documents
Xml p5 Lecture Notes
transforming xml using xsl and xslt
eXstensible Sylesheet Language_simple.pptx
5 xsl (formatting xml documents)

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
KodekX | Application Modernization Development
PDF
Approach and Philosophy of On baking technology
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Modernizing your data center with Dell and AMD
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Empathic Computing: Creating Shared Understanding
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
NewMind AI Weekly Chronicles - August'25 Week I
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Electronic commerce courselecture one. Pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
NewMind AI Monthly Chronicles - July 2025
Review of recent advances in non-invasive hemoglobin estimation
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Network Security Unit 5.pdf for BCA BBA.
Dropbox Q2 2025 Financial Results & Investor Presentation
KodekX | Application Modernization Development
Approach and Philosophy of On baking technology
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Modernizing your data center with Dell and AMD
“AI and Expert System Decision Support & Business Intelligence Systems”
Building Integrated photovoltaic BIPV_UPV.pdf
cuic standard and advanced reporting.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Empathic Computing: Creating Shared Understanding
CIFDAQ's Market Insight: SEC Turns Pro Crypto

Xml part5

  • 2. The Limitations of CSS When presenting XML data on a browser, we use CSS files, however CSS files have the following limitations: ● You can only display element values (you cannot display attribute values). ● You cannot add additional text. ● You cannot control the content of the XML file, you can only display it as it is. ● Difficult to display images or insert links.
  • 3. XSL XSL stands for “Extensible Stylesheet Language”. XSL specifies how the XML document will be presented. XSL composed in to two parts with each part acting as a separate language: 1. XSLT (XSL Transformations 2. XSL-FO (XSL- Formatting Objects)  XSL allows you to transform your XML data into a variety of formats, including HTML, XHTML, Portable Document Format (PDF), Rich Text Format (RTF), and even a new XML document.
  • 4. XSLT Features:  An XSLT stylesheet is an XML document (with elements and attributes), and it is used to transform the contents of an XML document into another document format.  The extension of an XSLT file is .xsl  An XSLT processor transforms an XML document into another output format (ex: XML, XHTML, RTF). by default, an XSLT processor renders the result document as an XML file.  An XSLT processor is included in some browsers, including Internet Explorer and Firefox. It can also take place on a server. * In this course, we will use a browser to transform XML to XML with formatting (the default transformation option).
  • 6. Creating an XSLT StyleSheet
  • 7. Attaching an XSLT StyleSheet <?xml-stylesheet type=”text/xsl” href=”url” ?>  Recall that when we applied a CSS file to an XML file we added the following processing instruction: <?xml-stylesheet type=”text/css” href=”style.css” ?>
  • 8. XPath  The path to reach a certain node in the xml document.  In XSLT, the content of an XML document is organized into nodes: o Document node o Processing instruction nodes o Element nodes o Attribute nodes o Text nodes  The following are not considered as nodes:  The XML declaration.  A CDATA section.  A DOCTYPE declaration (ex: internal DTD).
  • 12. 1- The “template” element  XSLT applys style to the XML document by applying templates to XML tags using the match attribute. Inside the template element, we use: 1- HTML elements to apply formatting 2- XSLT elements to select elements from the XML document and control the content of the resulting XML document.  The match attribute allows you to select the XML elements which you want to apply the style or template to.  The match attribute contains XPath expressions to determine which element you want to apply style to. * note: In this course, we will match the template to the root element only.
  • 13. 2- The “value-of” element  To insert a node’s value into the result document, you use the XSLT element <xsl:value-of select="expression" /> where expression is an XPath expression that identifies the node from the source document’s node tree.
  • 14. <?xml version=“1.0”?> <?xml-stylesheet type=“text/xsl” href=“customers.xsl”?> <customers> <customer custID="b123"> <name title="Mr.">Ahmed</name> <address>40 Kent st.</address> <phone>3456123</phone> </customer> <customer custID="C136"> <name title="Mr.">Khaled</name> <address>14 bronson st.</address> <phone>3456123</phone> </customer> <customer custID="D234"> <name title="Mrs.">Mona</name> <address>17 Aberdeen st.</address> <phone>3451111</phone> </customer> </customers>
  • 15. <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <html> <head> Customers </head> <body style="background-color: LightGray ; font-family: Cambria"> <h1> this is a list of customers: </h1> <p><xsl:value-of select="customers/customer/name"/></p> <p><xsl:value-of select="customers/customer/address"/></p> <p><xsl:value-of select="customers/customer/phone"/></p> </body> </html> </xsl:template> </xsl:stylesheet>
  • 17. The “value-of” element The value-of element extracts a value from the XML document. Since we have 3 customers in our example, only the first value was extracted. If we specifically wanted the second customer, the Xpath expression would be: customers/customer[2]
  • 18. <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <html> <head> Customers </head> <body style="background-color: LightGray ; font-family: Cambria"> <h1> this is a list of customers: </h1> <p><xsl:value-of select="customers/customer[2]/name"/></p> <p><xsl:value-of select="customers/customer[2]/address"/></p> <p><xsl:value-of select="customers/customer[2]/phone"/></p> </body> </html> </xsl:template> </xsl:stylesheet>
  • 20. 3-The “for-each” element  The for-each element is like a loop, it allows you to loop through multiple occurrences of an element.
  • 21. <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <html> <head> Customers </head> <body style="background-color: LightGray ; font-family: Cambria"> <xsl:for-each select="customers/customer"> <p><xsl:value-of select="name"/><br/> <xsl:value-of select="address"/><br/> <xsl:value-of select="phone"/> </p> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet>
  • 23. Look at the screen first, then try to guess the code
  • 25. <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <html> <head> Customers </head> <body style="background-color: LightGray ; font-family: Cambria"> <xsl:for-each select="customers/customer"> <p>Name: <xsl:value-of select="name"/><br/> Address: <xsl:value-of select="address"/><br/> Phone: <xsl:value-of select="phone"/> </p> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet>
  • 27. <xsl:template match="/"> <html> <body style="background-color: LightGray ; font-family: Cambria"> <table border="1" cellpadding="8"> <tr> <th>Name</th> <th>Address</th> <th>Phone</th> </tr> <xsl:for-each select="customers/customer"> <tr> <td><xsl:value-of select="name"/></td> <td><xsl:value-of select="address"/></td> <td><xsl:value-of select="phone"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template>
  • 28. What about attributes? So far, we used XSLT to display elements in our XML file. How do we display the attributes? @attribute root-element/child-element/@attribute
  • 29. <?xml version=“1.0”?> <?xml-stylesheet type=“text/xsl” href=“customers.xsl”?> <customers> <customer custID="b123"> <name title="Mr.">Ahmed</name> <address>40 Kent st.</address> <phone>3456123</phone> </customer> <customer custID=" C136"> <name title="Mr.">Khaled</name> <address>14 bronson st.</address> <phone>3456123</phone> </customer> <customer custID="D234"> <name title="Mrs.">Mona</name> <address>17 Aberdeen st.</address> <phone>3451111</phone> </customer> </customers>
  • 30. <xsl:template match="/"> <html> <body style="background-color: LightGray ; font-family: Cambria"> <table border="1" cellpadding="8"> <tr> <th>Name</th> <th>Address</th> <th>Phone</th> </tr> <xsl:for-each select="customers/customer"> <tr> <td><xsl:value-of select="name/@title"/> <xsl:value-of select="name"/></td> <td><xsl:value-of select="address"/></td> <td><xsl:value-of select="phone"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template>
  • 32. <table border="1" cellpadding="8"> <tr> <th>Customer ID</th> <th>Name</th> <th>Address</th> <th>Phone</th> </tr> <xsl:for-each select="customers/customer"> <tr> <td><xsl:value-of select="@custID"/></td> <td><xsl:value-of select="name/@title"/><xsl:value-of select="name"/></td> <td><xsl:value-of select="address"/></td> <td><xsl:value-of select="phone"/></td> </tr> </xsl:for-each> </table>
  • 34. Using the Double-forward Slash “//”  The Path to a certain node begins with the root node and proceeds down to the selected node: root-node/child-node/sub-child-node To avoid listing all of the levels of a node tree, use a double forward slash (//): //selected-node  And for attributes: //@attribute
  • 35. Using the Double-forward Slash “//” When we have more than one occurrence of the same element, XPath will return the first one it finds. If we want to specify which occurrence we want, we specify it: //selected-node[3] If we want to loop over all occurrences, we use the “for-each” element.
  • 36. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <html> <head> Customers </head> <body style="background-color: LightGray ; font-family: Cambria"> <h1> this is a list of customers: </h1> <p><xsl:value-of select="customers/customer/name"/></p> <p><xsl:value-of select="customers/customer/address"/></p> <p><xsl:value-of select="customers/customer/phone"/></p> </body> </html> </xsl:template> </xsl:stylesheet>
  • 37. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <html> <head> Customers </head> <body style="background-color: LightGray ; font-family: Cambria"> <h1> this is a list of customers: </h1> <p><xsl:value-of select="//name"/></p> <p><xsl:value-of select="//address"/></p> <p><xsl:value-of select="//phone"/></p> </body> </html> </xsl:template> </xsl:stylesheet> Here we used “//” to abbreviate the XPath expression.
  • 38. The Context Node  When XPath stops at a certain node, we call it the “context node”. We can refer to it using the “.” symbol.  Example: <xsl:for-each select=“//selected-node”> <xsl:value-of select=“.”/> </xsl:for-each> This code will print all the text contained within the context node.  If the context node contains child elements, all their values will be printed. (Attribute values will not be printed, only child element values will print out)
  • 39. <xsl:template match="/"> <html> <body> <xsl:for-each select="//Magazine"> <xsl:value-of select="."/><br/> </xsl:for-each> </body> </html> </xsl:template>
  • 41. XSLT Conditional Elements: If Element  The If element syntax: <xsl:if test=“ test “> … </xsl:if>  Where test is an XPath expression that is either true or false. If the test is true, the XSLT style commands are generated by the processor; otherwise, nothing is done.
  • 42. <customers> <customer custID="b123"> <name title="Mr.">Khaled</name> <address>40 Kent st.</address> <phone>3456123</phone> <gender>male</gender> </customer> <customer custID="C136"> <name title="Mrs.">Mona</name> <address>14 Bronson st.</address> <phone>3456123</phone> <gender>female</gender> </customer> <customer custID="b123"> <name title="Mr.">Ahmed</name> <address>40 Kent st.</address> <phone>3456123</phone> <gender>male</gender> </customer> <customer custID="D234"> <name title="Mrs.">lama</name> <address>17 Aberdeen st.</address> <phone>3451111</phone> <gender>female</gender> </customer> </customers> notice that we added a new element “gender”
  • 43. <xsl:for-each select="//customer"> <xsl:if test="gender = 'female'"> ID: <xsl:value-of select="@custID"/><br/> Name: <xsl:value-of select="name/@title"/> <xsl:value-of select="name"/><br/> Address: <xsl:value-of select="address"/><br/> Phone: <xsl:value-of select="phone"/> <br/><br/> </xsl:if> </xsl:for-each>
  • 45. XSLT Conditional Elements: Choose Element <xsl:choose> <xsl:when test=“ test “> … </xsl:when> <xsl:otherwise> … </xsl:otherwise> </xsl:choose>
  • 46. <xsl:for-each select="//customer"> <xsl:choose> <xsl:when test="gender = 'female'"> <div style="color:red"> ID: <xsl:value-of select="@custID"/><br/> Name: <xsl:value-of select="name/@title"/> <xsl:value-of select="name"/><br/> Address: <xsl:value-of select="address"/><br/> Phone: <xsl:value-of select="phone"/></div> </xsl:when> <xsl:otherwise> <div style="color:blue"> ID: <xsl:value-of select="@custID"/><br/> Name: <xsl:value-of select="name/@title"/> <xsl:value-of select="name"/><br/> Address: <xsl:value-of select="address"/><br/> Phone: <xsl:value-of select="phone"/></div> </xsl:otherwise> </xsl:choose> </xsl:for-each> XSLT Conditional Elements: Choose Element
  • 47. XSLT Conditional Elements: Choose Element
  • 48. Conditional Operators • Comparisons can be made between numbers, text strings, attribute nodes, element nodes, or text nodes.
  • 49. Conditional Operators Examples  Example 1 : day &lt; 5 you write it as 5 > day  Comparison tests can be combined using the “and” and “or” operators. For example: day > 2 and day &lt; 5 (tests whether the value of the day element lies between 2 and 5.)  Similarly, the expression @symbol = "AA" or @symbol = "UCL” (tests whether the value of the symbol attribute is equal to “AA” or “UCL”.)
  • 50. Working with Predicates  A predicate is part of a location path that tests for a condition and references the node set that fulfills that condition. The general syntax for a predicate is node [expression]  Example:  name[3] Select the third name element in customers.  name[@title = ‘Mr.’]( match all the name who title is Mr.)  sName[@symbol = "AA" or @symbol="UCL”] (matches all sName elements whose symbol attribute is equal to either “AA” or “UCL”.)
  • 51. Predicates and Using XPath Functions A predicate can contain XPath functions:  last() returns the last node in the node tree Example: book[last()] returns the last book  position() returns the position value of the node Example: book[position()>=2 and position()&lt;=5] returns the second, third, fourth and fifth book *note: book[position()=2] is equivalent to book[2]
  • 52. More XPath Functions Numeric Functions: sum() count() String Functions concat(string 1, string 2, string 3,…)  Combines string1, string2, string3, ... into a single text string.
  • 53. Count() & Sum() XPath Functions
  • 54. Count() & Sum() XPath Functions
  • 55. Count() & Sum() XPath Functions
  • 56. <table border="1" cellpadding="8"> <tr> <th>Name</th> <th>Customer ID</th> <th>Address</th> <th>Phone</th> </tr> <xsl:for-each select="//customer"> <tr> <td><xsl:value-of select="concat(name/@title,' ',name)"/></td> <td><xsl:value-of select="@custID"/></td> <td><xsl:value-of select="address"/></td> <td><xsl:value-of select="phone"/></td> </tr> </xsl:for-each> </table> Using Concat() XPath Functions
  • 57. Using Concat() XPath Functions
  • 58. Working with Mathematical Operators You can use mathematical operators inside the Xpath expressions to calculate new values based on the element or attribute values from the XML document.
  • 60. <xsl:template match="/"> <html> <body> <xsl:for-each select="//product"> <xsl:value-of select="name"/><br/> <xsl:value-of select="price*quantity"/><br/><br/> </xsl:for-each> total number of items=<xsl:value-of select=“sum(//quantity)”/> number of products:<xsl:value-of select=“count(//product)”/> </body> </html> </xsl:template>
  • 62. Sorting Nodes: Using the “sort” Element
  • 63. Sorting Nodes  To sort nodes, use the XSLT element “sort” with the “for-each” element. <xsl:sort select=“expression” data-type=“type” order=“type”/>  The select attribute determines the Criteria under which the context node is sorted.  The data-type attribute indicates the type of data (text or number).  The order attribute indicates the direction of sorting (ascending or descending).
  • 64. <breakfast_menu> <order orderID="M24"> <name type="sweet">Belgian Waffles</name> <price>5.95</price> </order> <order orderID="Q33“> <name type="sweet">Strawberry Belgian Waffles</name> <price>7.95</price> </order> <order orderID="K50“> <name type="salty">Eggs Benedict</name> <price>6.95</price> </order> <order orderID="B12“> <name type="sweet">Berry-Berry Belgian Waffles</name> <price>8.95</price> </order> <order orderID="G56“> <name type="sweet">French Toast with Maple Syrup</name> <price>4.50</price> </order> <order orderID="K23“> <name type="salty">Homestyle Breakfast</name> <price>6.95</price> </order> </breakfast_menu>
  • 65. <h3>Orders sorted by name:</h3> <xsl:for-each select="//order"> <xsl:sort select="name" data-type="text" order="ascending"/> <xsl:value-of select="name"/> <br/> </xsl:for-each>
  • 66. Default Values  The default value for the select attribute is the context node. Therefore, if you do not include the select attribute, XSLT processors assume you want to sort the values of the context node.  The default value for the data-type attribute is text.  The default value for the order attribute is ascending.
  • 67. <h3>Orders sorted by name:</h3> <xsl:for-each select="//name"> <xsl:sort/> <xsl:value-of select="."/> <br/> </xsl:for-each> Relying on the default attribute values will return the same output:
  • 68. <h3>Orders sorted by price, descending:</h3> <xsl:for-each select="//order"> <xsl:sort select="price" data-type="number" order="descending"/> <xsl:value-of select="name"/>: <xsl:value-of select="price"/> <br/> </xsl:for-each>
  • 69. <h3>Orders sorted by ID:</h3> <xsl:for-each select="//order"> <xsl:sort select="@orderID" data-type="text"/> <xsl:value-of select="@orderID"/>: <xsl:value-of select ="name"/> <br/> </xsl:for-each>
  • 70. <h3>Orders sorted by type, then name:</h3> <xsl:for-each select="//order"> <xsl:sort select="name/@type"/> <xsl:sort select="name"/> <xsl:value-of select="name/@type"/>: <xsl:value-of select="name"/> <br/> </xsl:for-each> Here we used a double sort: first sort based on the type, then sort based on the name.
  • 71. Element Attribute xsl:stylesheet xmlns:xsl , version xsl:template match xsl:value-of select xsl:for-each select xsl:if test xsl:choose - xsl:when test xsl:otherwise - xsl:sort select , data-type , order
  • 72. XSLT root element: <?xml version= "1.0" encoding= "UTF-8"?> <xsl:stylesheet xmlns:xsl= "http://www.w3.org/1999/XSL/Transform" version= "1.0"> </xsl:stylesheet> Linking an XML file to a StyleSheet: <?xml-stylesheet type= "text/xsl" href= "customers.xsl"?> Schema root element: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs= "http://www.w3.org/2001/XMLSchema" > </xs:schema>  Linking an XML file to a XML Schema <students xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "studentsSchema.xsd“ >