Displaying XML Using XSLT

XSLT stands for Extensible Stylesheet Language Transformation.

  • XSLT is used to transform XML document from one form to another form.
  • XSLT uses Xpath to perform matching of nodes to perform these transformation .
  • The result of applying XSLT to XML document could be an another XML document, HTML, text or any another document from technology prespective.
  • The XSL code is written within the XML document with the extension of (.xsl).
  • In other words, an XSLT document is a different kind of XML document.

XML Namespace: XML Namespaces are the unique names .

  • XML Namespace is a mechanism by which element or attribute is assigned to a group.
  • XML Namespace is used to avoid the name conflicts in the XML document.
  • XML Namespace is recommended by W3C.

XML Namespace Declaration:
It is declared using reserved attribute such as the attribute is xmlns or it can begin with xmlns:

  • Syntax:
     <element xmlns:name = "URL">


    • Namespace starts with the xmlns.
    • The word name is the namespace prefix.
    • the URL is the namespace identifier.

  • Example:
    Consider the following xml document named Table.xml :-

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/css" href="rule.css"?>

    In the above code, there would be a name conflict, both of them contains the same table element but the contents of the table element is different .To handle this situation, the concept of XML Namespace is used.

  • Example:
    Consider the same XML document to resolve name conflict:

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/css" href="rule.css"?>
      <m:table xmlns:m=""http://www.google.co.in">
      <n:table xmlns:m=""http://www.yahoo.co.in">


  • Xpath is an important component of XSLT standard.
  • Xpath is used to traverse the element and attributes of an XML document.
  • Xpath uses different types of expression to retrieve relevant information from the XML document.
  • Xpath contains a library of standard functions.

    • bookstore/book[1] => Fetches details of first child of bookstore element.
    • bookstore/book[last()] => Fetches details of last child of bookstore element.


  • An XSL stylesheet contains one or more set of rules that are called templates.
  • A template contains rules that are applied when the specific element is matched.
  • An XSLT document has the following things:
    • The root element of the stylesheet.
    • A file of extension .xsl .
    • The syntax of XSLT i.e what is allowed and what is not allowed.
    • The standard namespace whose URL is http://www.w3.org/1999/XSL/Transform.

In this example, creating the XML file that contains the information about five students and displaying the XML file using XSLT.

  • XML file:
    Creating Students.xml as:

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl "href="Rule.xsl" ?>
       <name> Divyank Singh Sikarwar </name>
       <branch> CSE</branch>
       <city> Agra </city>
       <name> Aniket Chauhan </name>
       <branch> CSE</branch>
       <age> 20</age>
       <city> Shahjahanpur </city>
       <name> Simran Agarwal</name>
       <branch> CSE</branch>
       <age> 23</age>
       <city> Buland Shar</city>
       <name> Abhay Chauhan</name>
       <branch> CSE</branch>
       <age> 17</age>
       <city> Shahjahanpur</city>
       <name> Himanshu Bhatia</name>
       <branch> IT</branch>
       <age> 25</age>
       <city> Indore</city>

    In the above example, Students.xml is created and linking it with Rule.xsl which contains the corresponding XSL style sheet rules.

  • XSLT Code:

    Creating Rule.xsl as:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" 
    <xsl:template match="/">
      <h1 align="center">Students' Basic Details</h1>
       <table border="3" align="center" >
        <xsl:for-each select="student/s">
        <td><xsl:value-of select="name"/></td>
        <td><xsl:value-of select="branch"/></td>
        <td><xsl:value-of select="age"/></td>
        <td><xsl:value-of select="city"/></td>
  • Output :

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Recommended Posts:

4 Average Difficulty : 4/5.0
Based on 1 vote(s)

User Actions