What is XSLT

XSLT means eXtensible Stylesheet Language Transformation. It's an important component in XML technology architecture. It's a domain specific language allows you specify the rules to transform an XML document to other formats like another XML document, HTML document or pure text.

The power for XSLT not just comes from itself, but also from the fact that the source document is represented as XML. When you store your data as XML, it gives you a lot of potential flexibility for future changes with the help of tools like XSLT. This flexibility comes from the separation of structure and presentation. Just like what happens in HTML and CSS.

XSLT is the natural next step of learning XML technology.

Some XML files only save data, they never need to be displayed graphically. They are used as a data structure. For example the configuration file for Visual Studio and many Java libraries. Some even be used as programming language, for example the Ant built tool.

Even XML can be a very good choice for configuration and data structure, but the most useful feature of XML is to represent document, but it only keeps the meta information and structure of the document. It's not supposed to include the display information like font, color, line height, indent and stuff like that.

Ultimately, documents needs to be view by human on various devices and applications. In those devices, different kind of data need different style. Title may need a bigger font, paragraphs may need different margin and padding.

Now we have various devices that can display document, from cell phone, Kindle to desktop browser and PDF viewer. Create different version of the same document for all these devices will be overwhelming.

The solution is XSLT. The idea is XML will only store the meta data of document, then transform it to different platforms. For example transform XML document to HTML.

XSLT itself is XML

We have mentioned that XML can be used to implement a programming language. It's not surprise that XSLT itself is implemented in XML.

So why it's not implemented in general programming language like Java or Python? You may ask. Because writing code to handle XML transformations in XSLT is much easier than in any other commonly used programming languages. You can think of it as a Domain Specific Language.

XSLT is highly domain specific, elements are first class object. In XSLT you can easily select a part of a document and convert it to another format. Or reconstruct the elements in origin document, for example an element named chapter can be converted to h1 element in HTML and a subtitle can be converted to h3.

You can select all the leaf node in a tree and convert it to a flat list. These tasks are coded in high level of abstraction, actually it's feels not like programming, it feels like marking up or configuring.

XSLT is more powerful than CSS

Both XSLT and CSS can add styles to a document, but XSLT can also change the structure of the source document.

For an element, CSS can change it's color and font size , etc. XSLT can turn it into completely different things, actually it can be anything. You can think of it the strongest template engine.

If you define your data or document as XML, you can get a great flexibility by using XSLT. XML will be way much better than your customized data format, like pure text or binary format.

And this is the reason why XML will replace other data formats and be used almost everywhere. The advantage is so obvious that you have no reason to ignore it.

If you understand it you will know another popular data format like JSON is not comparable with XML. They are completely different. If you choose JSON, you lost a lot future potential and flexibility of your data. There are a lot debates about XML and JSON, which one to use. When you truly understand XML, XSLT and the whole ecosystem, you will never be bothered by this question.

Template and pattern matching

The template and pattern matching are two most important idea and component in any text transformation system. XSLT is no exception.

A typical process in XSLT is it will create a template and find elements in the source document , extract value from the elements like attributes, children and then fill into different positions in the template.