XSL

eXtensible Stylesheet Language 

What is Inside ??

  • What is XSL

  • XPath Introduction

  • What is XSLT

  • How XSLT works

  • Basic elements use in XSLT

What is XSL ??

  • XML-based Stylesheet Language

  • XSL describes how the XML document should be displayed

  • mainly consists of 

    • XSLT - designed for transforming xml documents
    • XPath - designed for navigating xml documents
    • XSL-FO - designed for formatting xml documents
    • XQuery - designed for querying xml documents

xPath

XML Path Language

XPath

XML Path Language

address the nodes of XML tree

used to navigate per nodes in an XML document

based on a tree representation of the XML document

XPath 3.0 version is available,but 1.0 is widely used 

 embedded in XSLT & XQuery

<?xml version="1.0" encoding="UTF-8"?>
<!--This is a School -->
<SCHOOL xmlns:tps="http://www.typefi.com/CXML">
    <STUDENT GRADE="10">
        <LASTNAME>Amal</LASTNAME>
        <NIC_NO>893212334V</NIC_NO>
        <ADDRESS TYPE="PERMANENET">No.13,Colombo</ADDRESS>
    </STUDENT>
    <STUDENT GRADE="11">
        <LASTNAME>Kamal</LASTNAME>
        <NIC_NO>883212334V</NIC_NO>
        <ADDRESS TYPE="MAILING">No.12,Colombo</ADDRESS>
    </STUDENT>
    <CLASS>
        <tps:student MARKS="GOOD"></student>
    </CLASS>
</SCHOOL>

XML File - Source Tree

XML document is tree of nodes

XPath Nodes

Kinds Of Nodes

  • element -
    • <STUDENT>
  • attribute -
    • GRADE
  • text -
    • Amal 
  • namespace 
    • xmlns:tps="http://www.typefi.com/CXML"
  • processing-instruction
    • <?xml version="1.0" encoding="UTF-8"?>
  • comment
    • <!--This is a School -->

XPath Axes

  • This represents a relationship to the context node
  • Axes is used to locate nodes relative to that node on the tree
  • Thirteen axes available in XPath 
    • Examples:child,ancestor,parent,self,sibling

Predicates

  • select only nodes for which some condition is true
  • written in square brackets
  • Example:
    • /STUDENT[@GRADE=10]​[1]

Node-set

  • This is a specific node or expressions
  • Example:
    • //tps:student

Xpath Syntax

  • There are so many syntax in XPath
  • Syantax of selecting location path is important(location Path Expression)
  • Location Path consist:

 

 

  1. an Axes
  2. a node set
  3. predicate (optional)
  • axisname::nodetest[predicate]
  • Location Path Expression is evaluated with respect to a context node(current node)
  • Axis specifies the direction to navigate
  • Example:
    • attribute::GRADE[1]

Relative Path

  • If location path starts with the node that we've selected then it is an relative path
  • Example:
    • CLASS/tps:student

Absolute Path

  • If location path starts with root node it is absolute path
  • ​Example:
    • /ancestor::STUDENT/

XPath examples..

  1. Select student in the school

    1. SCHOOL/STUDENT
  2. Select all element student

    1. //STUDENT
  3. Select all element student children of class element

    1. //CLASS/STUDENT

  4. Select element student in grade 10

    1. //STUDENT[@GRADE="10"]

  5. Select all descendants of student

    1. //STUDENT/descendant::* 

Result Tree of Examples

Text

    <STUDENT GRADE="10">
        <LASTNAME>Amal</LASTNAME>
        <NIC_NO>893212334V</NIC_NO>
        <ADDRESS TYPE="PERMANENET">No.13,Colombo</ADDRESS>
    </STUDENT>
    <STUDENT GRADE="11">
        <LASTNAME>Kamal</LASTNAME>
        <NIC_NO>883212334V</NIC_NO>
        <ADDRESS TYPE="MAILING">No.12,Colombo</ADDRESS>
    </STUDENT>

SCHOOL/STUDENT

    <STUDENT GRADE="10">
        <LASTNAME>Amal</LASTNAME>
        <NIC_NO>893212334V</NIC_NO>
        <ADDRESS TYPE="PERMANENET">No.13,Colombo</ADDRESS>
    </STUDENT>
    <STUDENT GRADE="11">
        <LASTNAME>Kamal</LASTNAME>
        <NIC_NO>883212334V</NIC_NO>
        <ADDRESS TYPE="MAILING">No.12,Colombo</ADDRESS>
    </STUDENT>
    
    <STUDENT MARKS="GOOD"></STUDENT>
   

//STUDENT

<STUDENT MARKS="GOOD"></STUDENT>

//CLASS/STUDENT

    <STUDENT GRADE="10">
        <LASTNAME>Amal</LASTNAME>
        <NIC_NO>893212334V</NIC_NO>
        <ADDRESS TYPE="PERMANENET">No.13,Colombo</ADDRESS>
    </STUDENT>

//STUDENT[@GRADE="10"]

 

        <LASTNAME>Amal</LASTNAME>
        <NIC_NO>893212334V</NIC_NO>
        <ADDRESS TYPE="PERMANENET">No.13,Colombo</ADDRESS>
        ....

//STUDENT/descendant::*​ 

XSLT

eXtensible Stylesheet Language 

Transformation

What is XSLT?

  • Language for transforming XML document into other formats

  • Transformation could be

    • XML to DOC(PDF,Text,Epub) Transformation

    • XML to HTML(XHTML) Transformation

    • XML to XML Transformation

  • W3C has published XSLT 1.0,XSLT 2.0, and XSLT 3.0

How XSLT works?

XSLT PROCESSOR

SOURCE TREE

RESULT TREE

STYLESHEET TREE

How to get started ?

  • Select a suitable editor - eg:oXygen ​
  • get new XSLT Stylesheet
  • add <xsl:stylesheet> tag
  • save file with .xsl extension

 

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    
</xsl:stylesheet>

Basic Elements used in XSLT

 

  1. <xsl:template>
    A template contains rules to apply when a specified node is matched

    ​​​match is the main and mandatory attribute

     match is a subset of the set of all possible XPath            expressions

     reverse axes are not allowed

     but can be specified within the predicates

 

 

 

XML file:

<tps:section>
    <tps:c type="Footnote Reference">
       <tps:footnote>
          <tps:p type="Footnote Text">
          
          </tps:p>
       </tps:footnote>
    </tps:c>
</tps:section>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     exclude-result-prefixes="xs"
     version="2.0">

    <xsl:template 
            match="tps:footnote/tps:p[@type='Footnote Text']">
       <xsl:text>Typefi</xsl:text>
    </xsl:template>
    
</xsl:stylesheet>

XSLT file:

More Examples

<xsl:template match="a/b">

 

<xsl:template match="@*">

<xsl:templte match =    "a[preceding::b[@type='X']]">

 

<xsl:templte match =    "preceding::b[@type='X']">

2. <xsl:if>

Similar to conditional staments.

<xsl:if test="tps:c[@type="Footnote Text"]">
    <xsl:text>It is simple</xsl:text>
</xsl:if>

3. <xsl:value-of>

Inserts the value of the selected node as text.

<xsl:value-of select="tps:c[@type="Footnote Text"]"/>

4. <xsl:choose>

Required child elements are <xsl:when> and <xsl:otherwise>

Use to test multiple conditions

<?xml version="1.0"?>
<school>
    <class>
        <student/>
        <student/>
        <total>25<total>
    </class>
    <class>
        <student/>
        <student/>
        <total>30<total>
    </class>
    <class>
        <student/>
        <student/>
        <total>40<total>
    </class>
</school>
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:template>
        <xsl:choose>
            <xsl:when test="total < 40">
                not space availabe
            </xsl:when>
            <xsl:otherwise>
                space availabe
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

XSLT

By Hasini Thamarasa

XSLT

XSLT and XPath for beginners.

  • 811