XSLT

  Home  Computer Programming  XSLT


“XSLT Interview Questions and Answers will guide us now that XSLT (XSL Transformations) is a declarative, XML-based language used for the transformation of XML documents into other XML documents. The original document is not changed; rather, a new document is created based on the content of an existing one. So learn XSLT or get preparation for the job of XSLT (XSL Transformations) by the help of this basic XSLT Interview Questions with Answers guide”



31 XSLT Questions And Answers

1⟩ What is the XSLT?

XSLT stands for Extensible Stylesheet Language Transformations(XSLT).This is developed by World Wide Web Consortium(W3C).This is written in XML.We use XSLT when we want to transform an XML document into the oter XML document.Generally we use XSLT when we want to make transformation from a XML document into another XML document,Convert a XML document into the HTML or XHTML document, creating dynamic web pages, can convert an XML document into PDF document. We saved the XSLT file by using .xsl or .xslt extension.Recent version of XSLT is XSLT2.0 launched at 23rd Jan 2007.It is a part of XSL.Editor od first version of XSLT are James Clark.

 179 views

5⟩ Do you feel that you are a good XSLT programmer?

This is definitely a very tricky question. You might be quite confused while answering to this question. Hence you should practice to answer this as well as similar questions at your home. Go for the interview only when you are hundred percent sure that you are ready for the interview.

 184 views

7⟩ How to use <xslsort>element in XSLT?

We use <xsl:sort> element to sort the given output.

Example:

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html>

<body>

<h2>Book Collection</h2>

<table border="1">

<tr bgcolor="#9acd32">

<th>Title</th>

<th>Author</th>

</tr>

<xsl:for-each select="catalog/book">

<xsl:sort select="author"/>

<tr>

<td><xsl:value-of select="title"/></td>

<td><xsl:value-of select="author"/></td>

</tr>

</xsl:for-each>

</table>

</body>

</html>

</xsl:template>

</xsl:styleshee

 205 views

8⟩ How to use filtering in XSLT?

We can filter the XNL output by using filter operators.Some Legal filter operators are given below:

1.=(equal to)

2.!=(not equal to)

3.<(less than)

4.>(greater than)

I have given you a example. In this I have uses '=' equal to filer operation.

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html>

<body>

<h2>Book Collection</h2>

<table border="1">

<tr bgcolor="#9acd32">

<th>Title</th>

<th>Author</th>

</tr>

<xsl:for-each select="catalog/book[author='Jhon Smith']">

<tr>

<td><xsl:value-of select="title"/></td>

<td><xsl:value-of select="author"/></td>

</tr>

</xsl:for-each>

</table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

 181 views

10⟩ What does XSLT processing models involve?

As far as the XSLT processing model is concerned it involves one or more XML documents as well as one ore more XSLT style sheet modules. It also requires XSLT template processing engine (the processor) as well as one or more result documents.

 179 views

11⟩ How you define template in XSLT?

When XSL style sheet has one or more set of rules are told as templates.

We used <xsl:template> element to create templates.

We can attach a template with an XML document by using match attribute.The match attribute value is an XPath exprssion.Like: match="/" use to define whole document.

Example:

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html>

<body>

<h2> Book Collection </h2>

<table border="1">

<tr bgcolor="#9acd32">

<th>Title</th>

<th>Author</th>

</tr>

<tr>

<td>.</td>

<td>.</td>

</tr>

</table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

 181 views

12⟩ How to transform an XML into XHTML?

Below, I write an example which show you how transform an XML into XHTML.

Example:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet

version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns="http://www.w3.org/1999/xhtml">

<xsl:output method="html"/>

<xsl:template match="/persons">

<html>

<head>

<title>Test an XML Example</title>

</head>

<body>

<h1>Persons</h1>

<ul>

<xsl:apply-templates select="person">

<xsl:sort select="family-name" />

</xsl:apply-templates>

</ul>

</body>

</html>

</xsl:template>

<xsl:template match="person">

<li>

<xsl:value-of select="family-name"/>

<xsl:text>, </xsl:text>

<xsl:value-of select="name"/>

</li>

</xsl:template>

</xsl:stylesheet>

To get output on the XHTML we write like that,

<?xml version="1.0" encoding="UTF-8"?>

<html xmlns="http://www.w3.org/1999/xhtml">

<head> <title>Test an XML Example</title> </head>

<body>

<h1>Persons</h1>

<ul>

<li>gupta, Abhi</li>

<li>jain, sudi</li>

</ul>

</body>

</html>

 191 views

13⟩ How to transform an XML document into another XML document?

Here,I given you a exampl which show you how to transform an XML document into another XML document.

Example:

<?xml version="1.0" ?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="/persons">

<root> <xsl:apply-templates select="person"/> </root>

</xsl:template>

<xsl:template match="person">

<name username="{@username}">

<xsl:value-of select="name" />

</name>

</xsl:template>

</xsl:stylesheet>

We can tranform above XML document into another document like that,

<?xml version="1.0" encoding="UTF-8"?>

<root>

<name username="jhoh">jhon</name>

<name username="smith">smith</name>

</root>

 206 views

14⟩ How we compare XSLT and XPath?

Some comparison b/w XSLT and XPath and given below:<br>1.XSLT is depends upon W3C XPath language.Which is use to identify subset of source document tree. XPath is also used to provide the function range.<br>2.Both XSLT and XPath published at same time than we can say that XSLT2.0 trusts on XPath2.0 and XSLT1.0 trusts on XPath1.0.

 181 views

15⟩ How to use filtering function in XSLT?

We can filter the XNL output by using filter operators.Some Legal filter operators are given below:

1.=(equal to)

2.!=(not equal to)

3.<(less than)

4.>(greater than)

I have given you a example. In this I have uses '=' equal to filer operation.

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html>

<body>

<h2>Book Collection</h2>

<table border="1">

<tr bgcolor="#9acd32">

<th>Title</th>

<th>Author</th

 179 views

16⟩ How to perform XML transformation in Java?

XSL transformation is the process of transforming one XML file into another XML, HTML or other type of file based upon selective rules and condition. XSL(XML Style Sheet language) is used to define those rules and condition in a .xls file, which is called style sheet document. Any XSLT engine can read those instruction defined in style sheet document and transform source XML file into something expected. Core of XSLT is, transformation engine and style sheet document. XSLT engine can be written in Java or any other language. Java has XSLT support via javax.xml.transform package which specifies classes like Templates, TransformFactory, an implementation of abstract factory design pattern, which can be used to read XSL file and transform XML files.

 188 views

17⟩ Explain how to remove a particular element from XML?

Removing element from XML document via XSL transformation or XSLT is easy if you are familiar with Identity template. You need to write two templates one is Identity template, which copies every thing and other for matching with particular element and doing nothing just like shown below, which will then result in removal of a that particular element. See an example of removing XML elements using XSLT for details.

<xsl:template match="/root/product"/>

 181 views

18⟩ Explain how to remove a particular attribute from XML?

Process of removing an attribute is similar to removing elements from XML document, as discussed in above XSLT interview question. Along with Identity template, define another template to match with that particular attribute as shown below.

<xsl:template match="@product_synonym"/>

 194 views

19⟩ Explain how to rename a particular element and attribute from XML using XSL?

Renaming attribute is also similar to removing or deleting attribute as discussed in XSLT question 1, but instead of not doing anything when an attribute matches, you need to create an attribute and copy value of current attribute into new attribute. Identity template will be same and you need to add another template for renaming attribute using XSL:

<xsl:template match="@id">

<xsl:attribute name="emp_id">

<xsl:value-of select="." />

</xsl:attribute>

</xsl:template>

if you are using XSLT 2.0 than instead of separate <xsL:value-of> element you can use select attribute directly with <xsL:attribute> as shown below

<xsl:attribute name="emp_id" select=".">

 194 views

20⟩ Tell me what is Identity template in XSL, why do you use it?

Identity template in XSL is used to create deep copy of source XML file. It's template matches to every node() and attribute and copy everything to create copy of original xml file. many people define Identity template in its own file like Identity.xsl but some people also preferred to keep in main XSL file as top template. Identity template has several uses in XSL transformation, like if you want to remove any attribute or element you will most likely copy everything using Identity template and create another template for not doing anything for those attribute or elements.

<xsl:template match="@|node()">

<xsl:copy>

<xsl:apply-templates select="@|node()"/>

</xsl:copy>

</xsl:template>

Above template is called Identity template. If you look at definition first template matches any attribute or

any node and then copies current node including any attributes and child nodes.

 196 views