EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials XML Tutorial XSLT Functions
Secondary Sidebar
Asp.Net MVC Interview Questions

DB2 Interview Questions

What is ARP?

Switch Statement in JavaScript

Remove Duplicate Elements from Array Javascript

Sort Array 0s,1s and 2s

XSLT Functions

XSLT Functions

Introduction to XSLT Functions

XSLT has many inbuilt functions to do the manipulation of strings, numeric values, date and many more. They are defined by the prefix namespace fn: and gives the meaningful functions for the respective category. The core activities of creating an XSLT function are done with the templates where the logic of the functions is implemented, which is written using Standard XSLT 1.0 or 2.0. Every function should have one or more signatures.

List of XSLT Functions with Syntax

Here we will see about various functions used in XSLT. Functions can be called from the location where easy XPATH expressions are provided. Functions can be user-defined and inbuilt. A well-designed function should have return types and parameters and also should have consistent names across multiple functions.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The below-given list is the Built-in functions of XSLT:

1. current()

The XSLT function returns the element that contains the current node. This function is necessary for accessing the current node when it is not the same as the context node provided in the code because both the nodes are the same.

Syntax:

<xsl: value-of >=Current()
// This function doesn’t take any argument

Example:

Code:

XML:

<?xml version = "1.0" encoding = "utf-8"?>
<?xml-stylesheet type = "text/xsl" href = "xslff.1.xsl" ?>
<airfly>
<aeroplane>
<year> 1965 </year>
<manufacturer> Boeing </manufacturer>
<modelname> Bk1234 </modelname>
<colortype> Light red and sandal </colortype>
</aeroplane>
<aeroplane>
<year> 1977 </year>
<manufacturer> Bizliner </manufacturer>
<modelname> K567 </modelname>
<colortype> Light red and sandal </colortype>
</aeroplane>
<aeroplane>
<year> 1966 </year>
<manufacturer> Dassault Falcon </manufacturer>
<modelname> DF345 </modelname>
<colortype> Light blue and grey </colortype>
</aeroplane>
<aeroplane>
<year> 1980 </year>
<manufacturer> Bombardiere</manufacturer>
<modelname> B8999 </modelname>
<colortype> Plain White and brown </colortype>
</aeroplane>
</airfly>

XSLT:

<?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>
<xsl:for-each select="airfly/aeroplane/manufacturer">
Current node: <xsl:value-of select="current()"/>
<br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Explanation:

  • The above example shows how an individual element is processed by xsl:for-each temporarily by the current node.

Output:

XSLT Functions 1

2. format_number()

This function converts a given input number into a string. This acts together with the decimal format. For instance, let’s say the number 89067 is an input value, and the second argument is the pattern.

Syntax:

string format-number(number,format,[decimalformat])

3. function_available()

This boolean function checks whether the processor supports the function specified inside this function.

Syntax:

Boolean function_available( sum/add… a string);

Example:

Code:

XML:

Code same as Current function.

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:choose>
<xsl:when test="function-available('factorial')">
<p>factorial() is supported in XLST.</p>
</xsl:when>
<xsl:otherwise>
<p>factorial() is not supportedin XSLT.</p>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="function-available('boolean')">
<p>boolean() is supported.</p>
</xsl:when>
<xsl:otherwise>
<p>current() is not supported.</p>
</xsl:otherwise>
</xsl:choose>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Explanation:

  • The above code returns a boolean value according to the availability of the function specified inside the nodes. So here, factorial() built-in functions; therefore, it prints the result as not supported.

Output:

XSLT Functions

4. element-available()

This function checks the available of the element that supports it. This function uses few elements inside the templates: xsl:chose,xsl:copy,xsl:attributes, xsl:value-of and so on.

Syntax:

Element-available(function string;)

Example:

Code:

XML:

Code same as current function.

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:choose>
<xsl:when test="element-available('xsl:if')">
<p>xsl:element is supported in processor.</p>
</xsl:when>
<xsl:otherwise>
<p>xsl:Element is not supported.</p>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="element-available('xsl:function')">
<p>xsl:function is not supported.</p>
</xsl:when>
<xsl:otherwise>
<p>xsl: exit the code.</p>
</xsl:otherwise>
</xsl:choose>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Explanation:

  • The XSLT code searches the available element supported by the XSLT. The test element function is xsl:if, which is listed below the XSLT library.

Output:

element-available()

5. key()

This function returns specific nodes from the document using the element <xsl:key>.

Syntax:

<xsl:for-each> key(string , value)

Code:

XML:

<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "students.xsl"?>
<Pharmacy>
<pharma idno = "693">
<itemname>Abacavir</itemname>
<itemprice>500</itemprice>
<netqty>2000</netqty>
<itemdiscount>15</itemdiscount>
</pharma>
<pharma idno = "852">
<itemname>Calpol</itemname>
<itemprice>900</itemprice>
<netqty>6000</netqty>
<itemdiscount>10</itemdiscount>
</pharma>
<pharma idno = "741">
<itemname>Pamelor</itemname>
<itemprice>150</itemprice>
<netqty>500</netqty>
<itemdiscount>5</itemdiscount>
</pharma>
</Pharmacy>

XSL:

<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:key name = "itemname-find" match = "pharma" use = "itemname"/>
<xsl:template match = "/">
<html>
<body>
<h2>Pharma details</h2>
<table border = "2">
<tr bgcolor = "brown">
<th>ID No</th>
<th>Item Name</th>
<th>ItemPrice</th>
<th>Net Qty</th>
<th>Item Discount</th>
</tr>
<xsl:for-each select = "key('itemname-find', 'Calpol')">
<tr>
<td><xsl:value-of select = "@idno"/></td>
<td><xsl:value-of select = "itemprize"/></td>
<td><xsl:value-of select = "netqty"/></td>
<td><xsl:value-of select = "itemdiscount"/></td>
<td><xsl:value-of select = "itemname"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Explanation:

  • Next, here the code is to find the item name through the key value. Here we have assigned an item name as Calpol to search the node, and the result is here.

Output:

specific nodes from the document

6. format-date ()

Format date is used much in web development frequently for manipulation. The format date on XSLT Technique uses the Time Standard format. The format-date function uses picture strings with the Specifiers like Y for Year, M-month in a year, D for Day in a week.

Syntax:

<xsl:attribute name="feed>
<xsl:value-of select =" format-date(function name) (inpvalue, 'MMM dd, yyyy')"/>
<xsl:attribute>

7. substring-before()

This function provides a section of a string to be printed in the result. The substring -before returns the value before the subpart.

Syntax:

Substring-before('free - dom' ,'-') // this returns a value before the hypen .so it is 'free'.

Example:

Code:

st.xml:

<?xml version="1.0"?>
<spensstore>
<store id="1">
<pname>cosmetics</pname>
<brand>MAC</brand>
<price>30.50</price>
</store>
<store id="2">
<pname>shampoo</pname>
<brand>Dove</brand>
<price>50.50</price>
</store>
<store id="3">
<pname>eatables</pname>
<brand>babyjunk</brand>
<price>41.50</price>
</store>
</spensstore>

ps.xsl:

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:for-each select="//store">
<xsl:value-of select="substring-before(pname, 'bles')" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Explanation:

  • The above XSL file took an output method as HTML and followed by the select option in which the ‘before’ keyword truncates the substring part of the string. Here the element pname is selected for the selection by which the value ‘bles’ is divided by the before-option.

Output:

section of a string to be printed in the result

Conclusion

Therefore, in this article, we saw about XSLT functions; we also saw how to process this function and how it works in XSLT. We also showed the implementation of various functions individually. XSLT being in a unique way useful in many projects as they adapt themselves according to the environment.

Recommended Articles

This is a guide to XSLT Functions. Here we discuss the introduction and list of XSLT functions with syntax for better understanding. You may also have a look at the following articles to learn more –

  1. XML Schema
  2. XML Mapping
  3. XML ampersand
  4. XML XSD
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more