XML XSLT (Extensible Stylesheet Language Transformations):

Key Features of XSLT:

  • Transformation: Convert XML data into various formats.
  • XPath Integration: Use XPath to select and manipulate data within XML documents.
  • Templates: Define how different parts of the XML document should be transformed.

Example:

Given an XML document of a bookstore:

                    
<bookstore>
  <book id="001">
    <title>XML Fundamentals</title>
    <author>John Smith</author>
    <price>29.99</price>
  </book>
  <book id="002">
    <title>Advanced XML</title>
    <author>Jane Doe</author>
    <price>39.99</price>
  </book>
</bookstore>

                    
                  

XSLT can transform this data into an HTML document or another format.

Basic XSLT Syntax and Templates:

XSLT Stylesheet Structure:

XSLT is an operating script that is created as an XML document, which contains the rules for transforming an XML document. undefined

  • <xsl:stylesheet> or <xsl:transform>: Root element that defines the XSLT document.
  • <xsl:template>: Defines a template rule for matching XML elements.
  • <xsl:value-of>: Extracts and outputs the value of an XML element or attribute.

Example of XSLT Stylesheet:

                    
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <!-- Template for the root element -->
  <xsl:template match="/">
    <html>
      <body>
        <h1>Bookstore</h1>
        <xsl:apply-templates select="bookstore/book"/>
      </body>
    </html>
  </xsl:template>
  
  <!-- Template for book elements -->
  <xsl:template match="bookstore/book">
    <div>
      <h2><xsl:value-of select="title"/></h2>
      <p>Author: <xsl:value-of select="author"/></p>
      <p>Price: $<xsl:value-of select="price"/></p>
    </div>
  </xsl:template>
</xsl:stylesheet>

                    
                  

In this example:

  • This template is used in the document root to begin the HTML structure of the document:
  • The generator defines the template to create HTML tag for each of the books, author, title and price.

Applying XSLT Stylesheets to XML Documents

To apply an XSLT stylesheet to an XML document, you can use various tools and methods, such as:

  • XSLT Processors: Software tools like Xalan or Saxon.
  • Web Browsers: Some browsers can apply XSLT stylesheets to XML documents directly.
  • Programming Languages: Use libraries in languages like Java, Python, or JavaScript.

Example:

To use an XSLT processor to transform the XML document by the specified XSLT stylesheet, the XML document must point to an XSLT stylesheet location.

XML Document with XSLT Reference::

                    
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="bookstore.xsl"?>
<bookstore>
  <book id="001">
    <title>XML Fundamentals</title>
    <author>John Smith</author>
    <price>29.99</price>
  </book>
  <book id="002">
    <title>Advanced XML</title>
    <author>Jane Doe</author>
    <price>39.99</price>
  </book>
</bookstore>
                    
                  

When an XML document is loaded in a web browser that has support for XSLT, then it processes the transformations that are in the ‘bookstore’. contains xsl and shows the HTML form of the output.


Summary

  • XSLT (Extensible Stylesheet Language Transformations): A technology for converting XML documents into different formats with the help of templates and XPath.
  • Basic XSLT Syntax:
    • Stylesheet Structure: Employed with reference to <xsl:stylesheet> and templates with <xsl:template>.
    • XPath Integration: Use <xsl:value-of> and other XPath expressions to select and manipulate XML data.
  • Applying XSLT:
    • XSLT Processors: Utilize API calls or functions to perform XSLT transformations.
    • Web Browsers: Use XSLT for applying transformations on the client-side through web browsers.

This is a very useful tool and one of the most used in the web development and data processing because with XSLT you can format the XML data in many ways.