SlideShare a Scribd company logo
Transforming XML with XSLT
Basics and understanding
Malintha Adikari
Software Engineer
What is XSLT
XSL stands for
EXtensible Stylesheet Language, and is a style
sheet language for XML documents.
XSLT stands for
XSL Transformations
XSL vs CSS
Goals of XSLT
● XSLT is a transformation language for XML
● XSLT is a W3C XML language (the usual XML well-formedness criteria apply)
● XSLT can translate XML into almost anything , e.g.:
○ well-formed HTML (closed tags)
○ any XML, e.g. yours or other XML languages like SVG, X3D
○ non XML, e.g. PDF,ZIP...etc
XML Transformation
XML, XHTML, PDF, Plain text….
Why transforming
o Filter
Why transforming
o Presentations
Why transforming
o Formatting
XPath Recap..
XPath uses path expressions to navigate in XML documents
Let’s try following example
XPath Recap..
Example 1:
Example 2:
Start with XSLT
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
……..
</xsl:stylesheet>
● XSLT program is XML document
● XML declaration on top of the file
● A stylesheet root tag with the following
version and namespace attributes (The root
element that declares the document to be an
XSL style sheet is <xsl:stylesheet> or <xsl:
transform>)
● Usually the "xsl" prefix is used for XSLT tags,
but you also can find "xs" or none
● XSLT must be well-formed and valid
● XSLT files usually have the *.xsl extension
and should have the text/xsl or
application/xml mime type
● XSLT uses XPath to find information in an
Associate XML file and XSLT file
You can directly associate a XSLT stylesheet with an XML file by using a so-called
processing instruction
o Most programming languages and all well-know server-side scripting languages include
an XSLT library, so you can tell program to apply an XSLT stylesheet to an XML file
o use a xml-stylesheet instruction if you plan to display XML contents directly in a browser
Example
XML file
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
</catalog>
Expected output (HTML)
Example
XML file
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="myStyle.xsl"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
</catalog>
XSL file (myStyle.xsl)
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Title</th>
<th style="text-align:left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
How XSLT works
1. The XSLT engine first looks at the XML file and tries to find a XLT rule that will match the root
element
2. The XSLT processor will then "move" inside the rule element and do further processing
o HTML elements (or any other tags) will be copied to the output document
o If an XSLT instruction is found, it will be executed
● XSLT works down "depth-first" the XML tree, i.e.
● it first deals with the rule for the root element,
● then with the first instruction within this rule.
● If the first instruction says "find other rules" it will then apply the first rule found for the first
child element and so forth...
● The rule of the root element is also the last one be finished (since it must deal step-by-step
with everything that is found inside) !!!
Example
XML file
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="myStyle.xsl"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
</catalog>
XSL file (myStyle.xsl)
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Title</th>
<th style="text-align:left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XSLT elements
<xsl:template> Element
The <xsl:template> element is used to build templates.The
match attribute is used to associate a template with an XML
element. The value of the match attribute is an XPath
expression
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.
org/1999/XSL/Transform">
<xsl:template match="tool">
……….
</xsl:template>
<xsl:template match="paint">
……….
</xsl:template>
<?xml version="1.0" encoding="UTF-8"?>
<tool>
<field id="prodName">
<value>HAMMER HG2606</value>
</field>
</tool>
<paint>
<field id="prodName">
<value>mypaint001</value>
</field>
</paint>
XSLT elements
<xsl:value-of> Element
The <xsl:value-of> element can be used to extract the value of
an XML element and add it to the output stream of the
transformation
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<h1>
<xsl:value-of select="tool/field[@id=’prodName’]/value"/>
</h1>
</xsl:template>
<?xml version="1.0" encoding="UTF-8"?>
<tool>
<field id="prodName">
<value>HAMMER HG2606</value>
</field>
</tool>
<paint>
<field id="prodName">
<value>mypaint001</value>
</field>
</paint>
XSLT elements
<xsl:for-each> Element
The XSL <xsl:for-each> element can be used to select every
XML element of a specified node-set
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My Collection</h2>
<table border="1">
<th>Tool</th>
<th>Value</th>
</tr>
<xsl:for-each select="tool">
<tr>
<td><xsl:value-of select="field[@id]"/></td>
<td><xsl:value-of select="field/value"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<tool>
<field id="prodName">
<value>HAMMER HG2606</value>
</field>
</tool>
<tool>
<field id="prodName">
<value>myTool</value>
</field>
</tool>
XSLT elements
<xsl:apply-templates> Element
The <xsl:apply-templates> element applies a template to the
current element or to the current element's child nodes.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates select=”cd”/>
</body>
</html>
</xsl:template>
<xsl:template match="cd">
<p>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="artist"/>
</p>
</xsl:template>
<xsl:template match="title">
Title: <span style="color:#ff0000">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
<xsl:template match="artist">
Artist: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
</xsl:stylesheet>
<<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
</catalog>
XSLT elements
The <xsl:if> Element
To put a conditional if test against the content of the XML file,
add an <xsl:if> element to the XSL document.
Syntax
<xsl:if test="expression">
...some output if the expression is true...
</xsl:if>
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.
org/1999/XSL/Transform">
<xsl:template match="/">
…………
<xsl:for-each select="catalog/cd">
<xsl:if test="price>10">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
XSLT elements
XSLT <xsl:sort> Element
The <xsl:sort> element is used to sort the output.
For more XSLT elements and functions :
http://www.w3schools.com/xsl/default.asp
XSLT <xsl:choose> Element
The <xsl:choose> element is used in conjunction with
<xsl:when> and <xsl:otherwise> to express multiple
conditional tests.
Syntax
<xsl:choose>
<xsl:when test="expression">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>
Exercises
1. Download “automation.xml” file from following svn location
https://svn.wso2.org/repos/wso2/carbon/platform/branches/turing/platform-integration/test-automation-framework-2/org.wso2.
carbon.automation.engine/4.3.0/src/main/resources/automation.xml
2. Go to user management section and look at it’s current structure
3. Every “tenant” element has “admin”child and “admin”child has “user” element. You have to get that user element and put it in to
“users” element. New structure should look like this
<tenant domain="wso2.com" key="wso2">
<users>
<user key="admin">
<userName>admin</userName>
<password>admin</password>
</user>
<user key="user1">
<userName>testuser11</userName>
<password>testuser11</password>
</user>
</users>
</tenant>
Other elements remains same as original file. Write the output into new xml file.
Questions?
Contact us !

More Related Content

What's hot

Html Frames
Html FramesHtml Frames
Html Frames
Xainab Ishfaq
 
Dtd
DtdDtd
CSS
CSSCSS
Xml
XmlXml
HTML Frameset & Inline Frame
HTML Frameset & Inline FrameHTML Frameset & Inline Frame
HTML Frameset & Inline Frame
Nisa Soomro
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
Xml
XmlXml
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
Kasun Madusanke
 
Files in java
Files in javaFiles in java
Cascading Style Sheets - Part 01
Cascading Style Sheets - Part 01Cascading Style Sheets - Part 01
Cascading Style Sheets - Part 01
Hatem Mahmoud
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
Abhilash Nair
 
01 xml document structure
01 xml document structure01 xml document structure
01 xml document structure
Baskarkncet
 
XML, DTD & XSD Overview
XML, DTD & XSD OverviewXML, DTD & XSD Overview
XML, DTD & XSD Overview
Pradeep Rapolu
 
Html audio video
Html audio videoHtml audio video
Html audio video
Muhammad Ehtisham Siddiqui
 
computer language - Html frames
computer language - Html framescomputer language - Html frames
computer language - Html frames
Dr. I. Uma Maheswari Maheswari
 
Bootstrap PPT Part - 2
Bootstrap PPT Part - 2Bootstrap PPT Part - 2
Bootstrap PPT Part - 2
EPAM Systems
 
Visual basic 6.0
Visual basic 6.0Visual basic 6.0
Visual basic 6.0
Aarti P
 
Xml presentation
Xml presentationXml presentation
Xml presentation
Miguel Angel Teheran Garcia
 
Html frames
Html framesHtml frames
Html frames
eShikshak
 
Introduction to CSS
Introduction to CSSIntroduction to CSS
Introduction to CSS
Amit Tyagi
 

What's hot (20)

Html Frames
Html FramesHtml Frames
Html Frames
 
Dtd
DtdDtd
Dtd
 
CSS
CSSCSS
CSS
 
Xml
XmlXml
Xml
 
HTML Frameset & Inline Frame
HTML Frameset & Inline FrameHTML Frameset & Inline Frame
HTML Frameset & Inline Frame
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Xml
XmlXml
Xml
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Files in java
Files in javaFiles in java
Files in java
 
Cascading Style Sheets - Part 01
Cascading Style Sheets - Part 01Cascading Style Sheets - Part 01
Cascading Style Sheets - Part 01
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
01 xml document structure
01 xml document structure01 xml document structure
01 xml document structure
 
XML, DTD & XSD Overview
XML, DTD & XSD OverviewXML, DTD & XSD Overview
XML, DTD & XSD Overview
 
Html audio video
Html audio videoHtml audio video
Html audio video
 
computer language - Html frames
computer language - Html framescomputer language - Html frames
computer language - Html frames
 
Bootstrap PPT Part - 2
Bootstrap PPT Part - 2Bootstrap PPT Part - 2
Bootstrap PPT Part - 2
 
Visual basic 6.0
Visual basic 6.0Visual basic 6.0
Visual basic 6.0
 
Xml presentation
Xml presentationXml presentation
Xml presentation
 
Html frames
Html framesHtml frames
Html frames
 
Introduction to CSS
Introduction to CSSIntroduction to CSS
Introduction to CSS
 

Viewers also liked

XML Security Using XSLT
XML Security Using XSLTXML Security Using XSLT
XML Security Using XSLT
Ahmed Muzammil
 
Xslt
XsltXslt
transforming xml using xsl and xslt
transforming xml using xsl and xslttransforming xml using xsl and xslt
transforming xml using xsl and xslt
Hemant Suthar
 
Introduction to XSLT
Introduction to XSLTIntroduction to XSLT
Introduction to XSLT
Mahmoud Allam
 
Learning XSLT
Learning XSLTLearning XSLT
Learning XSLT
Overdue Books LLC
 
Devouring Security XML Attack surface and Defences
Devouring Security XML Attack surface and DefencesDevouring Security XML Attack surface and Defences
Devouring Security XML Attack surface and Defences
gmaran23
 
XSLT
XSLTXSLT
XSLT
rpoplai
 

Viewers also liked (7)

XML Security Using XSLT
XML Security Using XSLTXML Security Using XSLT
XML Security Using XSLT
 
Xslt
XsltXslt
Xslt
 
transforming xml using xsl and xslt
transforming xml using xsl and xslttransforming xml using xsl and xslt
transforming xml using xsl and xslt
 
Introduction to XSLT
Introduction to XSLTIntroduction to XSLT
Introduction to XSLT
 
Learning XSLT
Learning XSLTLearning XSLT
Learning XSLT
 
Devouring Security XML Attack surface and Defences
Devouring Security XML Attack surface and DefencesDevouring Security XML Attack surface and Defences
Devouring Security XML Attack surface and Defences
 
XSLT
XSLTXSLT
XSLT
 

Similar to Transforming xml with XSLT

XSLT. Basic.
XSLT. Basic.XSLT. Basic.
XSLT. Basic.
Alexander Kirillov
 
"Getting Started with XSLT" presentation slides
"Getting Started with XSLT" presentation slides"Getting Started with XSLT" presentation slides
"Getting Started with XSLT" presentation slides
Russell Ward
 
Xslt
XsltXslt
The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld
The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld
The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld
BookNet Canada
 
Xslt
XsltXslt
Xslt
XsltXslt
XSLT
XSLTXSLT
5 xsl (formatting xml documents)
5   xsl (formatting xml documents)5   xsl (formatting xml documents)
5 xsl (formatting xml documents)
gauravashq
 
XSLT for Web Developers
XSLT for Web DevelopersXSLT for Web Developers
XSLT for Web Developers
Sanders Kleinfeld
 
Xml part5
Xml part5Xml part5
Xml part5
NOHA AW
 
XSLT.ppt
XSLT.pptXSLT.ppt
XSLT.ppt
KGSCSEPSGCT
 
Xml p5 Lecture Notes
Xml p5 Lecture NotesXml p5 Lecture Notes
Xml p5 Lecture Notes
Santhiya Grace
 
eXtensible Markup Language (XML)
eXtensible Markup Language (XML)eXtensible Markup Language (XML)
eXtensible Markup Language (XML)
Serhii Kartashov
 
Xml transformation language
Xml transformation languageXml transformation language
Xml transformation language
reshmavasudev
 
IWMW 2001: XML and XSLT
IWMW 2001: XML and XSLT IWMW 2001: XML and XSLT
IWMW 2001: XML and XSLT
IWMW
 
Rancangan Jaringan Komputer
Rancangan Jaringan KomputerRancangan Jaringan Komputer
Rancangan Jaringan Komputer
Candra Adi Putra
 
Service Oriented Architecture - Unit II
Service Oriented Architecture - Unit IIService Oriented Architecture - Unit II
Service Oriented Architecture - Unit II
Roselin Mary S
 
Xml
XmlXml
Xml nisha dwivedi
Xml nisha dwivediXml nisha dwivedi
Xml nisha dwivedi
NIIT
 
XML XSLT
XML XSLTXML XSLT

Similar to Transforming xml with XSLT (20)

XSLT. Basic.
XSLT. Basic.XSLT. Basic.
XSLT. Basic.
 
"Getting Started with XSLT" presentation slides
"Getting Started with XSLT" presentation slides"Getting Started with XSLT" presentation slides
"Getting Started with XSLT" presentation slides
 
Xslt
XsltXslt
Xslt
 
The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld
The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld
The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld
 
Xslt
XsltXslt
Xslt
 
Xslt
XsltXslt
Xslt
 
XSLT
XSLTXSLT
XSLT
 
5 xsl (formatting xml documents)
5   xsl (formatting xml documents)5   xsl (formatting xml documents)
5 xsl (formatting xml documents)
 
XSLT for Web Developers
XSLT for Web DevelopersXSLT for Web Developers
XSLT for Web Developers
 
Xml part5
Xml part5Xml part5
Xml part5
 
XSLT.ppt
XSLT.pptXSLT.ppt
XSLT.ppt
 
Xml p5 Lecture Notes
Xml p5 Lecture NotesXml p5 Lecture Notes
Xml p5 Lecture Notes
 
eXtensible Markup Language (XML)
eXtensible Markup Language (XML)eXtensible Markup Language (XML)
eXtensible Markup Language (XML)
 
Xml transformation language
Xml transformation languageXml transformation language
Xml transformation language
 
IWMW 2001: XML and XSLT
IWMW 2001: XML and XSLT IWMW 2001: XML and XSLT
IWMW 2001: XML and XSLT
 
Rancangan Jaringan Komputer
Rancangan Jaringan KomputerRancangan Jaringan Komputer
Rancangan Jaringan Komputer
 
Service Oriented Architecture - Unit II
Service Oriented Architecture - Unit IIService Oriented Architecture - Unit II
Service Oriented Architecture - Unit II
 
Xml
XmlXml
Xml
 
Xml nisha dwivedi
Xml nisha dwivediXml nisha dwivedi
Xml nisha dwivedi
 
XML XSLT
XML XSLTXML XSLT
XML XSLT
 

Recently uploaded

Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
aymanquadri279
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
lorraineandreiamcidl
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
Ayan Halder
 
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise EditionWhy Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Envertis Software Solutions
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
Hornet Dynamics
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
Deuglo Infosystem Pvt Ltd
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
SOCRadar
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 

Recently uploaded (20)

Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
 
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise EditionWhy Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 

Transforming xml with XSLT

  • 1. Transforming XML with XSLT Basics and understanding Malintha Adikari Software Engineer
  • 2. What is XSLT XSL stands for EXtensible Stylesheet Language, and is a style sheet language for XML documents. XSLT stands for XSL Transformations
  • 4. Goals of XSLT ● XSLT is a transformation language for XML ● XSLT is a W3C XML language (the usual XML well-formedness criteria apply) ● XSLT can translate XML into almost anything , e.g.: ○ well-formed HTML (closed tags) ○ any XML, e.g. yours or other XML languages like SVG, X3D ○ non XML, e.g. PDF,ZIP...etc
  • 5. XML Transformation XML, XHTML, PDF, Plain text….
  • 9. XPath Recap.. XPath uses path expressions to navigate in XML documents Let’s try following example
  • 11. Start with XSLT <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> …….. </xsl:stylesheet> ● XSLT program is XML document ● XML declaration on top of the file ● A stylesheet root tag with the following version and namespace attributes (The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or <xsl: transform>) ● Usually the "xsl" prefix is used for XSLT tags, but you also can find "xs" or none ● XSLT must be well-formed and valid ● XSLT files usually have the *.xsl extension and should have the text/xsl or application/xml mime type ● XSLT uses XPath to find information in an
  • 12. Associate XML file and XSLT file You can directly associate a XSLT stylesheet with an XML file by using a so-called processing instruction o Most programming languages and all well-know server-side scripting languages include an XSLT library, so you can tell program to apply an XSLT stylesheet to an XML file o use a xml-stylesheet instruction if you plan to display XML contents directly in a browser
  • 13. Example XML file <?xml version="1.0" encoding="UTF-8"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>Greatest Hits</title> <artist>Dolly Parton</artist> <country>USA</country> <company>RCA</company> <price>9.90</price> <year>1982</year> </cd> </catalog> Expected output (HTML)
  • 14. Example XML file <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="myStyle.xsl"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>Greatest Hits</title> <artist>Dolly Parton</artist> <country>USA</country> <company>RCA</company> <price>9.90</price> <year>1982</year> </cd> </catalog> XSL file (myStyle.xsl) <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> <xsl:output method="html"/> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th style="text-align:left">Title</th> <th style="text-align:left">Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
  • 15. How XSLT works 1. The XSLT engine first looks at the XML file and tries to find a XLT rule that will match the root element 2. The XSLT processor will then "move" inside the rule element and do further processing o HTML elements (or any other tags) will be copied to the output document o If an XSLT instruction is found, it will be executed ● XSLT works down "depth-first" the XML tree, i.e. ● it first deals with the rule for the root element, ● then with the first instruction within this rule. ● If the first instruction says "find other rules" it will then apply the first rule found for the first child element and so forth... ● The rule of the root element is also the last one be finished (since it must deal step-by-step with everything that is found inside) !!!
  • 16. Example XML file <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="myStyle.xsl"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>Greatest Hits</title> <artist>Dolly Parton</artist> <country>USA</country> <company>RCA</company> <price>9.90</price> <year>1982</year> </cd> </catalog> XSL file (myStyle.xsl) <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> <xsl:output method="html"/> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th style="text-align:left">Title</th> <th style="text-align:left">Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
  • 17. XSLT elements <xsl:template> Element The <xsl:template> element is used to build templates.The match attribute is used to associate a template with an XML element. The value of the match attribute is an XPath expression <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3. org/1999/XSL/Transform"> <xsl:template match="tool"> ………. </xsl:template> <xsl:template match="paint"> ………. </xsl:template> <?xml version="1.0" encoding="UTF-8"?> <tool> <field id="prodName"> <value>HAMMER HG2606</value> </field> </tool> <paint> <field id="prodName"> <value>mypaint001</value> </field> </paint>
  • 18. XSLT elements <xsl:value-of> Element The <xsl:value-of> element can be used to extract the value of an XML element and add it to the output stream of the transformation <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <h1> <xsl:value-of select="tool/field[@id=’prodName’]/value"/> </h1> </xsl:template> <?xml version="1.0" encoding="UTF-8"?> <tool> <field id="prodName"> <value>HAMMER HG2606</value> </field> </tool> <paint> <field id="prodName"> <value>mypaint001</value> </field> </paint>
  • 19. XSLT elements <xsl:for-each> Element The XSL <xsl:for-each> element can be used to select every XML element of a specified node-set <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My Collection</h2> <table border="1"> <th>Tool</th> <th>Value</th> </tr> <xsl:for-each select="tool"> <tr> <td><xsl:value-of select="field[@id]"/></td> <td><xsl:value-of select="field/value"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> <?xml version="1.0" encoding="UTF-8"?> <tool> <field id="prodName"> <value>HAMMER HG2606</value> </field> </tool> <tool> <field id="prodName"> <value>myTool</value> </field> </tool>
  • 20. XSLT elements <xsl:apply-templates> Element The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes. <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <xsl:apply-templates select=”cd”/> </body> </html> </xsl:template> <xsl:template match="cd"> <p> <xsl:apply-templates select="title"/> <xsl:apply-templates select="artist"/> </p> </xsl:template> <xsl:template match="title"> Title: <span style="color:#ff0000"> <xsl:value-of select="."/></span> <br /> </xsl:template> <xsl:template match="artist"> Artist: <span style="color:#00ff00"> <xsl:value-of select="."/></span> <br /> </xsl:template> </xsl:stylesheet> <<?xml version="1.0" encoding="UTF-8"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> </catalog>
  • 21. XSLT elements The <xsl:if> Element To put a conditional if test against the content of the XML file, add an <xsl:if> element to the XSL document. Syntax <xsl:if test="expression"> ...some output if the expression is true... </xsl:if> <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3. org/1999/XSL/Transform"> <xsl:template match="/"> ………… <xsl:for-each select="catalog/cd"> <xsl:if test="price>10"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:if> </xsl:for-each> </xsl:template> </xsl:stylesheet>
  • 22. XSLT elements XSLT <xsl:sort> Element The <xsl:sort> element is used to sort the output. For more XSLT elements and functions : http://www.w3schools.com/xsl/default.asp XSLT <xsl:choose> Element The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests. Syntax <xsl:choose> <xsl:when test="expression"> ... some output ... </xsl:when> <xsl:otherwise> ... some output .... </xsl:otherwise> </xsl:choose>
  • 23. Exercises 1. Download “automation.xml” file from following svn location https://svn.wso2.org/repos/wso2/carbon/platform/branches/turing/platform-integration/test-automation-framework-2/org.wso2. carbon.automation.engine/4.3.0/src/main/resources/automation.xml 2. Go to user management section and look at it’s current structure 3. Every “tenant” element has “admin”child and “admin”child has “user” element. You have to get that user element and put it in to “users” element. New structure should look like this <tenant domain="wso2.com" key="wso2"> <users> <user key="admin"> <userName>admin</userName> <password>admin</password> </user> <user key="user1"> <userName>testuser11</userName> <password>testuser11</password> </user> </users> </tenant> Other elements remains same as original file. Write the output into new xml file.