XML Interview Questions

How to Transform XML with XSLT?

XSLT’s approach for converting XML into a more legible format for humans. The system is referred to as eXtensible Stylesheet Language: Transformation (XSLT). As an XML semantic, XSLT is a computer language. An XML file is written or created first, followed by an XSLT file, and then the two are combined using software to form a third file, much like CSS. The third file can be any plain text file, such as another XML file, a story, or even a complex collection of instructions like structured query language (SQL) queries that are meant to be used with a relational database application. Programming using JavaBean conventions can already be employed.

Unlike CSS or XHTML, XSLT is a computer language, in comparison. It includes all necessary function calls, conditional processing, and input arguments. The majority of computer languages are procedural, however XSLT is declarative. This also implies that after variables have been defined, it is impossible to modify their value.

Several XSLT processors are offered for various Java, Perl, and platform-specific operating systems:

  • Xerces and Xalan  – Java-based implementations
  • xsltproc  – A binary application built using a number of C libraries, and also comes with a program named xmllint used to validate XML documents
  • Sablotron – Another binary distribution built using C++ libraries and has both a Perl and a Python API
  • Saxon – another Java implementation

What is XSLT?

A programming language in the form of an XML file is called XSLT. As a result, every command is an XML element, and each command is qualified by an XML attributes

XSLT Commands

  • stylesheet – This is the root of all XSLT files. It requires attributes defining the XSLT namespace and version number. This is pretty much the standard XSLT stylesheet definition:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">.
  • output – This is used to denote what type of text file will be created as output and whether or not it requires indentation and/or a DTD specification. For example, this use of output tells the XSLT processor to indent the output to make the resulting text easier to read:
<xsl:output indent="yes" />.
  • template – This command is used to match/search for a particular part of an XML file. It requires an attribute named match and is used to denote what branch of the XML tree to process. For example, this use of template identifies all the things in the root element of the XML input file:
<xsl:template
match="/">.
  • value-of – Used to output the result of the required attribute named select which defines exactly what to output. In this example, the XSLT processor will output the value of a letter’s date element:
<xsl:value-of select="/letter/date/" />.
  • apply-templates – Searches the current XSLT file for a template named in the command’s select statement or outputs the content of the current node of the XML file if there is no corresponding template. Here the apply-templates command tells the processor to find templates in the current XSLT file matching paragraph or list elements:
<xsl:apply-templates select="paragraph | list" />.
  • Besides XSLT commands (elements), XSLT files can contain plain text and/or XML markup. When this plain text or markup is encountered, the XSLT processor is expected to simply output these values.

This is what allows us to create XHTML output. The processor reads an XML file as well as the XSLT file.

Example: Transform XML with XSLT

Example, we will transform the simplest of XML documents using XSLT.


Here is a very simple XML document:

<content>Hello, World!</content>

Our goal is to transform this document into a plain text output. To do that we will use this XSLT stylesheet,

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- plain o' text -->
<xsl:output method='text'/>
<!-- match the root element -->
<xsl:template match="/content">
<!-- output the contents of content and a line-feed -->
<xsl:value-of select='.'/>
<xsl:text>&#xa;</xsl:text>
<!-- clean up -->
</xsl:template>
</xsl:stylesheet>

The above post explains Transform XML with XLST, It gives a brief understanding of messaging and important transformation concepts are explained.