SlideShare une entreprise Scribd logo
1  sur  17
XSLT presentation
By Alejandro herreño y Miguel Teheran
XSLT
● XSLT stands for Extensible Stylesheet Language
Transformations
● XSLT is used to transform XML documents into other
kinds of documents--usually, but not necessarily, XHTML
● XSLT uses two input files:
The XML document containing the actual data
The XSL document containing both the “framework” in which
to insert the data, and XSLT commands to do so
Simple example
File data.xml:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="render.xsl"?>
<message>Howdy!</message>
File render.xsl:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0”
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- one rule, to transform the input root (/) →
<xsl:template match="/">
<html><body>
<h1><xsl:value-of select="message"/></h1>
</body></html>
</xsl:template>
</xsl:stylesheet>
The .xsl file
An XSLT document has the .xsl extension
The XSLT document begins with:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
Contains one or more templates, such as:
<xsl:template match="/"> ... </xsl:template>
And ends with:
</xsl:stylesheet>
xsl:value-of
<xsl:value-of select="XPath expression"/> selects
the contents of an element and adds it to the output stream
The select attribute is required
Notice that xsl:value-of is not a container, hence it needs to
end with a slash
Example (from an earlier slide):
<h1> <xsl:value-of select="message"/> </h1>
xsl:for-each
xsl:for-each is a kind of loop statement
The syntax is
<xsl:for-each select="XPath expression">
Text to insert and rules to apply
</xsl:for-each>
Example: to select every book (//book) and make an unordered
list (<ul>) of their titles (title), use:
<ul> <xsl:for-each select="//book">
<li> <xsl:value-of select="title"/> </li> </xsl:for-each> </ul>
Filtering output
You can filter (restrict) output by adding a criterion
to the select attribute’s value:
<ul> <xsl:for-each select="//book">
<li>
<xsl:value-of select="title[../author='Terry Pratchett']"/>
</li> </xsl:for-each>
</ul>
This will select book titles by Terry Pratchett
Filter details
Here is the filter we just used:
<xsl:value-of select="title[../author='Terry Pratchett']"/>
author is a sibling of title, so from title we have to
go up to its parent, book, then back down to author
This filter requires a quote within a quote, so we need
both single quotes and double quotes
Legal filter operators are:
= != &lt; &gt;
Numbers should be quoted, but apparently don’t have to be
xsl:if
xsl:if allows us to include content if a given condition (in the test attribute) is true
Example:
<xsl:for-each select="//book">
<xsl:if test="author='Terry Pratchett'">
<li> <xsl:value-of select="title"/>
</li>
</xsl:if>
</xsl:for-each>
This does work correctly!
xsl:choose
The xsl:choose ... xsl:when ... xsl:otherwise
construct is XML’s equivalent of Java’s switch ... case ... default statement
The syntax is:
<xsl:choose>
<xsl:when test="some condition">
... some code ...
</xsl:when> <xsl:otherwise>
... some code ...
</xsl:otherwise>
</xsl:choose>
• xsl:choose is often used within an xsl:for-each loop
xsl:sort
You can place an xsl:sort inside an xsl:for-each
The attribute of the sort tells what field to sort on Example:
<ul>
<xsl:for-each select="//book">
<xsl:sort select="author"/>
<li>
<xsl:value-of select="title"/> by <xsl:value-of select="author"/>
</li>
</xsl:for-each>
</ul>
This example creates a list of titles and authors, sorted by author
xsl:text
<xsl:text>...</xsl:text>
XSL isn’t very careful with whitespace in the document
This doesn’t matter much for HTML, which collapses all whitespace anyway
(though the HTML source may look ugly)
<xsl:text> gives you much better control over whitespace; it acts like the <pre>
element in HTML
Quick questions
- What are the input files in XSLT?
- Mentions 2 commands in XSL
- ¿What does the command choose?
Applying templates to children
<book>
<title>XML</title>
<author>Gregory Brill</author>
</book>
<xsl:template match="/">
<html> <head></head> <body>
<b><xsl:value-of select="/book/title"/></b>
<xsl:apply-templates select="/book/author"/>
<xsl:apply-templates select="/book/title"/>
</body> </html>
</xsl:template>
<xsl:template match="/book/author">
by <i><xsl:value-of select="."/></i>
</xsl:template>
With this line:
XML by Gregory Brill
Without this line:
XML
Calling named templates
You can name a template, then call it, similar to the way you
would call a method in Java
The named template:
<xsl:template name="myTemplateName">
...body of template...
</xsl:template>
A call to the template:
<xsl:call-template name="myTemplateName"/>
Or:
<xsl:call-template name="myTemplateName">
...parameters...
</xsl:call-template>
Templates with parameters
Parameters, if present, are included in the content of
xsl:template, but are the only content of xsl:call-template
Example call:
<xsl:call-template name="doOneType">
<xsl:with-param name="header" select="'Lectures'"/>
<xsl:with-param name="nodes" select="//lecture"/> </xsl:call-template>
Example template:
<xsl:template name="doOneType"> <xsl:param name="header"/> <xsl:param name="nodes"/>
...template body...refer to parameters as "$header" and "$nodes" </xsl:template>
Parameters are matched up by name, not by position
Single quotes inside double
quotes make this a string
This parameter is a
typical XPath
expression
Thoughts on XSL
XSL is a programming language--and not a particularly simple one
Expect to spend considerable time debugging your XSL
These slides have been an introduction to XSL and XSLT--there’s a lot more of it we haven’t covered
As with any programming, it’s a good idea to start simple and build it up incrementally: “Write a little, test
a little”
This is especially a good idea for XSLT, because you don’t get a lot of
feedback about what went wrong
I use jEdit with the XML plugin
I find it to be a big help, especially with XML syntax
My approach is: write (or change) a line or two, check for syntax errors,
then jump to IE and reload the XML file

Contenu connexe

Tendances

Tendances (20)

Xml schema
Xml schemaXml schema
Xml schema
 
Xpath presentation
Xpath presentationXpath presentation
Xpath presentation
 
RDBMS concepts
RDBMS conceptsRDBMS concepts
RDBMS concepts
 
Xml http request
Xml http requestXml http request
Xml http request
 
02 xml schema
02 xml schema02 xml schema
02 xml schema
 
WSDL
WSDLWSDL
WSDL
 
SQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredSQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics Covered
 
XML
XMLXML
XML
 
MYSQL
MYSQLMYSQL
MYSQL
 
HTML frames and HTML forms
HTML frames and HTML formsHTML frames and HTML forms
HTML frames and HTML forms
 
AngularJS: an introduction
AngularJS: an introductionAngularJS: an introduction
AngularJS: an introduction
 
Types of Selectors (HTML)
Types of Selectors (HTML)Types of Selectors (HTML)
Types of Selectors (HTML)
 
Bootstrap 4 ppt
Bootstrap 4 pptBootstrap 4 ppt
Bootstrap 4 ppt
 
How Cascading Style Sheets (CSS) Works
How Cascading Style Sheets (CSS) WorksHow Cascading Style Sheets (CSS) Works
How Cascading Style Sheets (CSS) Works
 
XSLT
XSLTXSLT
XSLT
 
introduction to CSS
introduction to CSSintroduction to CSS
introduction to CSS
 
Introduction to JSON
Introduction to JSONIntroduction to JSON
Introduction to JSON
 
html-css
html-csshtml-css
html-css
 
Java script
Java scriptJava script
Java script
 
javaScript.ppt
javaScript.pptjavaScript.ppt
javaScript.ppt
 

Similaire à XSLT presentation

Xml transformation language
Xml transformation languageXml transformation language
Xml transformation language
reshmavasudev
 

Similaire à XSLT presentation (20)

Xslt
XsltXslt
Xslt
 
XSLT and XPath - without the pain!
XSLT and XPath - without the pain!XSLT and XPath - without the pain!
XSLT and XPath - without the pain!
 
Introduction of xml and xslt
Introduction of xml and xsltIntroduction of xml and xslt
Introduction of xml and xslt
 
Xml p5 Lecture Notes
Xml p5 Lecture NotesXml p5 Lecture Notes
Xml p5 Lecture Notes
 
Session 4
Session 4Session 4
Session 4
 
Xslt
XsltXslt
Xslt
 
Xslt
XsltXslt
Xslt
 
Service Oriented Architecture - Unit II
Service Oriented Architecture - Unit IIService Oriented Architecture - Unit II
Service Oriented Architecture - Unit II
 
XML XSLT
XML XSLTXML XSLT
XML XSLT
 
Integrative Programming and Technology Chapter 4- Dr. J. VijiPriya
Integrative Programming and Technology Chapter 4- Dr. J. VijiPriyaIntegrative Programming and Technology Chapter 4- Dr. J. VijiPriya
Integrative Programming and Technology Chapter 4- Dr. J. VijiPriya
 
eXtensible Markup Language (XML)
eXtensible Markup Language (XML)eXtensible Markup Language (XML)
eXtensible Markup Language (XML)
 
Xslt elements
Xslt elementsXslt elements
Xslt elements
 
Week 12 xml and xsl
Week 12 xml and xslWeek 12 xml and xsl
Week 12 xml and xsl
 
Xslt attributes
Xslt attributesXslt attributes
Xslt attributes
 
Xslt mule
Xslt   muleXslt   mule
Xslt mule
 
Xml 2
Xml  2 Xml  2
Xml 2
 
Xml transformation language
Xml transformation languageXml transformation language
Xml transformation language
 
Xml processing in scala
Xml processing in scalaXml processing in scala
Xml processing in scala
 
Xml processing in scala
Xml processing in scalaXml processing in scala
Xml processing in scala
 
Day2 xslt x_path_xquery
Day2 xslt x_path_xqueryDay2 xslt x_path_xquery
Day2 xslt x_path_xquery
 

Plus de Miguel Angel Teheran Garcia

Plus de Miguel Angel Teheran Garcia (20)

Pruebas Automatizadas con PlayWright sobre nuestras Google Cloud Functions
Pruebas Automatizadas con PlayWright sobre nuestras Google Cloud FunctionsPruebas Automatizadas con PlayWright sobre nuestras Google Cloud Functions
Pruebas Automatizadas con PlayWright sobre nuestras Google Cloud Functions
 
PlayWright, Cypress, Selenium Cual es la mejor opción para desarrolladores Ja...
PlayWright, Cypress, Selenium Cual es la mejor opción para desarrolladores Ja...PlayWright, Cypress, Selenium Cual es la mejor opción para desarrolladores Ja...
PlayWright, Cypress, Selenium Cual es la mejor opción para desarrolladores Ja...
 
Introduction to Blazor Hybrid
Introduction to Blazor HybridIntroduction to Blazor Hybrid
Introduction to Blazor Hybrid
 
La historia de api-colombia
La historia de api-colombiaLa historia de api-colombia
La historia de api-colombia
 
DevFest 2022 - El Arte de escribir sobre programacion.pptx
DevFest 2022 - El Arte de escribir sobre programacion.pptxDevFest 2022 - El Arte de escribir sobre programacion.pptx
DevFest 2022 - El Arte de escribir sobre programacion.pptx
 
RoadMap y herramientas de Azure DevOps que debes conocer
RoadMap y herramientas de Azure DevOps que debes conocerRoadMap y herramientas de Azure DevOps que debes conocer
RoadMap y herramientas de Azure DevOps que debes conocer
 
Taller de TDD con .NET y xUnit
Taller de TDD con .NET y xUnitTaller de TDD con .NET y xUnit
Taller de TDD con .NET y xUnit
 
Introduction to OpenTelemetry in .NET
Introduction to OpenTelemetry in .NETIntroduction to OpenTelemetry in .NET
Introduction to OpenTelemetry in .NET
 
PRISM con MAUI
PRISM con MAUIPRISM con MAUI
PRISM con MAUI
 
.NET MAUI Offline first
.NET MAUI Offline first .NET MAUI Offline first
.NET MAUI Offline first
 
MAUIConf - Adios Net Maui Essentials Bienvenida Integración de Plataforma
MAUIConf - Adios Net Maui Essentials Bienvenida Integración de PlataformaMAUIConf - Adios Net Maui Essentials Bienvenida Integración de Plataforma
MAUIConf - Adios Net Maui Essentials Bienvenida Integración de Plataforma
 
Servicios Nativos MAUI
Servicios Nativos MAUIServicios Nativos MAUI
Servicios Nativos MAUI
 
Aplicaciones para MacOS con .NET MAUI
Aplicaciones para MacOS con .NET MAUIAplicaciones para MacOS con .NET MAUI
Aplicaciones para MacOS con .NET MAUI
 
Primero pasos con Visual Studio for MAC
Primero pasos con Visual Studio for MACPrimero pasos con Visual Studio for MAC
Primero pasos con Visual Studio for MAC
 
Aplicaciones con web con Blazor + MudBlazor
Aplicaciones con web con Blazor + MudBlazorAplicaciones con web con Blazor + MudBlazor
Aplicaciones con web con Blazor + MudBlazor
 
Building Web Applications with Blazor and MudBlazor
Building Web Applications with Blazor and MudBlazorBuilding Web Applications with Blazor and MudBlazor
Building Web Applications with Blazor and MudBlazor
 
Tips para una entrevista Tech Exitosa
Tips para una entrevista Tech ExitosaTips para una entrevista Tech Exitosa
Tips para una entrevista Tech Exitosa
 
Metaverso y Microsoft Mesh
Metaverso y Microsoft MeshMetaverso y Microsoft Mesh
Metaverso y Microsoft Mesh
 
Mejoras en Blazor con .NET 6
Mejoras en Blazor con .NET 6Mejoras en Blazor con .NET 6
Mejoras en Blazor con .NET 6
 
Apis with dotnet postgreSQL and Apsaradb
Apis with dotnet postgreSQL and ApsaradbApis with dotnet postgreSQL and Apsaradb
Apis with dotnet postgreSQL and Apsaradb
 

Dernier

%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Dernier (20)

%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 

XSLT presentation

  • 1. XSLT presentation By Alejandro herreño y Miguel Teheran
  • 2. XSLT ● XSLT stands for Extensible Stylesheet Language Transformations ● XSLT is used to transform XML documents into other kinds of documents--usually, but not necessarily, XHTML ● XSLT uses two input files: The XML document containing the actual data The XSL document containing both the “framework” in which to insert the data, and XSLT commands to do so
  • 3. Simple example File data.xml: <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="render.xsl"?> <message>Howdy!</message> File render.xsl: <?xml version="1.0"?> <xsl:stylesheet version="1.0” xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- one rule, to transform the input root (/) → <xsl:template match="/"> <html><body> <h1><xsl:value-of select="message"/></h1> </body></html> </xsl:template> </xsl:stylesheet>
  • 4. The .xsl file An XSLT document has the .xsl extension The XSLT document begins with: <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> Contains one or more templates, such as: <xsl:template match="/"> ... </xsl:template> And ends with: </xsl:stylesheet>
  • 5. xsl:value-of <xsl:value-of select="XPath expression"/> selects the contents of an element and adds it to the output stream The select attribute is required Notice that xsl:value-of is not a container, hence it needs to end with a slash Example (from an earlier slide): <h1> <xsl:value-of select="message"/> </h1>
  • 6. xsl:for-each xsl:for-each is a kind of loop statement The syntax is <xsl:for-each select="XPath expression"> Text to insert and rules to apply </xsl:for-each> Example: to select every book (//book) and make an unordered list (<ul>) of their titles (title), use: <ul> <xsl:for-each select="//book"> <li> <xsl:value-of select="title"/> </li> </xsl:for-each> </ul>
  • 7. Filtering output You can filter (restrict) output by adding a criterion to the select attribute’s value: <ul> <xsl:for-each select="//book"> <li> <xsl:value-of select="title[../author='Terry Pratchett']"/> </li> </xsl:for-each> </ul> This will select book titles by Terry Pratchett
  • 8. Filter details Here is the filter we just used: <xsl:value-of select="title[../author='Terry Pratchett']"/> author is a sibling of title, so from title we have to go up to its parent, book, then back down to author This filter requires a quote within a quote, so we need both single quotes and double quotes Legal filter operators are: = != &lt; &gt; Numbers should be quoted, but apparently don’t have to be
  • 9. xsl:if xsl:if allows us to include content if a given condition (in the test attribute) is true Example: <xsl:for-each select="//book"> <xsl:if test="author='Terry Pratchett'"> <li> <xsl:value-of select="title"/> </li> </xsl:if> </xsl:for-each> This does work correctly!
  • 10. xsl:choose The xsl:choose ... xsl:when ... xsl:otherwise construct is XML’s equivalent of Java’s switch ... case ... default statement The syntax is: <xsl:choose> <xsl:when test="some condition"> ... some code ... </xsl:when> <xsl:otherwise> ... some code ... </xsl:otherwise> </xsl:choose> • xsl:choose is often used within an xsl:for-each loop
  • 11. xsl:sort You can place an xsl:sort inside an xsl:for-each The attribute of the sort tells what field to sort on Example: <ul> <xsl:for-each select="//book"> <xsl:sort select="author"/> <li> <xsl:value-of select="title"/> by <xsl:value-of select="author"/> </li> </xsl:for-each> </ul> This example creates a list of titles and authors, sorted by author
  • 12. xsl:text <xsl:text>...</xsl:text> XSL isn’t very careful with whitespace in the document This doesn’t matter much for HTML, which collapses all whitespace anyway (though the HTML source may look ugly) <xsl:text> gives you much better control over whitespace; it acts like the <pre> element in HTML
  • 13. Quick questions - What are the input files in XSLT? - Mentions 2 commands in XSL - ¿What does the command choose?
  • 14. Applying templates to children <book> <title>XML</title> <author>Gregory Brill</author> </book> <xsl:template match="/"> <html> <head></head> <body> <b><xsl:value-of select="/book/title"/></b> <xsl:apply-templates select="/book/author"/> <xsl:apply-templates select="/book/title"/> </body> </html> </xsl:template> <xsl:template match="/book/author"> by <i><xsl:value-of select="."/></i> </xsl:template> With this line: XML by Gregory Brill Without this line: XML
  • 15. Calling named templates You can name a template, then call it, similar to the way you would call a method in Java The named template: <xsl:template name="myTemplateName"> ...body of template... </xsl:template> A call to the template: <xsl:call-template name="myTemplateName"/> Or: <xsl:call-template name="myTemplateName"> ...parameters... </xsl:call-template>
  • 16. Templates with parameters Parameters, if present, are included in the content of xsl:template, but are the only content of xsl:call-template Example call: <xsl:call-template name="doOneType"> <xsl:with-param name="header" select="'Lectures'"/> <xsl:with-param name="nodes" select="//lecture"/> </xsl:call-template> Example template: <xsl:template name="doOneType"> <xsl:param name="header"/> <xsl:param name="nodes"/> ...template body...refer to parameters as "$header" and "$nodes" </xsl:template> Parameters are matched up by name, not by position Single quotes inside double quotes make this a string This parameter is a typical XPath expression
  • 17. Thoughts on XSL XSL is a programming language--and not a particularly simple one Expect to spend considerable time debugging your XSL These slides have been an introduction to XSL and XSLT--there’s a lot more of it we haven’t covered As with any programming, it’s a good idea to start simple and build it up incrementally: “Write a little, test a little” This is especially a good idea for XSLT, because you don’t get a lot of feedback about what went wrong I use jEdit with the XML plugin I find it to be a big help, especially with XML syntax My approach is: write (or change) a line or two, check for syntax errors, then jump to IE and reload the XML file