XPath - String Functions Examples

admin's picture
The part of XML document that we will use for the following examples is:
 
<Data>
         <PrimaryDocument SCIObjectID="serverName:6546ef:f8ca28794b:-3ae6"/>
         <username>Joe</username>
..........
<firstname>Joe</firstname>
<lastname>User</lastname>
<fullname>Joe User</fullname>
<email>[email protected]</email>
..........
 
1.  concat(//firstname/text(),'_',//lastname/text())
 
Concatenate content of firstname element, string '_' and content of lastname element.
 
Result: Joe_User
 
2.  starts-with(/*/*[position()=2]/text(),'J')
 
Check if the content of the child of the root element, at the second position, starts with the letter 'J'.
 
Result: true
 
3. starts-with(//username,'J')
 
Check if the content of username element starts with the letter 'J'.
 
Result: true
 
4.  starts-with(//username,'o')
 
Check if the content of username element starts with the letter 'o'.
 
Result: false
 
5.  contains(//username,'o')
 
Check if the content of username element contains the letter 'o'.
 
Result: true
 
6.  substring-before(//email,'@')
 
Select a substring of email element, returns start of email element before string '@' that occurs in it
 
Result: Joe.User
 
7.  substring-after(//email,'@')
 
Select a substring of email element, returns remainder of email element after string '@' that occurs in it
 
Result: gmail.com
 
8.  substring(//email,9,1)
 
Select a substring starting at the position 9 with length 1.
 
Result: @
 
9.  string-length(//email)
 
Return the number of characters in the string that is content of email element.
 
Result: 8
 
10.                   translate(//email,'@','_')
 
Translates the character '@' with '_' in the element email.
 
Result: Joe.User_gmail.com