XSLT stands for XML style sheet transformation and it as XML technology used to transform one XML into another XML or HTML format. XML and XSLT interview questions are commonly asked to Java developers, who happen to use XML in there project and mentioned XSLT as key skill in there resume. Given XML’s popularity as data transfer protocol, many systems in middle and back office space uses XML messages to transfer trade details, for example Bookings, Settlement, and Confirmation system uses it as a data exchange protocol. Since each of this system performs some normalization, enrichment and transform on incoming trade message, they use XSLT for those transformation. XSLT is rich, powerful and given it’s support in Java and several other programming language, it comes natural choice of XML transformation. What you need to is to write XSL files, also known as XML style sheet to specify your transformation rule and than XSLT engine will transform each incoming XML documents as per your XSL file. Though, XSLT is rich, it’s can also be very complex for Java programmers, who are used to procedural style of coding, as XSLT uses recursion a lot. So if you are a Java programmer, who have used XSLT or going for a Job interview, where XSLT is a key skill, you better be prepare with some popular XSLT interview questions, In this article, I am sharing my list of XSLT questions, which is collected from internet, friends and colleagues and frequently asked as part of XML Interview questions.
XSLT or XML transformation Interview Questions Answers
Here is my list of XSLT questions, which is frequently asked to Java developers for projects, who uses XML extensively for transferring data, generating dynamic HTML and other XML transformation task. Though these XSLT interview question are not very difficult, but they can be tricky, given XSLT not being Java programmers main strength and lack of time on learning XSLT. You can use these questions for preparation or refreshing your knowledge before going for any Java and XML based Job interview.
Question 1: What is XSL transformation or XSLT? How do you perform XML transformation in Java?
Answer : 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
Question 2: How to remove a particular element from XML?
Answer : 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.
Question 3: How to remove a particular attribute from XML?
Answer : 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.
Question 4: How to rename a particular element and attribute from XML using XSL?
Answer : 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:value-of select="." />
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=".">
Question 5: What is Identity template in XSL, why do you use it?
Answer : 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 as discussed in XSLT interview questions 1 and 2.
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.
Question 6 : Why we use select="@|node()" in the <xsl:apply-templates/> element on Identity template? what will happen if we use <xsl:apply-templates/> without select attribute?
Answer : This is an extension or follow up questions of previous XSLT question about Identity template. we use select="@|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.
Question 7: Can you explain me this XSL template? What output this XSL template will produce given a particular xml file?
Answer : 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.
Question 8 : How to retrieve value of an attribute for an element using XSLT?
Answer : 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:
Value of attribute Id is :
Question 9 : How do you generate dynamic HTML pages from relational database using XSLT?
Answer : 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.
How to transform one XML to another XML document using XSLT transform?
How to transform an XML file into HTML using XSL transformation (XSLT)?
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.
Related Java and XML Tutorials from Javarevisited Blog