Hasini Thamarasa
Software Engineer
<?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
Kinds Of Nodes
Select student in the school
Select all element student
Select all element student children of class element
//CLASS/STUDENT
Select element student in grade 10
//STUDENT[@GRADE="10"]
Select all descendants of student
//STUDENT/descendant::*
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 PROCESSOR
SOURCE TREE
RESULT TREE
STYLESHEET TREE
<?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>
<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>
<xsl:template match="a/b">
<xsl:template match="@*"> <xsl:templte match = "a[preceding::b[@type='X']]">
<xsl:templte match = "preceding::b[@type='X']">
Similar to conditional staments.
<xsl:if test="tps:c[@type="Footnote Text"]">
<xsl:text>It is simple</xsl:text>
</xsl:if>
Inserts the value of the selected node as text.
<xsl:value-of select="tps:c[@type="Footnote Text"]"/>
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>
By Hasini Thamarasa
XSLT and XPath for beginners.