0
15kviews
What is XML, XPATH, XSL and XSLT? Explain with an example.
1 Answer
4
720views

eXtensible Markup Language (XML)

  • XML is developed by the World Wide Web Consortium (W3C), which is used as a format to store and transfer data over the Web.
  • XML is a markup language based on simple, platform-independent rules for processing and displaying textual information in a structured way.
  • XML was designed to carry data, not to display data. In XML tags are not predefined. User defines his own meaningful tags.
  • An XML document can be converted into another format, such as HTML and PDF, to make it more readable.
  • XML has been used to implement the following various operations, like
    • Configuration information
    • Publishing
    • Electronic Data Interchange (EDI)
    • Voicemail system
    • Remote Method Invocation (RMI)
    • Object Serialization
  • Example:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>  
<book>  
    <title>Harry Potter</title>  
    <author>J K. Rowling</author>  
    <year>2005</year>    
  </book>  
  <book>  
    <title>Internet Programming</title>  
    <author>Binita Mayekar</author>  
    <year>2018</year>    
  </book>  
</bookstore>

Extensible Stylesheet Language (XSL)

  • XSL is a language for expressing style sheets. An XSL style sheet is, like with CSS, a file that describes how to display an XML document of a given type.
  • W3C developed XSL because an XML based stylesheet language was needed to transform XML documents.
  • XSL act as an intermediary between the XML elements and Web browser. It tells the browser how to display the various elements.
  • XSL used to performs following operations, like
    • Support for browsing, printing, and rendering.
    • Formatting highly structured documents (XML).
    • Performing complex publishing tasks: tables of contents, indexes, reports.
    • Addressing accessibility and internationalization issues.
    • Written in XML.
  • XSL began as a single language to serve the purpose, but further, it was divided into three parts, such as
    • XSLT – For transforming XML document.
    • XPATH – For navigating in XML document.
    • XSL-FO – For formatting XML document.
  • XSL has two attributes
    • ’type’ - The type of file being linked to like ‘text/xsl’.
    • ’href’ - Specify location of the file.
  • Following code structure is used to add XSL file into XML document <?xml-stylesheet type='text/xsl' href='style.xsl'?>
  • Example:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="https://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr style="background-color:lightblue;color:black">
<th align="center">Book Name</th>
<th align="center">Author</th>
<th align="center">Year</th>
</tr>
<xsl:for-each select="bookstore/book">
<tr><td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of select="year"/></td></tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

XSL Transformations (XSLT)

  • XSLT is an XML-based language that transforms an XML documents and generates output into a different format such as HTML, XHTML or PDF.
  • XSLT is an extension of XSL, which is a stylesheet definition language for XML. XSLT is the most important part of XSL.
  • XSLT helps to add or remove elements and attributes from the final output. It also re-arranges and sort elements, performs tests and makes decisions about which elements to hide and display.
  • It allows an XML author to write content only once and put it in many different formats.
  • We simply say like XSLT transforms an XML source-tree into an XML result-tree.
  • XSLT contains many elements that can be used to manipulate, iterate and select XML, for output.
    • <xsl:value-of>- Extracts the value of a selected node.
    • <xsl:for-each> - Select every XML element of a specified node-set.
    • <xsl:if>- Put a conditional if test against the content of the XML file.
    • <xsl:sort> - Sort the output.
    • <xsl:choose> - Used along with <xsl:when> and <xsl:otherwise> to express multiple conditional tests.
    • <xsl:template> - Defines a template.
    • <xsl:apply-template>- Applies a template to the current element or to the current element's child node.

XSLT Working:

  • In the transformation process, XSLT uses XPath to define parts of the source document that should match one or more predefined templates.
  • When a match is found, XSLT will transform the matching part of the source document into the result document.
  • Example: Following example only display the records for book published in year 2018.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="https://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr style="background-color:lightblue;color:black">
<th align="center">Book Name</th>
<th align="center">Author</th>
<th align="center">Year</th>
</tr>
<xsl:for-each select="bookstore/book">
<xsl:if test="year=2018">
      <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="author"/></td>
        <td><xsl:value-of select="year"/></td>        
      </tr>
    </xsl:if>
    </xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

XML Path Language (XPATH)

  • XPath can be used to navigate through elements and attributes in an XML document. It allows XML authors to link to very specific locations within an XML document.
  • XPath uses "path like" syntax to identify and navigate nodes in an XML document.
  • XPath contains over 200 built-in functions.
  • XPath is a major element used in the XSLT standard.
  • XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the path expressions used in traditional computer file systems.
  • The most useful path expressions are as follows:
    • nodename - Selects all nodes with the name "nodename".
    • / - Selects from the root node.
    • // - Selects nodes in the document from the current node that match the selection no matter where they are.
    • . - Selects the current node.
    • .. - Selects the parent of the current node.
    • @ - Selects attributes.
  • Predicates are also used to find a specific node or a node that contains a specific value. Predicates are always embedded in square brackets.
  • XPath wildcards can be used to select unknown XML nodes and also the use of | operator in an XPath expression select several paths.
Please log in to add an answer.