Open In App

XSLT Syntax

Last Updated : 28 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 upon a static XML file.

Uses of XSLT

  • XSLT can be used for organizing large trees of XML elements. So anyone can read it.
  • It is used to transform XML – to – HTML
  • Since XSLT is doing the transformation on the client side. The server has to do the less work.

XSLT Transform Syntax Declaration

  • Since it is an XML file it starts with an XML declaration.
<? xml version = "1.0" ?>
  • Then in order to make it as an XSLT document. We need to use the root tag called stylesheet.
<xsl : stylesheet version = "1.0" xmlns : xsl = "https://www.w3.org/1999/XSL/Transform">
  • This is the first root tag that you define within this XSLT document and it has to be given the version. In order to access all the properties, attributes, and features you have to include this namespace. We include a namespace xmlns and as a parameter we pass xsl with the value “https://www.w3.org/1999/XSL/Transform”
<xsl : template match="/" >
[action]
<xsl : template>

Now XSLT document can contain one or more templates. Templates actually define the rules that you want to place for the elements. The template has an attribute called match. In the attribute, we try to give the Xpath expression [ match = “/” ] which tries to match the pattern in the source XML document.

Document structure of XSLT

<? xml version = "1.0" ?>
<xsl : stylesheet version = "1.0" xmlns : xsl = "https://www.w3.org/1999/XSL/Transform">
<xsl : stylesheet>
<xsl : tempate match="/" >
[action]
<xsl : template>
<xsl : tempate match="/" >
[action]
<xsl : template>
--------------------
</xsl : stylesheet>

Program that displays movie names using XSLT

  • We create one file called movie.xml. We​ d​​eclare the version of XML as discussed above. After that we include the stylesheet called​​ movie.xsl
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "movie.xsl"?>   
  • We define our movies names followed by it’s attribute moviename, genre and year. We pass movieId as the Xpath ​expression in t​he movie tag. All the movies details is enclosed within <list></list> tag.
<list>   
   <movie movieId = "01">  
      <moviename>Harry Potter and the Philosopher's Stone</moviename>
      <genre>Fictional</genre>
      <year>2001</year>
</list>  

Example: Here is the full implementation.

XML




<!-- file name- movie.xml-->
<?xml version = "1.0"?>  
<?xml-stylesheet type = "text/xsl" href = "movie.xsl"?>   
<list>   
   <movie movieId = "01">  
      <moviename>Harry Potter and the Philosopher's Stone</moviename>
      <genre>Fictional</genre>
      <year>2001</year>
   </movie>
   <movie movieId = "02">  
      <moviename>Harry Potter and the Chamber of Secrets</moviename>
      <genre>Fictional</genre>
      <year>2002</year>
   </movie>
   <movie movieId = "03">  
      <moviename>Harry Potter and the Prisoner of Azkaban </moviename>
      <genre>Fictional</genre>
      <year>2004</year>
   </movie>
   <movie movieId = "04">  
      <moviename>Harry Potter and the Goblet of Fire</moviename>
      <genre>Fictional</genre>
      <year>2005</year>
   </movie>
   <movie movieId = "05">  
      <moviename>Harry Potter and the Order of the Phoenix</moviename>
      <genre>Fictional</genre>
      <year>2007</year>
   </movie>
   <movie movieId = "06">  
      <moviename>Harry Potter and the Half-Blood Prince</moviename>
      <genre>Fictional</genre>
      <year>2009</year>
   </movie>
   <movie movieId = "07">  
      <moviename>Harry Potter and the Deathly Hallows – Part 1</moviename>
      <genre>Fictional</genre>
      <year>2010</year>
   </movie>
   <movie movieId = "08">  
      <moviename>Harry Potter and the Deathly Hallows – Part 2</moviename>
      <genre>Fictional</genre>
      <year>2011</year>
   </movie>
</list>


XML




<!-- file name movie.xsl -->
<xsl:stylesheet version = "1.0"   
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">      
   <xsl:template match = "/">     
      <html>   
         <body>  
            <h2>Harry Potter Movie Series</h2>   
            <table border = "1">   
               <tr bgcolor = "#81B0EE"
                  <th>Movie Id</th>
                  <th>Movie Name</th>
                  <th>Genre</th>
                  <th>Year</th>
               </tr>      
              <xsl:for-each select="class/movie">
                  <tr>
                     <td><xsl:value-of select = "@movieId"/></td>
                     <td><xsl:value-of select = "moviename"/></td
                     <td><xsl:value-of select = "genre"/></td
                     <td><xsl:value-of select = "year"/></td
                  </tr>   
               </xsl:for-each>   
            </table>   
         </body>   
      </html>   
   </xsl:template>    
</xsl:stylesheet>


  • We need to include movie.xsl as a stylesheet, just like we have CSS for HTML.
  • Include template with the Xpath attribute match. Since we are using only one XML file. That’s why we use match = “/”.
  • Define the structure of web page using HTML. We are displaying the data in table format.
  • We use for-each to traverse on the list. We are displaying MovieId, Moviename, genre and year.

Output: Now try to open movie.xml file in Internet Explorer.

XSLT_output_2

List of Movies



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads