Formatting XML with XSL

What is XSL?
 
The EXtensible Stylesheet Language (XSL) includes both a formatting language and a transformation language. Each of these, naturally enough, is an XML application. The transformation language provides elements that define rules for how one XML document is transformed into another XML document. The transformed XML document may use the markup and DTD of the original document or it may use a completely different set of elements. In particular, it may use the elements defined by the second part of XSL, the formatting objects. XSL like CSS can be used to format XML document and define how the XML document should look.
XSL is a combination of two languages used as a single language, it’s not one language. The first language is a transformation language, the second a formatting language. Its ability to move data from one XML representation to another makes it an important component of XML-based electronic commerce, electronic data interchange (B2B exchange etc), metadata exchange, and any application that needs to convert between different XML representations of the same data. These uses are also united by their lack of concern with interpretation of data on a display for humans to read. They are purely about moving data from one computer system or program to another.
The transformation and formatting halves of XSL can function independently of each other. For instance, the transformation language can transform an XML document into a well-formed HTML file, and completely ignore XSL formatting objects.
 
A sample XSL file :
 
Source code of “xsl_tutorial.xsl “
 
<?xml version=”1.0″ encoding=”iso-8859-1″?>

    <!DOCTYPE xsl:stylesheet [
    <!ENTITY nbsp ” “>
    <!ENTITY copy “©”>
    <!ENTITY reg “®”>
    <!ENTITY trade “™”>
    <!ENTITY mdash “—”>
    <!ENTITY ldquo ““”>
    <!ENTITY rdquo “””>
    <!ENTITY pound “£”>
    <!ENTITY yen “¥”>
    <!ENTITY euro “€”>
]>

<xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>

<xsl:template match=”/”>

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″/>
<title></title>
</head>
<body style=”background-color: #00CCFF; font:’Courier New’, Courier, monospace; “>
    <xsl:for-each select=”csitquestion/tutorial”>
        <div style=” margin-left:-4px; border:#00CCCC thin dotted; width:400; m
argin-top:4″>
         <span style=” font-weight:bolder; color:#FFFFFF;padding:8px”>
          <span style=”font-style:italic”> Cateogory of tutorial :</span>
         <xsl:value-of select=”category” />
    <br />

Sub-Category: <span style=”padding-left:15; color:#990000″><xsl:value-of select=”sub_category” />
<br />

<b>Name of Tutorial: 
<span style=”color:#006699;”>
<xsl:value-of select=”title” /></span>
<br />
</b>
</span>
<br />
Author :<span style=”color:#006699;”>
<xsl:value-of select=”author” />
</span>
<br />
Date of Lanunch : <span style=”color:#006699;”>
<xsl:value-of select=”launch_date” />
</span>
</span>
</div>
<xsl:for-each select=”CSITQuestion”>
<xsl:value-of select=”info” /> </xsl:for-each>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

 
 
 
Source code of “xml_without_xsl.xml”
 
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<!DOCTYPE tutorial [
<!ELEMENT csitquestion (tutorial?,info)>
<!ELEMENT tutorial (category,sub_category?,title,author,url,launch_date)>
        <!ATTLIST tutorial category (cc|ll|ul) #REQUIRED>
    <!ENTITY developer “[email protected]”>
    <!ENTITY developer1 “[email protected]”>
    <!ENTITY developer2 “[email protected]”>
    <!ENTITY nbsp ” “>
    <!ENTITY copyright “csitquestion.com, All right Reserved, 2007”>
]>
<ebiz>
<tutorial category=”cc”>
    <category>Computer Courses</category>
    <sub_category> Scripting Language</sub_category>
    <title> XML</title>
    <author>&developer;</author>
    <url>http://csitquestion.com/html/xml/</url>
    <launch_date> 13/Aug2016 </launch_date>

</tutorial>
<tutorial category=”ll”>
    <category>Language Learning</category>
    <title>French Tutorial </title>
    <author>&developer;</author>
    <url>http://csitquestion.com/html/xml/</url>
    <launch_date> 13/Aug2016  </launch_date>

</tutorial>
<tutorial category=”cc”>
    <category>Computer Courses</category>
    <sub_category> General</sub_category>
    <title> Computer Hardware</title>
    <author>&developer2;</author>
    <url>http://csitquestion.com/html/xml/</url>
    <launch_date> 13/Aug2016  </launch_date>
</tutorial>

</CSITQuestion>

 
 
Given below is the source of the XML file, which has been formatted using XSL. The second line,
<?xml-stylesheet type=”text/xsl” href=”xsl_tutorial.xsl” ?>
links the XML file to the XSL file.
 
Source code of xml_with_xsl.xml
 
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<?xml-stylesheet type=”text/xsl” href=”xsl_tutorial.xsl” ?>
<!DOCTYPE tutorial [
<!ELEMENTCSITQuestion (tutorial?,info)>
<!ELEMENT tutorial (category,sub_category?,title,author,url,launch_date)>
     <!ATTLIST tutorial category (cc|ll|ul) #REQUIRED>

    <!ENTITY developer “[email protected]”>
    <!ENTITY developer1 “[email protected]”>
    <!ENTITY developer2 “[email protected]”>
    <!ENTITY nbsp ” “>
        <!ENTITY copyright “CSITQuestion.com, All right Reserved, 2016”>
]>

<ebiz>

<tutorial category=”cc”>
    <category>Computer Courses</category>
    <sub_category> Scripting Language</sub_category>
    <title> XML</title>
    <author>&developer;</author>
    <url>http://csitquestion.com/html/xml/</url>
    <launch_date> 22/oct2016 </launch_date>
</tutorial>

<tutorial category=”ll”>
    <category>Language Learning</category>
    <title>French Tutorial </title>
    <author>&developer;</author>
    <url>http://csitquestion.com/html/xml/</url>
    <launch_date>13/Aug2016 </launch_date>
</tutorial>

<tutorial category=”cc”>
    <category>Computer Courses</category>
    <sub_category> General</sub_category>
    <title> Computer Hardware</title>
    <author>&developer2;</author>
    <url>http://csitquestion.com/html/xml/</url>
    <launch_date> 13/Aug2016  </launch_date>
</tutorial>

</CSITQuestion>