1
XML –P5
Mrs.C.Santhiya
Assistant
Professor/IT
TCE,Madurai
What is XSL?
• XSL is a language that allows one to describe a browser
how to process an XML file.
• XSL can convert an XML file into another XML with
different format.
• XSL can convert an XML file into a non-XML file.
2
Purpose
• The most common type of XSL processing is to convert XML file
into HTML file which can be displayed by browsers. We will
focus on this use of XSL.
• XSL is the bridge between XML and HTML.
• We can use XSL to have different HTML formats for
the same data represented in XML.
• Separating data (contents) from style tags (display
commands).
3
ex
4
Declaration
• <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
• <xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
5
• <xsl:apply-templates>
• <xsl:apply-templates match=“expression”>
• <xsl:template>
• <xsl:value of select=“expression”>
• <xsl:for each select=“expression”>
• <xsl:sort select=“expression”>
• <xsl:output>
• <xsl:copy>
6
Match attribute
• <xsl:template match="/">
• <xsl:template match=“/”>
….
</xsl:template>
7
xsl:for-each
• <xsl:value-of select="catalog/cd/title"/>
• <xsl:value-of select=“name”/>
• <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>
8
xsl:sort
• <xsl:for-each select="catalog/cd">
<xsl:sort select="artist"/>
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
9
xsl:if
• <xsl:for-each select="catalog/cd">
<xsl:if test="price &gt; 10">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
<td><xsl:value-of select="price"/></td>
</tr>
</xsl:if>
</xsl:for-each>
10
xsl:choose
• <xsl:choose>
  <xsl:when test="expression">
    ... some output ...
  </xsl:when>
  <xsl:otherwise>
    ... some output ....
  </xsl:otherwise>
</xsl:choose>
11
EX
• <xsl:for-each select="catalog/cd">
    <tr>
      <td><xsl:value-of select="title"/></td>
      <xsl:choose>
        <xsl:when test="price &gt; 10">
          <td bgcolor="#ff00ff">
          <xsl:value-of select="artist"/></td>
        </xsl:when>
        <xsl:otherwise>
          <td><xsl:value-of select="artist"/></td>
        </xsl:otherwise>
      </xsl:choose>
    </tr>
    </xsl:for-each>
12
• <xsl:template match="title">
•   Title: <span style="color:#ff0000">
•   <xsl:value-of select="."/></span>
•   <br />
• </xsl:template>
13
XSl:copy
• Copying:
• xsl:copy, copies the current node
• xsl:apply-templates, processes the children of the current node
• ex. <xsl:stylesheetxmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
• <xsl:template match="title">
• <xsl:copy>
• <xsl:apply-templates/>
• </xsl:copy>
• </xsl:template>
• </xsl:stylesheet>
• xsl:copy-of element, can copy the entire subtree of each node that the
template selects
– ex. <xsl:template match="title“>
– <xsl:copy-of select="*"/>
– </xsl:template>
14
Example of Turning XML into HTML
• <?xml version="1.0"?>
• <?xml-stylesheet type="text/xsl" href="FitnessCenter.xsl"?>
• <FitnessCenter>
•         <Member level="platinum">
•                 <Name>Jeff</Name>
•                 <Phone type="home">555-1234</Phone>
•                 <Phone type="work">555-4321</Phone>
•                 <FavoriteColor>lightgrey</FavoriteColor>
•         </Member>
• </FitnessCenter>
15
HTML Document in an XSL Template
<?xml version="1.0"?>
<xsl:output method="html"/>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE>Welcome</TITLE>
</HEAD>
<BODY>
Welcome!
</BODY>
</HTML>
</xsl:template>
16
Extracting
• <?xml version="1.0"?>
• <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
• version="1.0">
•
• <xsl:output method="html"/>
• <xsl:template match="/">
• <HTML>
• <HEAD>
• <TITLE>Welcome</TITLE>
• </HEAD>
• <BODY bgcolor="{/FitnessCenter/Member/FavoriteColor}">
• Welcome <xsl:value-of select="/FitnessCenter/Member/Name"/>!
• </BODY>
• </HTML>
• </xsl:template>
• </xsl:stylesheet>
17
validity
• <Body bgcolor="<xsl:value-of
select='/FitnessCenter/Member/FavoriteColor'/>">
• <Body
bgcolor="{/FitnessCenter/Member/Fav
oriteColor}">
18
• <?xml version="1.0"?>
• <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
• version="1.0">
• <xsl:output method="html"/>
• <xsl:template match="/">
• <HTML>
• <HEAD>
• <TITLE>Welcome</TITLE>
• </HEAD>
• <BODY bgcolor="{/FitnessCenter/Member/FavoriteColor}">
• Welcome <xsl:value-of select="/FitnessCenter/Member/Name"/>!
• <BR/>
• Your home phone number is:
• <xsl:value-of select="/FitnessCenter/Member/Phone[@type='home']"/>
• </BODY>
• </HTML>
• </xsl:template>
• </xsl:stylesheet> 19

Xml p5 Lecture Notes