Q1.For the below xml, create an xsl which displays only the text “24X7 news” when applied to the below xml.

News.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="News.xsl" ?>
<news>
   <news_item>
        <title>Title</title>
        <content>
           <par>
               This is My News
               <footnote>toi</footnote>
          </par>
        </content>
       <date>19/01/11</date>
        <author>Anki</author>
        <news_agent>TOI</news_agent>
    </news_item>
</news>

Solution

The xsl for the above xml

News.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="news_item">
     <html>
       <h1>24x7 News</h1>
    </html>
</xsl:template>
</xsl:stylesheet>


Open the XML file in a browser. The o/p is shown below

News1

Q2. Modify the xsl, to print the title, date and author of the news item in a tabular format.

Solution

News2.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="news_item">
   <html>
        <br/>
        <table border="1" align="center" cellpadding="10">
         <tr>
           <th>Title</th>
           <th>Date</th>
           <th>Author</th>
         </tr>
         <tr>
             <td><xsl:value-of select="title"/></td>
              <td><xsl:value-of select="date"/></td>
              <td><xsl:value-of select="author"/></td>
         </tr>
      </table>
     </html>
</xsl:template>
</xsl:stylesheet>


To link an XML file with a XSL add this line

Here in News.xml file we added this line

<?xml-stylesheet type=”text/xsl” href=”News2.xsl” ?>

Open this file in a Browser

o/p

News2

Advertisements