How to use starts-with operation in String Function Using XPath in Xml

In this article I will explain starts-with operation of string function.
  • 2361

Introduction

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

Syntax

boolean starts-with(string1,string2)

Hear starts-with is a function that takes two string argument to check whether the first string is starts with 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 starts-with function,

Code for starts_with.xml

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

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

<college>

  <Student>

    <Name>Richa</Name>

    <address>Delhi</address>

  </Student> 

  <Student>

    <Name>Megha garg</Name>

    <address>Noida</address>

  </Student> 

  <Student>

    <Name>Reena</Name>

    <address>Noida</address>

  </Student> 

  <Student>

    <Name>Ravi</Name>

    <address>Gaziyabad</address>

  </Student>

</college>

 

Code for starts.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="starts-with(Name, 'R')">

      <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 (starts_with.xml), the XSLT style sheet above produces the following output:


starts_with.jpg

Ask Your Question 

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

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.