Updated April 19, 2023
Definition of XSLT Concat Function
Concat function in XSLT helps to concatenate two strings into single strings. The function Concat() takes any arguments that are not the string is been converted to strings. The cardinality function passed over here in each argument should satisfy zero or one function() respectively. We can pass as many strings as possible. Concat function by itself concatenates the output in the text form.
The following section illustrates the use of this function and its working.
Syntax:
The simplest Signature of Concat format is given as follows:
fn:concat(string1 , string2);
Here the parameters two values which is a string are done at the end of the other string. The variables used here are atomic type and the values are been converted to strings. And finally, the combination of two strings is returned.
How Concat function works in XSLT?
XSLT with the help of XPATH expression has a handy set of functions to go with their templates. Making use of the strings variables helps in permitting advanced translations. The Concat function takes two input strings as a source and concatenates them defined in XPATH. Below we shall see the arguments it returns and it is very easy to accomplish with XSLT.
concat(‘x’) – Gives out error as it got one single string value.
Concatenation of two strings
Concat(‘x’,’ y’) – This returns xy.
Passing several strings
concat((‘x’,’ y’ , ‘z’)) – This returns an error.
Empty Strings with several strings
fn:concat( ‘Yes’, ‘this ‘, ‘is so’, (), “,”, “genuine”,) – This returns the string ‘Yes this is so genuine.
Let’s see the easiest method to define concatenation.
The XML structure goes like this:
<Theme>The Butterfly Scenario</Theme>
<Publisher>
<first-name>Henry</first-name>
<last-name>Harvard Mecni</last-name>
</Publisher>
<price>13.99</price>
When the above transformation is applied on the XML document the XSLT code is given below
TD>
<xsl:value-of select="concat(//author/first-name,' ',//author /last-name)"/>
</TD>
To concatenate a sequence of string values we can use fn:string-join function.
Adding White Space character elements
<xsl:value-of select="fn:concat(EName,' ', MName)" />
Using param name we can declare the concatenation
<xsl:param name="Concatvar">
<xsl:for-each select=" first-name/author "><!--<xsl:if test=" first-name/author ">-->
<xsl:value-of select="concat($Concatvar, first-name/author/text())"/>
<!--</xsl:if>-->
</xsl:for-each>
When Sequence is done as arguments it results in an error. When XPATH 1.0 is activated it overtakes the arguments. Example:
Fn:concat ((‘the’ ,’the’) ,(‘ of’ ,’of’))
If the XPATH is activated it results in ‘ theof ‘.
Now, let’s take a scenario where we require an N number of strings to pass into the transaction process of UserID. Passing a string to this transaction process each time is not a good format. Therefore we Opt for Concat to finalise the Strings. Concatenation using while Loop is not preferred when Strings are more. To make it handy we have to write an XSLT template as defined in the following section.
Examples
Here we will look at an example to see how it works in real-time Practice.
Example #1: Using two Strings
Given the following XML source first and we are asked to provide the concatenate output below:
pp.xml
<?xml version = "1.0"?>
<warehouses>
<warehouse place = "google">
<wname>Type Corporation</wname>
</warehouse>
<warehouse place = "IBM">
<wname>RBL Corporation, Inc.</wname>
</warehouse>
</warehouses>
pp. xsl
<?xml version = "1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/warehouses">
<html>
<body>
<ul>
<xsl:for-each select="warehouse">
<xsl:if test="starts-with(@place, 'g')">
<li>
<xsl:value-of select="concat(@place,' - ', wname)" />
</li>
</xsl:if>
</xsl:for-each>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Explanation
As we can see in the Output below, the result of the two expressions under the select statement is concatenated producing the output as :
Output:
Example #2
XML sheet source code is given below.
bill.xml
<?xml version="1.0" encoding="utf-8"?>
<bill.ref.no.book>
<bill.ref.no>
<invoice>RAM Requirement</invoice>
<number>X-900-677</number>
<year>2008</year>
</bill.ref.no>
<bill.ref.no>
<prefix>Database Doc</prefix>
<number>T-988-900</number>
<year>2010</year>
</bill.ref.no>
</bill.ref.no.book>
bill. xsl
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="bill.ref.no.book">
<xsl:variable name="bil">
<section class="demo2">
<xsl:text disable-output-escaping="yes">RAM Requirement</xsl:text>
</section>
</xsl:variable>
<xsl:variable name="exam">
<xsl:value-of select="./bill.ref.no/invoice"/>
</xsl:variable>
<xsl:variable name="loop">
<xsl:value-of select="./bill.ref.no/number"/>
<xsl:if test="following::bill.ref.no/number">;</xsl:if>
</xsl:variable>
<xsl:variable name="year">
<xsl:value-of select="./bill.ref.no/year"/>
</xsl:variable>
<div class="new">
<xsl:value-of select="concat($bil,' – ',$exam,' Nos. ',$loop,'-',$year)"/>
</div>
</xsl:template>
</xsl:stylesheet>
Explanation
In the above code snippet, the Concat method uses four strings. So here I have highlighted how to use Concat type in XSLT. And we had assigned multiple elements in the XML input with the class type. And XSL sheet would contain if-else and sometimes loop functions as the conversion source type.
Output:
Example #3
Here an input of XML goes like this for a payment
trr.xml
<Online_details>
<Payment>
<Type>
<Bank>9F01</Bank>
<ac_no>9F01020001</ac_no>
</Type>
<Type>
<Bank>American Express</Bank>
<ac_no>3152864856</ac_no>
</Type>
<Type>
<Bank>Swiss </Bank>
<ac_no>78456690</ac_no>
</Type>
<Type>
<Bank>IDBI</Bank>
<ac_no>6369568844</ac_no>
</Type>
<Type>
<Bank>Norse Dein</Bank>
<ac_no>3172000458</ac_no>
</Type>
<Type>
<Bank>Korean</Bank>
<ac_no>8541452224</ac_no>
</Type>
<Type>
<Bank>Tavael Blizz</Bank>
<ac_no>858102000</ac_no>
</Type>
<Type>
<Bank>jjjjj</Bank>
<ac_no>541114741</ac_no>
</Type>
</Payment>
</Online_details>
And the Corresponding template would be like this
trr.xslt
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:for-each select="Online_details/Payment">
<xsl:variable name="Type61">
<xsl:choose>
<xsl:when test="Type[ac_no='61']">
<xsl:value-of select="concat('Bank', ' ', Type[ac_no='61']/tag_value)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('ac_no', ' ', 'No-Data')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="Type9F01">
<xsl:choose>
<xsl:when test="Type[ac_no='9F01']">
<xsl:value-of select="concat('Tag9F01', ' ', Type[ac_no='9F01']/tag_value)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('TypeF01', ' ', 'No-Data')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="Type81">
<xsl:choose>
<xsl:when test="Type[ac_no='81']">
<xsl:value-of select="concat('Type81', ' ', Type[ac_no='81']/tag_value)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('Type81', ' ', 'No-Data')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="Type4F">
<xsl:choose>
<xsl:when test="Type[ac_no='64856']">
<xsl:value-of select="concat('6690', ' ', Type[ac_no='4F']/tag_value)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('636', ' ', 'No-Data')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="concat($Type61, ' ', $Type9F01, ' ', $Type81, ' ', $Type4F)"/>
</xsl:for-each>
</xsl:stylesheet>
Explanation
It takes a type element with the account no element to concatenate.
Output:
Example #4
<?xml version="1.0" encoding="UTF-8"?>
<karoake>
<stanza>statement 1</stanza>
<stanza>statement 2</stanza>
<stanza>statement 3</stanza>
<Music>yyyy</Music>
</karoake>
yyy.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="karoake">
<xsl:value-of
select="concat(statement[1], ' ',
statement[2], ' ',
' ',
statement[3], ' ',
' ',
statement[4], ' ',
statement[5], ' ',
' -',
Music)" />
</xsl:template>
</xsl:stylesheet>
Explanation
Here the concat() takes statement 1 and statement2 and combines them.
Output:
Example #5
bll.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl"?><Harvester>
<field>
<groups>3</groups>
<Place>Fikaro</Place>
<longitude>5855</longitude>
<Farmers>2</Farmers>
<co-workers>52</co-workers>
</field>
<field1>
<data1>harry</data1>
<data2>Fiest</data2>
</field1>
</Harvester>
gok.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Place">
<Latitude><xsl:value-of select="co-workers"/></Latitude>
<Combine>
<xsl:apply-templates />
<xsl:apply-templates select="../co-workers2/*" />
</Combine>
</xsl:template>
<xsl:template match="co-workers2" />
</xsl:stylesheet>
Explanation
Here the template is used to copy the specified node and the Concat() operates on the specified text.
Output:
Conclusion
Therefore we have accomplished the task of String concatenation to a value declared using names. This is used in various application using XPATH expressions.
Recommended Articles
This is a guide to XSLT Concat. Here we discuss the Introduction, How Concat function works in XSLT? and examples with code implementation. You may also have a look at the following articles to learn more –