How to get string length

XML to be used for our example:


Now use this xml in xslt as

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="">

<xsl:template match="/">

<xsl:template match="FoodItems">
  <table border="1" style="background-color:#cccc00">
      <th>Food Item</th>
      <th>String Length</th>
    <xsl:for-each select="FoodItems">  
      <td><xsl:value-of select="Name"/></td>
      <td><xsl:value-of select="string-length(Name)"/></td>



FoodItem String Length
Juice 5
Bread 5
Eggs 4

How to access a variable from within xsl:for-each from outside xsl:for-each

You must have used variables in xslt, like how easy it is to use them anywhere.

In the same way you can also wrap xsl: for-each with an xsl: variable.

For example, the following stylesheet declares a variable EmployeeName and within the xsl: for-each it uses xsl: value-of and xsl: text.

All of the text values are assigned to the variable EmployeeName, which is used outside of the xsl: for-each to select it’s value.

We will use the following xml, for example :



And here is the code for xslt –