Searching for Strings

This section provides information about searching for strings. This section discusses the following topics:

Finding Identical Strings

In a document, you can search for text that is an exact match with what you specify in your query. For example, consider the following query:

//name [ . ="Lu" ] 

This query finds all name elements that contain only the text Lu. It would return elements like these:

<name>Lu</name>  
<name>  
<firstname>Lu</firstname>  
</name>

The same query does not return elements like these:

<name>Lu Chen</name> 
<name> 
<firstname>Lu</firstname> 
<lastname>Chen</lastname> 
</name>

The XPath processor does not return the first name element because the comparison is between "Lu" and "Lu Chen". The query does not return the second name element because the XPath processor concatenates the two strings "Lu" and "Chen" before it makes the evaluation. Consequently, the comparison is between "Lu" and "LuChen". Note that the XPath processor does not insert a space between text nodes that it concatenates.

Case Sensitivity

Searches are case sensitive. A search for "Lu" does not return "lu".

Finding Strings That Contain Strings You Specify

To obtain elements that contain a particular string, call the contains() function. The format is

boolean contains(string, string)  

The contains() function returns true if the first argument string contains the second argument string, and otherwise returns false. For example, the following query returns all books that have a title that contains the string "Trenton":

/bookstore/book[contains(title, "Trenton")] 

When the first argument is a node list, the XPath processor tests only the string value of the node in the node list that is first in document order. Any subsequent nodes are ignored.

Finding Substrings That Appear Before Strings You Specify

To obtain a substring that appears before a string you specify, call the substring-before() function. The format is

string substring-before(string, string)  

The substring-before() function returns the substring of the first argument string that precedes the first occurrence of the second argument string in the first argument string. This function returns the empty string if the first argument string does not contain the second argument string. For example, the following call returns "1999":

substring-before("1999/04/01","/") 

Finding Substrings That Appear After Strings You Specify

To obtain a substring that appears after a string you specify, call the substring-after() function. The format is

string substring-after(string, string)  

The substring-after() function returns the substring of the first argument string that follows the first occurrence of the second argument string in the first argument string. This function returns the empty string if the first argument string does not contain the second argument string. For example, the following call returns "04/01":

substring-after("1999/04/01","/") 

Finding Substrings by Position

To obtain a substring that is in a particular position within its string, call the substring() function. The format is

string substring(string, number, number?)  

The substring() function returns the substring of the first argument, starting at the position specified in the second argument, with length specified in the third argument. For example, the following returns "234":

substring("12345", 2, 3) 

If you do not specify the third argument, the substring() function returns the substring starting at the position specified in the second argument and continuing to the end of the string. For example, the following call returns "2345":

substring("12345", 2)  

More precisely, each character in the string is considered to have a numeric position. The position of the first character is 1. The position of the second character is 2, and so on. The returned substring contains those characters for which the position of the character is greater than or equal to the rounded second argument and, if the third argument is specified, less than the sum of the value of the second and third arguments. The comparisons and addition used for the preceding follow the standard IEEE 754 rules. The XPath processor rounds the second and third arguments as if by a call to the round() function. For example:

substring("12345", 1.5, 2.6) returns "234"  
substring("12345", 0, 3) returns "12"  
substring("12345", 0 div 0, 3) returns ""  
substring("12345", 1, 0 div 0) returns ""  
substring("12345", -42, 1 div 0) returns "12345"  
substring("12345", -1 div 0, 1 div 0) returns ""  

 
Free Stylus Studio XML Training:
W3C Member