How to use contains operation in String Function Using XPath in Xml

In this article I will explain contains operation of string function.
  • 2421


Contains operation/function is a part of string function in XPath. This function takes two string argument and check whether the first string argument contains the second string argument or not. If the first string contains the second string within it then this function returns true otherwise return false.


boolean contains(string1,string2)

Hear contains is a function that takes two string argument to check whether the first string contain the second string or not if yes then return a Boolean value true otherwise return false.


The following example shows how we use the contains function,In the following example the string name contain garg so that it return the output

Code for XMLFile.xml

<?xml version="1.0" encoding="utf-8" ?>

<?xml-stylesheet type="text/xsl" href="XSLTFile.xslt"?>



    <Name>Richa garg</Name>





    <Name>Megha garg</Name>





Code for XSLTFile.xslt


<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl=""

    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">

  <xsl:output method="html" omit-xml-declaration="yes"/>

  <xsl:template match="/">






        <xsl:apply-templates select="//Student"/>




  <xsl:template match="Student">

    <xsl:if test="contains(Name, 'garg')">



          <xsl:value-of select="Name"/>

        </B> lives in


          <xsl:value-of select="address"/>








When applied to the XML file (XMLFile.xml), the XSLT style sheet above produces the following output:


Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here


More Articles

© 2020 DotNetHeaven. All rights reserved.