Updated April 10, 2023
Definition of XSLT string length
XSLT string length is defined as a string function and takes a single string argument and returns an integer value representing the number of characters in the string. It converts any declared type into a string except that an empty parenthesis cannot be converted. The whitespaces are taken into the count. To define a variable without whitespaces we could use normalize-space().
Syntax:
Here is the syntax of string-length() function
String-length ([string val])
The Details of the parameters is a string variable that is to be evaluated and returns an integer value.
Let’s get started with how to generate a length of a String along with the advantages in detail.
How to find string length in XSLT?
This function is described for manipulating a String value. If any argument is not passed to a string length() function they evaluate the present context node. Generally, to find the no. of characters in a variable using XSL we could use this and it is interpreted as String. Also, that spaces are part of the content of the node and also part of the string we are manipulating the length of.
For instance, there is a variable ‘gross’ to evaluate the expression like gross=15, the equivalent if-else structure in XSL adding the string length variable is given as:
<xsl:choose>
<xsl:when test="string-length($gross) = 15">
//code here to check the condition true
</xsl:when>
<xsl:otherwise>
// false statement
</xsl:otherwise>
</xsl:choose>
If the input String is the bus the function returns 3.
Let’s see here how the string length is calculated
string-length(‘world’) | 5 |
string-length(‘ world ‘) | 9 |
string-length(normalize-space(‘ world’)) | 5 |
string-length(‘Hey world ‘) | 9 |
string-length(”) | 0 |
–string-length(()) | 0 |
The “.” in the String length () function states that it is the current node of any variable or loop statement declared. For example
<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<xsl:for-each select="article/Introduction/line">
<p>
paragraph <xsl:value-of select="@num" /> is <xsl:value-of select="string-length(.)" /> characters length is .
</p>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The above template gives the result as Paragraph is ‘x no’ characters length with the repeated iteration.
Next with the choose element the string length is assigned with test attribute. Let’s see how it is declared
<xsl:choose>
<xsl:when test="string-length(@location) > 0">
//create element or attribute here
<xsl:element name="xsd:element">
<xsl:attribute name="location">
<xsl:value-of select="@location"/>
</xsl:attribute>
With If Statement the function is added up with the test case in two ways
<xsl:if test="string-length(content) > 1">...</xsl:if>
<xsl:if test="string-length(@val) > 1">...</xsl:if>
A detailed test case is here
<xsl:if test="@information">
<xsl:if test="string-length(@value) > 1 and
count(information|value) > 1">
<xsl:text>|</xsl:text>
</xsl:if>
<xsl:text>(</xsl:text>
<xsl:value-of select="@information"/>
<xsl:text>)</xsl:text>
</xsl:if>
With the character string, each occurrence of characters is counted as one including an empty string. In the contrast null string returns ‘zero’.
Examples
In the upcoming part, we will be showing a string-length function with different examples to display the number of characters in a line as a result. To show 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
Following Is my XML file
<?xml version="1.0" encoding="UTF-8"?>
<body>
<h1>A new Content is been Released</h1>
<p>The following Sentence is been added</p>
<Thesis>
<Section> Thesis says Reader about the content of the topic</Section>
<Section>
Introduction 2
</Section>
</Thesis>
</body>
XSL Transformation is
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
<xsl:template match="Section">
<xsl:value-of select="concat('length: ',string-length(.))" />
<xsl:if test="contains(.,'Thesis')">
<xsl:text>Thesis: yes!</xsl:text>
</xsl:if>
<xsl:if test="starts-with(.,'Goes')">
<xsl:text> Yes, starts with "Goes"</xsl:text>
</xsl:if>
<xsl:value-of select="normalize-space(.)" />
<xsl:value-of select="translate(.,'mnop','MNOP')" />
</xsl:template>
</xsl:stylesheet>
Explanation
The above stylesheet calculates a String length for the element <section> that counts the number of characters in a line. And it is ‘50’. Next, the translate function changes the characters into Upper case and the result is given as:
Output:
Example #2
Following Is my XML file
<?xml version="1.0" encoding="UTF-8"?>
<vegetables>
<colorveg>beetroot pinkish red</colorveg>
<colorveg>cabbage pale green</colorveg>
<colorveg>broccoli light green</colorveg>
</vegetables>
XSL Transformation is
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output omit-xml-declaration="yes" />
<xsl:variable name="fieldWidth">12</xsl:variable>
<xsl:template match="colorveg">
<xsl:value-of select="concat('length: ',string-length(.))" />
<xsl:variable name="padding" select="$fieldWidth" />
<xsl:value-of select="substring(' ',2,$padding)" />
<xsl:value-of select="." />
</xsl:template>
</xsl:stylesheet>
Explanation
Here the string length is manipulated for all the <vegetable> elements with the value-of statement. And the result is here
Output:
Example #3
Following Is my XML file
<?xml version="1.0" encoding="UTF-8"?>
<Math>
<a>6.1</a>
<b>5.2</b>
<c>12</c>
<d>sum</d>
</Math>
XSL Transformation is
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" />
<xsl:template match="Math">
1.2 + string-length("1.2") =
<xsl:value-of select="c + string-length(c)" />
</xsl:template>
</xsl:stylesheet>
Explanation
The above code performs math calculation by summing the input value to the calculated string Length.
Output:
Example #4
Following Is my XML file
<?xml version="1.0" encoding="UTF-8"?>
<Cosmetics >
<Categories cc = "01" >
<skin>Toners </skin>
<skin>Mists</skin>
<skin>Night Cream </skin>
</Categories>
<Categories cc = "02" >
<Personalcare>Rollers and Curlers </Personalcare>
<Personalcare>Hairfall and thining </Personalcare>
<Personalcare>Lotions and creams </Personalcare>
</Categories>
<Categories cc = "03" >
<Makeup>Face Primer </Makeup>
<Makeup>Highlighters </Makeup>
<Makeup>Contour </Makeup>
</Categories>
</Cosmetics>
XSL Transformation is
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" />
<xsl:template match="Cosmetics">
<xsl:choose>
<xsl:when test="string-length(@skin) > 10">
<xsl:value-of select="substring(skin, string-length(@skin) - 9)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="skin"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Explanation
The above code uses stylesheet to truncate the string by setting the maximum length / greater than.
Output:
Example #5
Following Is my XML file
<?xml version="1.0"?>
<store>
<store id="11">
<pname>cosmeticstype</pname>
<brand>LAKME</brand>
<price>20.50</price>
</store>
<store id="12">
<pname>Electronics</pname>
<brand>Phone</brand>
<price>50.50</price>
</store>
<store id="13">
<pname>eatables</pname>
<brand>Ghee</brand>
<price>41.50</price>
</store>
</store>
XSL Transformation is
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
<xsl:template match="store">
<xsl:value-of select="concat('length: ',string-length(.))" />
<xsl:if test="contains(.,'pname')">
<xsl:text>Thesis: yes!</xsl:text>
</xsl:if>
<xsl:if test="starts-with(.,'Goes')">
<xsl:text> Yes, starts with "Goes"</xsl:text>
</xsl:if>
<xsl:value-of select="normalize-space()" />
<xsl:value-of select="translate(.,'mnop','MNOP')" />
</xsl:template>
</xsl:stylesheet>
Explanation
Here the if-else test is evaluated for the XML code and the string length is calculated for the respective element.
Output:
Example #6
Following Is my XML file
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="c:\text.xsl"?>
<root>
<text>
hihelloeverybodyghnxlxlkbvvvvthisismacjohn
</text>
<Title>Animals</Title>
</root>
XSL Transformation is
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="text">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:choose>
<xsl:when test="string-length(.) > 10">
<xsl:value-of select="concat(substring(., 1, 10), '...')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:transform>
Explanation
The above code has the very long text content and the XSL evaluates the variable string length greater than 10. Here the characters are restricted to a particular maximum length value.
Output:
Conclusion
In this article, we had learned how to find the length of the string in XSLT. We have used a different example to show how the length is manipulated with different XSLT cases.
Recommended Articles
This is a guide to XSLT string length. Here we discuss the definition, syntax, How to find string length in XSLT? examples with code implementation. You may also have a look at the following articles to learn more –