A simple way to transform the XML document into other formats such as XHTML, XSLT is used. Well, there is much more to learn about XSLT.
So, in this blog, we will cover all the basics of XSLT. Also, it consists of several topics like understanding the purpose of XSLT, XSLT as a translation language, and more. However, before learning about XSLT, it will be an advantage to know little about XSL for better understanding.
So, let’s begin with the brief introduction on XSL and why is it needed.
An Overview of XSL
XSL is an acronym for eXtensible Stylesheet Language. This is very same for XML, as CSS is to HTML.
Need for XSL
As we know, in HTML document, tags are already defined like a div, table, &span; and even the browser knows the way to add style to them and display those using CSS styles. However, in XML, tags are not already defined.
So, to style and understand an XML document, XSL is developed by World Wide Web Consortium (W3C). It can act as XML based Stylesheet Language. In other words, an XSL document specifies how a browser should render an XML document.
However, three main components of XSL are:
1. XSLT − To transform an XML document into various other types of document, we use XSLT.
2. XPath − To navigate an XML document, we use XPath.
3. XSL-FO − We use it to format XML document.
An Overview of XSLT
XSLT (Extensible Stylesheet Language Transformations) offers the ability to transform XML data from one format to another, automatically.
Working of XSLT
To define the transformation rules to be applied to the target XML document, XSLT stylesheet is used. However, XSLT stylesheet is written in XML format.
As its working, the XSLT Processor takes the XSLT stylesheet and, further applies the transformation rules on the target XML document.
As an outcome, it generates a formatted document in the form of HTML, XML, or text format. Furthermore, this formatted document is utilized by XSLT formatter to generate the actual output that is to be displayed to the end-user.
Basic Principle of XSLT
Patterns and Templates-
1. The transformation rules are described by a style sheet.
2. A Pattern + Template is a transformation rule.
3. A configuration in the source tree is Pattern.
4. A structure to be instantiated in the result tree is Template.
5. The corresponding pattern is generated in the result tree, while a pattern is matched in the source tree.
Displaying XML with XSLT
Generally, it is the recommended style sheet language for XML. It is far more sophisticated than CSS. Moreover, it is possible to add/remove elements and attributes to or from the output file with XSLT.
Also, we can rearrange and sort elements, perform tests. It also gives the flexibility to make decisions about which elements to hide and display, and a lot more. Additionally, to find information in an XML document, XSLT uses XPath.
The Transformation Process
An XML document is simply a collection of nodes to an XSLT. The collection of nodes are:
1. Root node
2. Element nodes
3. Attribute nodes
4. Text nodes
5. Processing instruction nodes
6. Comment nodes
Though, there are one or more templates in an XSLT document, which are created with the <xsl: template/> tag. The XSLT processor reads through the XML document starting at the root when an XML document is transformed with an XSLT that is one level above the document element and progressing from top to bottom, same as any person would read the document. Once the processor encounters a node, it looks for a matching template in the XSLT.
It applies if only it finds a matching template, else it uses a default template as defined by the XSLT specification. Some default templates are:
- Node Type: Default Template
- Root: Apply templates for child nodes.
- Element: Apply templates for child nodes.
- Attribute: Output attribute value.
- Text: Output text value.
- Processing Instruction: Do nothing.
- Comment: Do nothing.
Moreover, attributes get ignored by the XSLT processor unless they are explicitly referenced by the XSLT document because attributes are not considered children of the elements that contain them.
All XSLT documents must be well-formed and valid XML documents. Hence it is necessary to follow the same syntax rules which apply to any other XML document.
So, below are some points we need to remember when creating XSLT documents:
1. XML Version
XML documents and XSL documents are similar so we should include the XML version in the document's prolog. Also, we must set the standalone attribute to no as we now rely on an external resource (i.e., the external XSL file).
<?xml version="1.0" standalone="no"?>
2. XSL Root Element
Further, we open the root element — xsl: stylesheet. The root element needs to include the XSL version and the XSL namespace.
3. XSL Namespace Prefix
There must be the XSL prefix in all XSL elements in your XSLT document.
XSLT <template> Element
We can see XSLT is all about being able to select one or more nodes from your XML document and further transforming or replacing its content with something else.
A node could be any of the below mentioned:
So, the tool which we use to select a node from your XML document and transform its contents is the <xsl: template> element.
However, we use the match attribute to select an element. And, we place the new content between the opening (<xsl: template>) and closing (</xsl: template>) tags to transform its contents.
XSLT and XPath
With XSLT’s development, its creators found that there was an overlap between the expression syntax in XSLT for selecting parts of a document and the XPointer language that was being developed for linking one document to another.
So, for this reason, the creators of XPointer and XSLT decided to combine both. And that resulted in a single language known as XPath.
So, XPath provides a sublanguage encapsulated within the XSLT stylesheet in the world of XSLT. Moreover, we can use XPath expressions for most programming operations, such as simple calculations or even testing for conditions.
Advantages of XSLT
Here are some of the advantages of using XSLT.
1. There is no dependency on programming. Generally, Transformations are written in a separate XSL file that is again an XML document.
2. By simply modifying the transformations in the XSL file, it is possible to alter the output. There is no need to change any code. Therefore, web designers can edit the stylesheet and also can see the change in the output quickly as well as easily.
3. Moreover, it applies user-defined transformations to an XML document. Though, its result can be HTML, XML, or any other structured document. That shows, we can easily merge XML data into the presentation.
4. To locate elements/attribute within an XML document, XPath used by XSLT. Therefore, it is a very convenient way to traverse an XML document instead of a traditional way, via the scripting language.
5. XSLT is more resilient to changes in documents than low-level DOM and SAX, being template based.
6. It is very easy to change the output format at any time easily without touching the code-behind by separating data (XML document) from the presentation (XSLT).
7. The application UI script will look clean and will be easier to maintain using XML and XSLT.
8. It is possible to use XSLT as a validation language as it uses tree-pattern-matching approach.
Disadvantages of XSLT
1. Implementing complicate business rules in XSLT is not easy.
2. It is difficult to change the variable value in looping in XSLT.
3. In some cases, the use of XSLT cause performance penalty. For example, its engine doesn’t optimize code by using a caching technique like a traditional compiler.
In all, we can say is that in contrast to languages treating XML as an adjunct to their base nature, XSLT Turing-complete programming language is created using and for XML.
Even there are several benefits of XSLT over XQuery and other imperative languages, primarily in the polymorphism of transformation behaviors for creating different but related solutions.
The investment is returned in the integrity, flexibility, and robustness of the stylesheets deployed, while the language needs training and practice in the art of creating nuanced and powerful stylesheet fragments.
Therefore, using XSLT is necessary to our information processing planning for XML and other structured content as is found in databases.