Displaying XML Using XSLT Last Updated : 13 May, 2021 Comments Improve Suggest changes Like Article Like Report 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 <?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 <?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 <?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 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <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 : Comment More infoAdvertise with us Next Article Displaying XML Using XSLT A AniketSingh1 Follow Improve Article Tags : Web Templates Web technologies-HTML and XML HTML and XML 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 C 4 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 c 3 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 4 min read XML declarations An XML declaration is a statement placed at the beginning of an XML document that provides essential information about the document's encoding, version, and standalone status. It serves as a metadata header and helps parsers and processors understand how to interpret the XML content correctly. Synta 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 3 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 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 3 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 = "patte 3 min read XML - CDATA Sections CDATA sections are a mechanism in XML for handling character data that might otherwise be misinterpreted by the XML parser. CDATA stands for Character Data. These sections include blocks of text within an XML document that the parser should treat literally, without interpreting any characters as XML 2 min read Like