The Wayback Machine - https://web.archive.org/web/20241121002820/https://www.geeksforgeeks.org/displaying-xml-using-xslt/
Open In App

Displaying XML Using XSLT

Last Updated : 13 May, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 perspective.
  • 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">

    where

    • 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"?>
     <tables>
      <table>
       <tr>
        <td>Apple</td>
        <td>Banana</td>
       </tr>
      </table>
      <table>
       <height>100</height>
       <width>150</width>
      </table>
     </tables>

    
    

    In the above code, there would be a name conflict, both of them contain the same table element but the contents of the table element are 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"?>
     <tables>
      <m:table xmlns:m=""http://www.google.co.in">
       <m:tr>
        <m:td>Apple</m:td>
        <m:td>Banana</m:td>
       </m:tr>
      </m:table>
      <n:table xmlns:m=""http://www.yahoo.co.in">
       <n:height>100</n:height>
       <n:width>150</n:width>
      </n:table>
     </tables>

    
    

Xpath:

  • 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.
    Example:

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

Templates:

  • 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.

Example:
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" ?>
     <student>
      <s>
       <name> Divyank Singh Sikarwar </name>
       <branch> CSE</branch>
       <age>18</age>
       <city> Agra </city>
      </s>
      <s>
       <name> Aniket Chauhan </name>
       <branch> CSE</branch>
       <age> 20</age>
       <city> Shahjahanpur </city>
      </s>
      <s> 
       <name> Simran Agarwal</name>
       <branch> CSE</branch>
       <age> 23</age>
       <city> Buland Shar</city>
      </s>
      <s> 
       <name> Abhay Chauhan</name>
       <branch> CSE</branch>
       <age> 17</age>
       <city> Shahjahanpur</city>
      </s>
      <s> 
       <name> Himanshu Bhatia</name>
       <branch> IT</branch>
       <age> 25</age>
       <city> Indore</city>
      </s>
     </student>

    
    

    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="/">
     <html>
     <body>
      <h1 align="center">Students' Basic Details</h1>
       <table border="3" align="center" >
       <tr>
        <th>Name</th>
        <th>Branch</th>
        <th>Age</th>
        <th>City</th>
       </tr>
        <xsl:for-each select="student/s">
       <tr>
        <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>
       </tr>
        </xsl:for-each>
        </table>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>

    
    

  • Output :


Previous Article
Next Article

Similar Reads

Displaying XML Using CSS
XML stands for Extensible Markup Language. It is a dynamic markup language. It is used to transform data from one form to another form. An XML file can be displayed using two ways. These are as follows :- Cascading Style Sheet Extensible Stylesheet Language Transformation Displaying XML file using CSS : CSS can be used to display the contents of th
4 min read
Difference Between text/xml and application/xml for Webservice Response
application/xmlThis is the recommended media type for XML data according to the IETF specifications [RFC 7303]. It signifies that the XML content might not be human-readable and could potentially contain processing instructions or other non-human-consumable elements. Syntax:Content-Type: application/xmlThis example demonstrates a web service respon
2 min read
Displaying inline and multiline blocks of code using Bootstrap
Bootstrap provides a number of classes for displaying inline and multiline blocks of code. Displaying Inline Code: Inline code should be wrapped in <code> tags. The resulting text will be displayed in a fixed-width font and given a red font color. Note: The < and > tags should be replaced with &lt; and &gt; respectively. Below i
3 min read
Convert JSON file into CSV file and displaying the data using Node.js
There are so many ways to store data for better understanding for individual purposes, in few cases, JSON files will be more suitable in few cases, CSV files, and there are also lots of other types as well like XML, etc. In this article, we will convert the JSON file data into CSV file data and also display that through Node.js. JSON stands for Jav
3 min read
Which tags are used to displaying data in tabular form in HTML ?
In this article, we will discuss the tags used to display the data in tabular form. Tags: Tags are the starting and ending parts of an HTML element. Every tag starts with the "<" symbol and ends with the">" symbol. Whatever inside these symbols are called tags. Opening and closing tags are used to store any information inside the tag. Opening
3 min read
How To Redirect To Home Page instead Of Displaying 404 Page in Gatsby?
In Gatsby when a user navigates to a non-existent page, they typically see a 404 error page. However, there may be situations where you want to redirect users to the home page (or another specified page) instead of showing the 404 error page. This can be useful for improving the user experience or handling specific routing needs. In this article, w
3 min read
Difference between XQuery and XSLT
XQuery: XQuery is the language for querying XML data. It is used to access information stored in XML format. It is used for fetching the information from a database that is to be used in Web services like SoapUI, receiving data from a database that is to be used with application integration, generating summary reports, to retrieve relevant informat
2 min read
XSLT Syntax
XSLT stands for Extensible Stylesheet Language Transformation. It is an integrated concept with an XML. It is not used for Visual effects. However, it is used for extracting or transforming data from XML and using the combination of HTML and CSS to format them. It also has dynamic properties, where you can do iteration and conditional statements up
4 min read
XSLT <key> Element
XSLT <key> Element is used to declare the key which is later used to identify the node with the help of the key function. The <xsl: key> tag assigns a specific key to an XML element And the key() function used to access this element of XML Syntax: <xsl:key name = "name" match = "pattern" use = "expression" /> Parameters: name: It
3 min read
XSLT <if> Element
XSLT <if> Element is used to check the condition of the content of XML. The test is used to check the condition if it satisfies the condition then inside if is executed. Syntax: <xsl:if test=EXPRESSION> // Some output if the expression is true </xsl:if>Parameters: test: It is the condition that will check on the content of XML.Exa
2 min read
XSLT <sort> Element
XSLT <sort> Element is used to sort the node which is selected. The XSLT <sort> element is added inside the <xsl:for-each> element. Syntax: <xsl:sort select= node order="ascending" | "descending" case-order="upper-first" | "lower-first" lang=XML:LANG-CODE data-type="text" | "number" />Parameters: select: It is used to select
3 min read
XSLT <value-of> Element
XSLT <value-of> Element is used to extract value from a node selected through name or expression. It is used to get value from XML and then display or output it. In this tutorial, we will learn to implement it. XSLT is a programming language used to convert XML documents into other forms such as HTML, PDF, JPEG, PNG, etc. Syntax<xsl:value-
2 min read
XSLT <foreach> Element
XSLT <foreach> Element iterates over the the XML node. It is used for every XML element of a specified XML node. Syntax: <xsl: for-each select = Expression >// other xsl code</xsl: for-each>Attributes: select: A variable reference to a node or an XPath expression. Example 1: In this example, we will use for-each to print each deta
3 min read
XSLT <choose> Element
The XSLT <choose> Element is used to define the choice among the values of the node. It is the same as the Switch keyword of the CPP language. Syntax: <xsl:choose> <xsl:when test="comparator"></xsl:when> <xsl:otherwise></xsl:otherwise> [It is case when test failes]</xsl:choose> Attributes: test: It is the c
3 min read
XSLT <import> Element
XSLT <xsl:import> element is used to include the content of one stylesheet within another. When you import a stylesheet, it allows you to incorporate the templates, definitions, and other components from the imported stylesheet into the importing stylesheet. Syntax: <xsl: import href=URI></xsl:import>Parameters: URI: It is the pat
3 min read
XSLT <message> Element
In XSLT (eXtensible Stylesheet Language Transformations), the '<xsl: message>' element outputs messages during the transformation process. It's a way to communicate information or provide feedback during the transformation. This element contains all the other XSL elements such as <xsl: text>, <xsl:value-of>, etc. Syntax:<xsl:me
1 min read
XSLT <apply-template> Element
The XSLT Apply Template tag is used to apply the appropriate templates in the context of the selected node. This tag is used to direct the processor to apply templates on node elements of XML. Syntax: <xsl:apply-template select = Expression mode = Name> // Other content</xsl:apply-template> Attributes: select: It is an XPath expression
3 min read
XSLT Transform
What is XSLT Transform?An XSLT transform is the process of applying an XSLT stylesheet to an XML document to produce a new document or presentation of the data. The XSLT stylesheet contains a set of rules and instructions that define how the transformation should occur. Syntax:<!-- Example XSLT stylesheet --><xsl:stylesheet version="1.0" x
5 min read
XSLT <template> Element
XSLT <template> element is a basic building block in XSLT and it provides a way to define rules for transforming specific elements into a source XML document. It is an instruction that tells a browser how to transform XML into something else. It looks for specific things in the XML called patterns and decides what the changed part should look
3 min read
How to parse and process HTML/XML using PHP ?
In this article, we will learn how to process XML using PHP. We have already learnt the basics of XML and their differences with respect to HTML. Different elements of XML can be learnt from XML Elements to understand the working of the following programs. The simplexml_load_string() function is used to parse the given XML and then the XML object c
2 min read
How to Convert XML data into JSON using PHP ?
In this article, we are going to see how to convert XML data into JSON format using PHP. Requirements: XAMPP Server Introduction: PHP stands for hypertext preprocessor, which is used to create dynamic web pages. It also parses the XML and JSON data. XML stands for an extensible markup language in which we can define our own data. Structure of XML:
3 min read
How to Extract Data from an XML File Using PHP ?
XML, which stands for Extensible Markup Language, is a data storage format that is easily searchable and understandable. It simplifies the process of storing, retrieving, and displaying data in informative applications. It is widely used in web applications. In this article, we will learn how to extract data from an XML file using the PHP programmi
3 min read
How to create XML Dynamically using JavaScript?
XML stands for Extensible Markup Language. It is a popular format for storing and exchanging data on the web. It provides a structured way to represent data that is both human-readable and machine-readable. There are various approaches to create XML Dynamically using JavaScript: Table of Content Using DOM ManipulationUsing Template LiteralsUsing DO
2 min read
Convert XML to JSON using JavaScript
Convert XML to JSON effortlessly using JavaScript, enabling seamless integration and manipulation of XML data within your applications. JavaScript libraries like xml-js and xmldom Library simplify the conversion process, ensuring compatibility and efficiency. Below are the approaches to convert XML to JSON in JavaScript: Table of Content Using xml-
2 min read
How to get Attribute Value from XML using JavaScript?
To get the attribute values from XML in JavaScript, We can use DOM manipulation or XPath querying and even use regular expressions to efficiently retrieve attribute values from XML documents. Below are the approaches to get attribute value from XML with Java Script: Table of Content Using DOM ParserUsing XPathUsing Regular ExpressionsUsing the attr
3 min read
How to POST a XML file using cURL?
This article explains how to use the cURL command-line tool to send an XML file to a server using a POST request. A POST request is commonly used to submit data to a server. There are two main approaches to include the XML data in your cURL request: Table of Content Reading from a FileProviding Inline DataReading from a FileThis is the most common
2 min read
How to Create Charts from XML file using CanvasJS ?
Canvas JS is a Charting library that supports multiple devices for displaying the Chart & is easy to use with HTML & Javascript. It provides a different plugin option that facilitates the creation of the basic chart by using the data from the XML file. In this article, we will learn to create Charts from external XML files using the Canvas
5 min read
How to generate an XML file dynamically using PHP?
A file can be generated using PHP from the database, and it can be done by the static or dynamic method in PHP. Static methods can be called directly - without creating an instance of a class. Here we are going to discuss how to create an XML file dynamically. The first thing we need to do is fetch the data from the database. For that, we need to w
3 min read
Read XML file and print the details as tabular data by using JavaScript
Reading an XML file and printing its details as tabular data using JavaScript involves parsing the XML content, extracting relevant data, and then displaying it in a structured table format. This can be done by manipulating the Document Object Model (DOM) and creating HTML tables dynamically in the browser. PrerequisitesBasic Knowledge HTMLBasic Kn
3 min read
Convert XML data into JSON using Node.js
Converting XML data to JSON in Node.js is a common task, especially when working with APIs or services that return data in XML format. This conversion allows you to manipulate the data more easily using JavaScript objects. ApproachTo convert XML to JSON in Node we will use libraries like xml2js. First, parse the XML string, then transform it into J
2 min read
three90RightbarBannerImg