Updated April 18, 2023
Definition of XSLT substring-after Function
The substring-after function returns a part out of the string declared in the string argument that is specified after the substring. If a second string is empty it returns an empty string as a result. String functions determine the manipulation of strings of text. The substring function helps in returning an atomic value not simple a node.
Syntax:
string substring-after(
string
string
)
The above function takes two inputs where the first string does search and the second string is the string to be searched for the first string.
The next sections explore these functions with few representations along with the advantages in detail.
How substring-after function works in XSLT?
Substring-after takes two string values and returns a string value that is next to a given string after a specified sub-string. In other words, a part of the string is returned.
Let’s see few examples on substring-after. It demonstrates how to extract a month and date in the document.
substring-after('14-March-1990', '-') = 'March-1990'
substring-after('14 March 1990', ' ') = 'March 1990'
substring-after('14 March 1990', ' ') = 'March 1990'
substring-after('14 March 1990', '-') = ''
substring-after('mm-nn','-') = nn
substring-after(‘mm-nn','m') = m-nn
substring-after(' mm-nn'','n') = n
substring-after(' mm-nn','d') = return null.
Now let’s see another example with dates having special symbols.
substring-after("2004/03/02","/") returns 03/02
substring-after ("2014/03/01","20") returns 14/03/01
In the above sample, the substring-after() is ruling so here a forward slash(/) is passed as a second argument which is used to find the sub-string.
Type Sensitivity is compensated with the Number function added to the sub-string after function in the expression format.
<sounds>
<track> jass </track>
<price>$ 21.33</price>
<tax>$ 10.1</tax>
</sounds>
The corresponding templates are written as
<xsl:template match=”sounds”>
Final Balance :< xsl:value-of select=”substring- after(price,’$) - substring- after(tax,’$)”/>
</xsl:template>
The final output should be 11. But it gives an error as a minus is not accepted as an argument. This could be re-written as
Final Balance :< xsl:value-of select=”number(substring- after(price,’$)) -number( substring- after(tax,’$))”/>
These functions might seem to be self-explanatory and they have a more interesting task in carrying out the expressions.
Examples
In this section, we will be showing a string-after function with different use cases to display the sub-string part as a result. To Give it in real-time concept, we have created an XML document and rules with XSL and the transformation is shown in the XML output method.
Example #1 – XML input format is given as
<Home>
<Transformation>tools/Cookies=Set valid</Transformation>>
<Transformation>Next</Transformation>>
</Home>
Lets have some part of the XSL code here and we shall configure the string based on the XML file.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Home">
<newwindow>
<xsl:apply-templates select="*"/>
</newwindow>
</xsl:template>
<xsl:template match="Transformation">
<newwindow>
<xsl:call-template name="select"/>
</newwindow>
</xsl:template>
<xsl:template name="select">
<xsl:choose>
<xsl:when test="contains(.,'/Cookies')">
<xsl:value-of select="substring-after(.,'/Cookies')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="*"/>
</xsl:stylesheet>
Explanation
The above code is a substring-after function assigned with XML elements. Here we had created two-value -of select statements with the elements Transformations and cookies which displays a string after the first string. So I have mentions the substring cookies. The output takes the string set valid which is the next string of the cookies. And the Output string is shown below.
Output:
Example #2
xml
<?xml version="1.0"?>
<Soft-drinks>
<Type id="10">
<product>Coco-cola classic</product>
<manufacturer> California North </manufacturer>
<Caffeine>60mg</Caffeine>
<price>63.1</price>
</Type>
<Type id="11">
<product>Pepsi doff</product>
<Caffeine>54mg</Caffeine>
<manufacturer>California West </manufacturer>
<price>52.5</price>
</Type>
<Type id="12">
<product>dr Pepper</product>
<Caffeine>42mg</Caffeine>
<manufacturer> Nigeria North </manufacturer>
<price>63.1</price>
</Type>
</Soft-drinks>
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="//Type">
<xsl:value-of select="substring-after(product, 'cola')" />
</xsl:for-each>
<xsl:for-each select="//Type">
<xsl:value-of select="substring-after(manufacturer, 'California')" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Explanation
The above source takes HTML as an output mode and then the template name is given for the value-of select in which the substring part of the string is truncated by the ‘after’ keyword. Here the element specification is searched for the index Type by which the value ‘manufacturer’ with the element is divided by the After-option. This searches the string value after the String California. Therefore, we will have the result as below.
Output:
Example #3
The string can have a value like below first is the XML file
<Song>
<Track1>Mountain Fall </Track1>
<Track2>Sing along </Track2>
</Song>
XSL File
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" >
<xsl:output method = "text" />
<xsl:variable name = "S" >https://educba.com</xsl:variable>
<xsl:template match = "/" >
<xsl:text >
substring-after https : </xsl:text>
<xsl:value-of select = "substring-after($S,'educba.')" />
<xsl:text >
substring-after com. : </xsl:text>
<xsl:value-of select = "substring-after($S,'com.')" />
</xsl:template>
</xsl:stylesheet>
Explanation:
This is similar to the previous example but with the difference of variable name declared here for the String. So, the Substring after function reads the URL declared under the name=S and prints the result of the string after the substring.
Output:
Example #4
XML file
<Generator>
<digits>5 6.2 4 72.6 110 -5 13</digits>
</Generator>
XSL Stylesheet
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/Generator">
<newval>
<xsl:call-template name="calculate">
<xsl:with-param name="val" select="digits" />
<xsl:with-param name="delim" select="' '" />
</xsl:call-template>
</newval>
</xsl:template>
<xsl:template name="calculate">
<xsl:param name="val" />
<xsl:param name="delim" />
<xsl:choose>
<xsl:when test="substring-after($val,$delim) != ''">
<child>
<xsl:value-of select="substring-after($val,$delim)" />
</child>
<xsl:call-template name="calculate">
<xsl:with-param name="val" select="substring-after($val,$delim)" />
<xsl:with-param name="delim" select="$delim" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<child>
<xsl:value-of select="$val" />
</child>
</xsl:otherwise>
</xsl:choose>
</xsl:template></xsl:stylesheet>
Explanation
The following code shows how to use after function() to represent the delimiter with the numbers.
Output:
Following is the next example showing Parenthesis values.
Example #5
XML file
<Essay>
<Introduction>(1)This is sentence paragraph.</Introduction>
<Introduction>(2)This is second sentence 4 paragraph.</Introduction>
</Essay>
XSLT file is given as
<?xml version="1.0"?><xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/*">
<xsl:apply-templates select="Introduction"/>
</xsl:template>
<xsl:template match="Introduction">
<xsl:variable name="Introduction" select="."/>
<xsl:variable name="words"
select="substring-after(substring-before($Introduction, ')'), '(')"/>
<xsl:variable name="Text" select="substring-after( $Introduction , ')')"/>
<line>
<count>
<xsl:value-of select="$words"/>
</count>
<text>
<xsl:value-of select="$Text"/>
</text>
</line>
</xsl:template>
</xsl:stylesheet>
Explanation
The following code will result in the below output. Here I have used substring after function for the purpose to separate a number within the parenthesis as declared in an XML document.
Output:
Advantages
- They have more support for Regular Expressions.
- Uses select attributes of xsl:value-of. The substring function finds the first string to be a String.
- XSL Sub-string is type Sensitivity for example with the minus sign.
Conclusion
Therefore, this article provides descriptions and implementations of Substring-after Functions and these can be applied in many WebLogic applications. Well, we had learned about sample work that can be used to create expressions for XSLT stylesheets.
Recommended Articles
This is a guide to XSLT substring-after. Here we discuss the Introduction, How substring-after function works in XSLT? examples with code implementation. You may also have a look at the following articles to learn more –