By completing this article one can know:
- What is XSLT
- Why XSLT
- How to work with XSLT
- Advantages of XSLT.
What is XSLT:
1. XSLT stands for “eXtensible Stylesheet Language Transformations.”
2. XSLT stands for XSL Transformations
3. XSLT is a language for transforming XML documents into XHTML documents or to other XML documents.
4. XSLT uses XPath to navigate in XML documents
1. In the development, I am surprised to find one programming language is taking an ever-increasing proportion of the work and it is none other XSLT.
2. XSLT is a specialized language for converting one data structure into another. It takes XML as input, but its output can be anything.
3. It can be used for a wide variety of development work:
4. It is used for serving web pages;
5. It is used as a report writer for integrating inside and between applications.
6. It is used for generating test data and data loads and for generating code.
XSLT is effective because it understands XML. In other languages, generally we process XML by querying the data structure just like standing outside a shop and trying to pick what you want by poking through the windows with a big stick.
But XSLT understands the data structure it is working on, more like walking through the shop and picking up what you need from the shelves.
At beginning XSLT can be daunting, but after a while it becomes easy, powerful and surprisingly error-free.
How to work with XSLT:
Step1: Take an XML file. Here I have taken a Catalog with child “cd” with some attributes.
Step2: I need the output as:
Step3: To get the Output as required, we are going to use XSLT. So take an XSLT file.
In the image we can see some lines marked as points. So we have to know about them.
<?xml version="1.0" encoding="ISO-8859-1"?>: This line of code implies that the file also XML type and version number and encoding.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">: As we know that XSLT is used for styling XML pages ,we have to include the Stylesheet version with the above line of code.
<xsl:for-each select="catalog/cd">: In XSLT we can write ‘for-each’, ‘for’, ‘if’, ‘switch’ and all the cases just to get the data as per requirement. Here I am going to select ‘cd’ node which is under ‘catalog’ node in my XML file.
<td><xsl:value-of select="artist"/></td> : My XML file contains many nodes under ‘cd’.But I am planning to show output with ‘title’ and ‘artist’ only. So for that purpose I have used ‘xsl:value-of’ which is used to pick the value of node that is specified.
Finally we have to specify the input XSLT for the XML that specifies XML to use the referenced XSLT. We have to set following reference in XML file.
“ <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?> “
Step5: So if we run the program,we will get the output as required.And eventhough if we view code, it just shows only XLM data.
XSLT is a powerful and easy language to manipulate the XML files.
Hope so this is helpful for all Beginners.