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

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

Introduction

String function is used to convert the object to the string. The types of conversion from object to string are:

  • Converting node-sets to string
  • Converting numbers to string
  • Converting Booleans to string
  • Converting objects to string

Syntax

string string (object?)

Converting node-sets to string : The string() function converts a node-set to a string by returning the string value of the first node in the node-set, for example if we write the node-set like, <ColorName>

<ColorName>
   <color>Red</color>
   <color>Blue</color>
   <color>Green</color>
   <color>White</color>
</ColorName>

Now the following function we can perform:

  • The following string function return the first node that is "Red"
    string(//ColorName())
     
  • If I want to display all the color name together means the concatenation of all the child element of the ColorName node then I write as,
    string(.)
    Output is as "RedBlueGreenWhite"
     
  • We can also apply the other function within the string function like,
     contains(//ColorName(),"Green")
    This function return false as the string(//ColorName()) gives the first element node that is Red.
     
  •  To search the other color name we write as,
    contains( . ,  "Green")
    Now it will return true as string(.) convert the string as "RedBlueGreenWhite" which contain the element Green.
     

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.