Rajiv Poplai
Agenda
 Introduction to XSLT
 Examples
 Implementations
 XSLT processor
Warming up…
 What does XML stand for ?
 eXtensible Markup Language
 Extensible means that there is no fixed
tag set (like in HTML)
Overview
 Stands for “eXtensible Stylesheet
Language Transformations”
 Language for transforming an input xml
document into output document.
 Navigation of input XML document is
through XPath
 Output document may be XML or any other
text format.
XSLT processor
Language
 Syntax defined by W3C
 XSLT stylesheet file is an XML file
 XSLT is based on the programming
paradigm of pattern matching.
 XSLT is based on concepts of functional
programming as a stylesheet is made up of
templates, which are essentially pure
functions.
<xsl:stylesheet version=“1.0”
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”>
Simple Example
 Sample xml File
 Sample Output required in csv format
<?xml version=“1.0”?>
<ContactInfo>
<Person>
<Name>Rajiv Poplai</Name>
<Mobile>9810766277</Mobile>
</Person>
</ContactInfo>
Rajiv Poplai, 9810766277
 XSLT file to accomplish the
transformation
<?xml version=“1.0”?>
<xsl:stylesheet version=“1.0”
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”>
<xsl:output method=“text”>
<xsl:template match=“Person”>
<xsl:value-of select=“Name”/>
<xsl:text>,</xsl:text>
<xsl:value-of select=“Mobile”/>
</xsl:template>
</xsl:stylesheet>
<xsl:template> Element
 An xsl:template element essentially tells the
XSLT processor, “as you go through the
source tree, when you find a node of that tree
whose name matches the value of my match
attribute, add my contents to the result tree.”
<?xml version="1.0"?>
<ListOfStudents>
<?xml version="1.0"?>
<Students>
<Person id="12998800">
<FirstName>Peter</FirstName>
<LastName>Kováč</LastName>
<BirthDate>1980-10-25</BirthDate>
<Study>
<Program id="1.MAT"></Program>
<Faculty id="1070">FMFI UK</Faculty>
</Study>
<Study>
<Program id="1.AE"></Program>
<Faculty id="1050">FiF UK</Faculty>
</Study>
</Person>
<Person id="88332212">
<FirstName>Karol</FirstName>
<LastName>Nový</LastName>
<BirthDate>1982-11-16</BirthDate>
<Study>
<Program id="1.INF"></Program>
<Faculty id="1070">FMFI UK</Faculty>
</Study>
</Person>
</Students>
<?xml version="1.0"?>
<xsl:stylesheet version=“1.0"
xmlns:xsl="http://www.w3.org/1999
/XSL/Transform">
<xsl:template match="/">
<ListOfStudents>
<xsl:apply-templates/>
</ListOfStudents>
</xsl:template>
<xsl:template match="Person">
<Student>
<xsl:value-of
select="FirstName"/>,
<xsl:value-of
select="LastName"/>,
<xsl:value-of
select="Study[1]/Faculty"/>
(<xsl:value-of
select="Study[1]/Faculty/@id"/>)
</Student>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0"?>
<ListOfStudents>
<Student>Peter, Kováč, FMFI UK (1070)</Student>
<Student>Karol, Nový, FMFI UK (1070)</Student>
</ListOfStudents>
Test your knowledge
 What would an XSLT processor do with a stylesheet that contained
no template rules?
 For example what would be the output if the stylesheet below:
 is applied to the XML file below
Answer
 XSLT has several built-in default templates that tell
the XSLT processor to output the text content of the
elements, leaving out the attributes and markup.
Other constructs
Tasks Construct (Instructions)
Creating new nodes <xsl:document>, <xsl:element>,
<xsl:attribute>, <xsl:text>, <xsl:value-of>
Iteration (process all nodes of a given
node set)
<xsl:for-each>
Conditional <xsl:if>, <xsl:choose>
Numbering <xsl:number>
Combining Stylesheets <xsl:include>, <xsl:import>
Sorting (sort a nodeset based on some
criteria)
<xsl:sort>
Variables <xsl:variable>, <xsl:param>
Invoking templates <xsl:apply-templates> (find a template
for each node in the input sequence)
<xsl:call-template>
Browser Support
Browser Minimum Version
Mozilla Firefox 3
Internet Explorer 6
Google Chrome 1
Opera 9
Apple Safari 3
Anatomy of XSLT processor

XSLT

  • 1.
  • 2.
    Agenda  Introduction toXSLT  Examples  Implementations  XSLT processor
  • 3.
    Warming up…  Whatdoes XML stand for ?  eXtensible Markup Language  Extensible means that there is no fixed tag set (like in HTML)
  • 4.
    Overview  Stands for“eXtensible Stylesheet Language Transformations”  Language for transforming an input xml document into output document.  Navigation of input XML document is through XPath  Output document may be XML or any other text format.
  • 5.
  • 6.
    Language  Syntax definedby W3C  XSLT stylesheet file is an XML file  XSLT is based on the programming paradigm of pattern matching.  XSLT is based on concepts of functional programming as a stylesheet is made up of templates, which are essentially pure functions. <xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”>
  • 7.
    Simple Example  Samplexml File  Sample Output required in csv format <?xml version=“1.0”?> <ContactInfo> <Person> <Name>Rajiv Poplai</Name> <Mobile>9810766277</Mobile> </Person> </ContactInfo> Rajiv Poplai, 9810766277
  • 8.
     XSLT fileto accomplish the transformation <?xml version=“1.0”?> <xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:output method=“text”> <xsl:template match=“Person”> <xsl:value-of select=“Name”/> <xsl:text>,</xsl:text> <xsl:value-of select=“Mobile”/> </xsl:template> </xsl:stylesheet>
  • 9.
    <xsl:template> Element  Anxsl:template element essentially tells the XSLT processor, “as you go through the source tree, when you find a node of that tree whose name matches the value of my match attribute, add my contents to the result tree.”
  • 10.
    <?xml version="1.0"?> <ListOfStudents> <?xml version="1.0"?> <Students> <Personid="12998800"> <FirstName>Peter</FirstName> <LastName>Kováč</LastName> <BirthDate>1980-10-25</BirthDate> <Study> <Program id="1.MAT"></Program> <Faculty id="1070">FMFI UK</Faculty> </Study> <Study> <Program id="1.AE"></Program> <Faculty id="1050">FiF UK</Faculty> </Study> </Person> <Person id="88332212"> <FirstName>Karol</FirstName> <LastName>Nový</LastName> <BirthDate>1982-11-16</BirthDate> <Study> <Program id="1.INF"></Program> <Faculty id="1070">FMFI UK</Faculty> </Study> </Person> </Students> <?xml version="1.0"?> <xsl:stylesheet version=“1.0" xmlns:xsl="http://www.w3.org/1999 /XSL/Transform"> <xsl:template match="/"> <ListOfStudents> <xsl:apply-templates/> </ListOfStudents> </xsl:template> <xsl:template match="Person"> <Student> <xsl:value-of select="FirstName"/>, <xsl:value-of select="LastName"/>, <xsl:value-of select="Study[1]/Faculty"/> (<xsl:value-of select="Study[1]/Faculty/@id"/>) </Student> </xsl:template> </xsl:stylesheet> <?xml version="1.0"?> <ListOfStudents> <Student>Peter, Kováč, FMFI UK (1070)</Student> <Student>Karol, Nový, FMFI UK (1070)</Student> </ListOfStudents>
  • 11.
    Test your knowledge What would an XSLT processor do with a stylesheet that contained no template rules?  For example what would be the output if the stylesheet below:  is applied to the XML file below
  • 12.
    Answer  XSLT hasseveral built-in default templates that tell the XSLT processor to output the text content of the elements, leaving out the attributes and markup.
  • 13.
    Other constructs Tasks Construct(Instructions) Creating new nodes <xsl:document>, <xsl:element>, <xsl:attribute>, <xsl:text>, <xsl:value-of> Iteration (process all nodes of a given node set) <xsl:for-each> Conditional <xsl:if>, <xsl:choose> Numbering <xsl:number> Combining Stylesheets <xsl:include>, <xsl:import> Sorting (sort a nodeset based on some criteria) <xsl:sort> Variables <xsl:variable>, <xsl:param> Invoking templates <xsl:apply-templates> (find a template for each node in the input sequence) <xsl:call-template>
  • 14.
    Browser Support Browser MinimumVersion Mozilla Firefox 3 Internet Explorer 6 Google Chrome 1 Opera 9 Apple Safari 3
  • 15.
    Anatomy of XSLTprocessor