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" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- Template for matching the root element of the XML document --> <xsl:template match=""> <html> <body> <h2>Transformed Output</h2> </body> </html> </xsl:template> <!-- Template for matching specific elements in the XML document --> <xsl:template match="book"> <p><xsl:value-of select="title"/></p> </xsl:template>
</xsl:stylesheet>
- The <xsl:stylesheet> element is the root element that declares the XSLT stylesheet.
- The version attribute indicates the XSLT version being used.
- The xmlns:xsl attribute defines the XML namespace for XSLT.
- <xsl:template> elements define templates for matching specific elements in the XML document.
- The match attribute specifies the pattern to match in the XML document.
- <xsl:value-of> is used to extract and output the value of a selected element.
Elements used in XSLT Transform:
1. <xsl:stylesheet>: The root element that encapsulates the entire XSLT stylesheet.
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
2. <xsl:template>: Defines a template rule that specifies how a particular element or set of elements should be transformed.
<!-- This XSL template is designed to match the root element ("") of the input XML document. --> <xsl:template match=""> <html> <body> <!-- Heading level 2 for the transformed output --> <h2>Transformed Output</h2> </body> </html> </xsl:template>
3. <xsl:value-of>: Extracts the value of a selected node or expression and includes it in the output.
<!-- This XSL template is designed to match elements with the class "book" in the input XML document. --> <xsl:template match="book"> <!-- Div element with the class "book" for styling purposes --> <div class="book"> <!-- Heading level 2 for the book title --> <h2><xsl:value-of select="title"/></h2> <!-- Paragraph for displaying the author of the book --> <p>Author: <xsl:value-of select="author"/></p> <!-- Paragraph for displaying the published year of the book --> <p>Published Year: <xsl:value-of select="publishedYear"/></p> </div> </xsl:template>
4. <xsl:apply-templates>: Triggers the processing of child nodes using their corresponding templates.
Example: Apply templates to process child nodes.
<!-- This XSL template matches the root element ("") of the input XML document. --> <xsl:template match=""> <html> <body> <!-- Heading level 1 for the transformed content --> <h1>Transformed Content</h1> <!-- Applying templates to process child elements of the root --> <xsl:apply-templates/> </body> </html> </xsl:template>
5. <xsl:for-each>: Iterates over a selected set of nodes and applies a template for each item in the node-set.
<!-- xsl:for-each to iterate over 'book' elements --> <xsl:template match=""> <html> <body> <h1>Book List</h1> <!-- Use xsl:for-each to iterate over 'book' elements --> <xsl:for-each select="library/books/book"> <div class="book"> <h2><xsl:value-of select="title"/></h2> <p>Author: <xsl:value-of select="author"/></p> </div> </xsl:for-each> </body> </html> </xsl:template>
6. <xsl:if>: Provides conditional processing, allowing certain actions to be taken based on a specified condition.
<!-- xsl:if for conditional processing --> <xsl:template match="book"> <div class="book"> <h2><xsl:value-of select="title"/></h2> <p>Author: <xsl:value-of select="author"/></p> <!-- Use xsl:if to conditionally display a message --> <xsl:if test="publishedYear < 2000"> <p>Older Book</p> </xsl:if> </div> </xsl:template>
7. <xsl:choose> and <xsl:when>: Allows multiple conditions to be tested, with different actions taken based on the first condition that evaluates to true.
<!-- xsl:choose and xsl:when for multiple conditions --> <!-- This XSL template is designed to match elements named "book" in the input XML document. --> <xsl:template match="book"> <!-- Styled division for a book with the class "book" --> <div class="book"> <!-- Heading level 2 for the book title --> <h2><xsl:value-of select="title"/></h2> <!-- Paragraph for displaying the author of the book --> <p>Author: <xsl:value-of select="author"/></p> <!-- xsl:choose to handle multiple conditions based on publishedYear --> <xsl:choose> <!-- Condition for books published before 2000 --> <xsl:when test="publishedYear < 2000"> <p>Older Book</p> </xsl:when> <!-- Condition for books published between 2000 and 2010 --> <xsl:when test="publishedYear >= 2000 and publishedYear < 2010"> <p>Published between 2000 and 2010</p> </xsl:when> <!-- Default condition for recent books --> <xsl:otherwise> <p>Recent Book</p> </xsl:otherwise> </xsl:choose> </div> </xsl:template>
8. <xsl:text>: Defines text content to be included in the output.
<!-- xsl:text to output text content --> <xsl:template match="book"> <div> <h2><xsl:value-of select="title"/></h2> <p>Author: <xsl:value-of select="author"/></p>
<!-- xsl:text to add a line break --> <xsl:text> </xsl:text>
<!-- xsl:text to add additional text content --> <xsl:text>Published Year: </xsl:text> <xsl:value-of select="publishedYear"/> </div> </xsl:template>
9. <xsl:copy>: Copies the current node or a specified node to the output.
<!-- xsl:copy to copy elements --> <xsl:template match="book"> <!-- xsl:copy to copy the 'book' element to the output --> <xsl:copy> <!-- Copy all attributes of the 'book' element --> <xsl:copy-of select="@*"/>
<!-- Add additional elements or modify existing elements if needed --> <additionalInfo>Additional Information Here</additionalInfo>
<!-- Process child nodes of the 'book' element --> <xsl:apply-templates/> </xsl:copy> </xsl:template>
10. <xsl:copy-of>: Copies the current node-set or a specified node-set to the output.
<!-- xsl:copy-of to copy nodes --> <xsl:template match="library"> <!-- xsl:copy-of to copy all child nodes of 'library' to the output --> <xsl:copy-of select="*"/> </xsl:template>
11. <xsl:message>: Outputs a message to the application or system.
<!-- xsl:message to output messages --> <xsl:template match=""> <!-- xsl:message to output a message --> <xsl:message>Transformation started!</xsl:message>
<html> <body> <h1>Transformed Content</h1> <!-- Apply other templates or transformation logic as needed --> <xsl:apply-templates/> </body> </html>
<!-- xsl:message to output another message --> <xsl:message>Transformation completed!</xsl:message> </xsl:template>
Conclusion
In summary, XSLT transformation serves as a robust mechanism for the stylized presentation of XML data. Proficiency in XSLT enables developers with the ability to convert raw XML data into sophisticated and user-specific outputs. Within the dynamic realm of web development, a distinct comprehension of XSLT has a great significance, serving as an important asset that unlocks a realm of transformative possibilities.
|