Substring function in xslt

Substring function returns the first argument starting at the position specified in the second argument and the length specified in the third argument.
string substring(string, number, number)

Example – 

The following function call returns “456”:
substring(“34567”,2,3)
The following function call returns “234”:
substring(“1234”,2)
The following function returns “sheet”:
substring(“harsheet”,4)

Substring-after

substring-after(string,string)
The substring-after function returns the substring of the first argument string that follows the first occurrence of the second argument string in the first argument string, or the empty string if the first argument string does not contain the second argument string.
The following function call returns “99/04/01”:
substring-after("1999/04/01","19")

Substring-before

substring-before(string, string)

Returns the substring of the first argument string that precedes the first occurrence of the second argument string in the first argument string, or the empty string if the first argument string does not contain the second argument string.

The following function call returns “1999”:

substring-before("1999/04/01","/") 
Advertisements

Elements in XSLT

XSLT is all about selecting one or more nodes from XML document and transforming or replacing its content with something else. A node could be any of the following: elements, attributes, text, namespaces, processing-instructions, and comments.
The <xsl:template> element is to select a node from XML document and transform its contents.

To select an element, you use the match attribute. To transform its contents, you simply place the new content between the opening (<xsl:template>) and closing (</xsl:template>) tags.

Example

<FoodItem>
    <Item1>Juice</Item1>
    <Item2>Milk</Item2>
</FoodItem>

In this case, I’m selecting the root node (i.e. FoodItem). By selecting this node, the template element tells the XSLT processor how to transform the output. The processor will replace the root node (i.e. the whole XML document) with what is being written between the <xsl:template> tags.
In this case, the contents of an HTML document are written inside the tags. When a user views any XML document that uses this XSL document, they will simply see the line “New item…”.

<xsl:template match="FoodItem">
  <html> 
    <body>
      <p>New item...</p>
    </body>
  </html>
</xsl:template>

Root Node Selection

In the example above, we selected the “FoodItem” node which happens to be the root node of our XML document. Another way of selecting the root node is to use a forward slash in place of the node’s name. The following example results in the same output as the above example.

Example:

<xsl:template match="/">
  <html>
    <body>
      <p>New item...</p>
    </body>
  </html>
</xsl:template>

 

How to get string length

XML to be used for our example:

<FoodItems>
    <Item>
       <Name>Juice</Name>
    </Item>
    <Item>
        <Name>Bread</Name>
    </Item>
    <Item>
        <Name>Eggs</Name>
    </Item>
<FoodItems>

Now use this xml in xslt as

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>

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

 

Output 

FoodItem String Length
Juice 5
Bread 5
Eggs 4

Create an array in xslt

You can create any data type model you want to in xslt.

To create an array, just create a variable for it.

Example:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" omit-xml-declaration="yes"/>
 
    <xsl:variable name="array" as="element()*">
        <Item>A</Item>
        <Item>B</Item>
        <Item>C</Item>
    </xsl:variable>
 
    <xsl:template match="/">
        <xsl:value-of select="$array[2]"/>
    </xsl:template>
 
</xsl:stylesheet>

Output

B

Sort XML by date using xslt

For this, we just use the simple xml.

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="style.xslt"?>
 
<birthdays>
  <item>
    <date>13.05.2005</date>
    <title>John</title>
  </item>
  <item>
    <date>06.02.2000</date>
    <title>Steve</title>
  </item>
  <item>
    <date>13.01.2007</date>
    <title>Lisa</title>
  </item>
  <item>
    <date>29.05.2010</date>
    <title>Sarah</title>
  </item>
</birthdays>

And the xslt code would be something like this

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
    
   <xsl:template match="birthdays">
    <h1>Birthdays</h1>
    <ul>
      <xsl:for-each select="item">
        <xsl:sort 
           select="normalize-space(substring(date,7,4))" 
           order="descending" />
        <xsl:sort 
           select="normalize-space(substring(date,4,2))" 
           order="descending" />
        <xsl:sort 
           select="normalize-space(substring(date,0,2))" 
           order="descending" />
        <li>
          <xsl:value-of select="date" />
          <br />
          <xsl:value-of select="title" />
        </li>
      </xsl:for-each>
    </ul>
    </xsl:template>
</xsl:stylesheet>

Output

Birthdays

  • 29.05.2010
    Sarah
  • 13.01.2007
    Lisa
  • 13.05.2005
    John
  • 06.02.2000
    Steve

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 :

xml

 

And here is the code for xslt –

xslt