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 string length
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 string length

XSLT string length

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:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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) &gt; 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) &gt; 1">...</xsl:if>
<xsl:if test="string-length(@val) &gt; 1">...</xsl:if>

A detailed test case is here

<xsl:if test="@information">
<xsl:if test="string-length(@value) &gt; 1 and
count(information|value) &gt; 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:

nwe content

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:

XSLT String length 1

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:

XSLT String length 2

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) &gt; 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:

XSLT String length 3

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 5

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(.) &gt; 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:

example 6

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 –

  1. XML DOM
  2. XSLT count
  3. XSLT substring-after
  4. XSLT replace
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