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

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

Introduction

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.

Syntax

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.

Example

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"?>

<college>

  <Student>

    <Name>Richa garg</Name>

    <address>Delhi</address>

  </Student>

 

  <Student>

    <Name>Megha garg</Name>

    <address>Noida</address>

  </Student>

</college>

 

Code for XSLTFile.xslt

 

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

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

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

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

  <xsl:template match="/">

    <html>

      <head>

        <title>example</title>

      </head>

      <body>

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

      </body>

    </html>

  </xsl:template>

  <xsl:template match="Student">

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

      <DIV>

        <B>

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

        </B> lives in

        <I>

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

        </I>

      </DIV>

    </xsl:if>

  </xsl:template>

</xsl:stylesheet>

 

Output
 

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

contains.jpg

Ask Your Question 

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

Programming Answers here

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.