Questions and answers taken from real job interviews.
Find real interview questions and answers on this website:
This kind of XSL transformation questions are more popular to gauge real understanding of templates. they can write template in front of you and may ask you to explain, most simple example of this is writing Identity template as discussed in XSLT interview question 3. Alternatively they may give you XSL and XML file and ask you about about of transformation. This are tricky questions in XSL and in order to answer these question you need to be familiar with XSL language, which is the primary reason people ask it. On the other hand this is an excellent opportunity to show you how well you know about XSL working or how template executes, by clearly explaining what a particular template does.
<xsl:template match="date"> <xsl:variable name="M" select="substring-before(., '/')" /> <xsl:variable name="D" select="substring-before(substring-after(., '/'), '/')" /> <xsl:variable name="Y" select="substring-after(substring-after(., '/'), '/')" /> <xsl:value-of select="concat($Y, '-', format-number($M, '00'), '-', format-number($D, '00'))" /> <br /> </xsl:template>
Even if candidates have never participated in a project involving this type of architecture, they should recognize it as one of the common uses of XML. Querying a database and then formatting the result set so that it can be validated as an XML document allows developers to translate the data into an HTML table using XSLT rules. Consequently, the format of the resulting HTML table can be modified without changing the database query or application code since the document rendering logic is isolated to the XSLT rules.
The SQLXML 3.0 and advanced versions provide the facility of mapping the SQL queries output with XSLT templates. It uses XSLT to present the records that are retrieved from databases on Web pages (HTML pages). An application can use XSLT to modify the output that is retrieved from data sources and display the output by XSL templates. The XSLT displays data without affecting the database query and the code of application.
Using choose conditon in XSL file we can add some multi conditional test cases. 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"> <tr> <td><xsl:value-of select="title"/></td> <xsl:choose> <xsl:when test="price > 150"> <td bgcolor="#ff00ff"> <xsl:value-of select="author"/></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="author"/></td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> In the above program we display those entries of Author column with pink color WHEN book price is higher than 150.
This is one of the XSLT interview questions which checks practical knowledge of candidate in XSL. This is one of the most common application of XSLT I have seen where data stored in relational database is converted into XML and by using XSLT transformed into HTML pages. Database stored procedure can be used for first part and having all the logic of rendering HTML in XSLT you don't need to change your query now and then if you need to change structure of HTML pages. If candidate successfully answer this XSLT interview question then there is very good chance that he has a good understanding of how things works with database, xml and XSLT. Now let’s see couple of XSLT Interview question for practice, you need to find answer of these two questions by yourself, and once you find the answer, you can also post them as comment here. The reason, I am not giving answer of these question here because, they are extremely basic and should come as experience, i.e. you would better write code for that. That will enable you to understand other XSLT questions as well.
Process of removing an attribute is similar to removing elements from XML document, as discussed in aboveXSLT interview question. Along with Identity template, define another template to match with that particular attribute as shown below. <xsl:template match="@product_synonym"/>
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 element you can use select attribute directly with as shown below <xsl:attribute name="emp_id" select=".">
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.
This XSLT interview question is pretty common in many XML interviews as well. If candidate has worked in XSLT then this is a fairly easy question as it just need to come up with a XSLT template which can copy an attribute from an element like below: <xsl:template match="/employees/employee"> Value of attribute Id is : <xsl:value-of select="@id"></xsl:value-of> </xsl:template>
That’s all on my list of XSLT and XML transformation interview questions and answers. XSLT is one of the important skill to have in your resume, if you are using XML in your project. Since XML is mostly used as transportation protocol and middle and back office systems, those roles look for candidates which are good in XML, XSL and XSLT transformation. So if you are applying for any middle and back office Java development role in Investment banks, make sure to prepare XSLT well.
Using <xsl:apply-template>element we can apply a template on current element or can apply on the child nodes of current element. 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> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="book"> <p> <xsl:apply-templates select="title"/> <xsl:apply-templates select="author"/> </p> </xsl:template> <xsl:template match="title"> Title: <span style="color:#ff0000"> <xsl:value-of select="."/></span> <br /> </xsl:template> <xsl:template match="author"> Author: <span style="color:#00ff00"> <xsl:value-of select="."/></span> <br /> </xsl:template> </xsl:stylesheet>
When we want to use <xsl:choose> element in XSL file.Than we use <xsl:when> and<xsl:otherwise> inside the <xsl:choose> element.Using them we can show multiple conditional test cases. Syntax: <xsl:choose> <xsl:when test="expression"> ... Than gives that output ... </xsl:when> <xsl:otherwise> ... Than gives that output .... </xsl:otherwise> </xsl:choose>
Using <xsl:if> element we can test the content of an XSL document. Syntax: <xsl:if test="expression"> ...Write here output it will dispay when condition is true... </xsl:if> I have given you example to use of <xsl:if> element. <?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:if test="price > 150"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="author"/></td> </tr> </xsl:if> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> It will give the title of book with related author which book cost greater than 150.
XSLT has more than 100 build-in functions.This functions are made for string values,date and time comparison,numeric values,sequence manipulation, Node and QName and boolean variable etc. We can find out the XSLT function namespace from http://www.w3.org/2005/02/xpath-functions I have given you some XSLT build-in functions. current(),document(),element-variable(),function-variable(),key,gererated-id() etc.
Successful candidates should recognize this as one of the most basic applications of XSLT. If they are not able to construct a reply similar to the example below, they should at least be able to identify the components necessary for this operation: xsl:template tomatch the appropriate XML element, xsl:value-of to select the attribute value, and the optional xsl:apply-templates to continueprocessing the document. Extract Attributes from XML Data Example 1. <xsl:template match="element-name"> Attribute Value: <xsl:value-of select="@attribute"/> <xsl:apply-templates/> </xsl:template>
The components necessary for the above mentioned operation are as follows: • The template element - Matches the correct XML element. • The value-of element - Selects the attribute value. • The optional apply-templates element - Allows continuous processing of the document
1) Stylesheet parameters are declared with <xsl:param> instead of <xsl:variable>. 2) You can override the (default) value of a stylesheet parameter by specifying a value on the command line, or in some other way depending on how you invoke the XSL processor.
XPath expressions can evaluate to numbers, strings, boolean, and node sets. Result tree fragments are also used in XSLT.
The interfaces of SAX are: • DocumentHandler- is used for getting event notification relating to a document. • DTDHandler- is implemented to get the notifications related to declarations in DTD like entities and notations • EntityResolver- is used for reading external entities. • ErrorHandler- is used for handling error related notifications.
CDATA Sections are used to escape blocks of text containing characters which would otherwise be recognized as markup. All tags and entity references are ignored by an XML processor that treats them just like any character data. CDATA blocks have been provided as a convenience measure when you want to include large blocks of special characters as character data, but you do not want to have to use entity references all the time.
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 likeIdentity.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 as discussed in XSLT interview questions 1 and 2. <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.
The difference is that the latter gives the string value of its select expression, which in the case of a RTF or node-set would be simply the text in the text nodes; whereas the former copies all (the elements, attributes, and other nodes) of the result of the select expression.
An XSL transformer will transform in the following way: • The source tree is obtained by parsing in a normal XML style • The transformation is now applied to the source with the help of information available in Stylesheet.
XPath is an expression language used for addressing parts of an XML document. XPath is used to navigate through elements and attributes in an XML document.
Template specifies transformation rules. A Stylesheet document can be made up of at least one template, which acts as an entry point. Every template uniquely identifies a particular node in the source tree.
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. See XSL transformation in Java for more details
eXtensible Stylesheet Language(XSL) deals with most displaying the contents of XML documents.XSL consists of three parts: • XSLT - a language for transforming XML documents • XPath - a language for navigating in XML documents • XSL-FO - a language for formatting XML documents
XSL-FO deals with formatting XML data. This can be used for generating output in a particular format like XML to PDF, XML to DOC, etc.
This is an extension or follow up questions of previous XSLT question about Identity template. we useselect="@|node() to copy all child element and any attribute.if we don't use that than <xsl:apply-templates/> will default on select="node()" which will copy child nodes except attributes.