XSLT - interview questions and answers
What is the function of XSLT?
- XSLT is the transformation language used for XML documents and it performs high level of functions.- XSLT provides a way to solve the complex problems like styling and the operations related to the generation of table and its contents.
- It also provides the support for the indexes and other processing languages that can be integrated with one another.
- The transformation language is used to provide XSL to generate the HTML pages using the XML data.
- It defines the semantics and syntax using the XSLT language and expresses the XML document in a well formed manner.
What are the roles of XSLT?
XSLT is used to transform an XML document into another XML document such as HTML, etc.XSLT can be used to add or remove elements and attributes to or from the output file.
XSLT can also be used for rearranging and sorting elements.
It can also be used for performing tests and making decisions about hiding and displaying of elements
Explain the XSLT data model
XSLT shares the same data model of XPATH with some additions. XSLT accepts a XSLT stylesheet as an input and output is another XML, text or another document. The model is based on text nodes. These text nodes are embedded within element nodes. The model is a hierarchical structure like a tree. The tree has a variety of nodes like- text nodes, attribute nodes, element nodes, comment nodes, and processing instruction nodes. For every node type there is a way of determining a string-value for a node of that type.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.<xsl:template match="/root/product"/>
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.<xsl:template match="@product_synonym"/>\
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: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=".">
How you define functions in XSLT?
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
ex: current(),document(),element-variable(),function-variable(),key,gererated-id() etc.
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.<br><br>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.<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.