Manipulating Strings

After you obtain a string, you might want to manipulate it and use the result in the query. This section describes functions that allow you to do this. It discusses the following topics:

Concatenating Strings

To concatenate two or more strings, call the concat() function. The format is

string concat(string, string, {string}...) 

The concat() function returns the concatenation of its arguments.

Determining the Number of Characters in a String

To obtain the number of characters in a string, call the string-length() function. The format is

number string-length(string?) 

The string-length() function returns the number of characters in the string. If you omit the argument, it defaults to the string value of the context node.

Normalizing Strings

To strip leading and trailing white space from a string, call the normalize-space() function. The format is

string normalize-space(string?) 

The normalize-space() function removes leading and trailing white space. White space consists of spaces, tabs, new lines, and returns.

If there are consecutive internal spaces, the normalize-space() function collapses the internal spaces into one space. The normalize-space() function returns the string with the extraneous white space removed. If you omit the argument, it defaults to the string value of the context node.

Replacing Characters in Strings with Characters You Specify

To replace characters in a string with other characters, call the translate() function. The format is

string translate(string, string, string) 

The translate() function looks for characters in the first string that are also in the second string. For each such character, the translate() function replaces the character in the first string with a character from the third string. The replacement character is the character in the third string that is in the same position as the character in the second string that corresponds to the character being replaced. For example:

translate("bar", "abc", "ABC")

Execution of this function returns "BAr". Following is another example:

translate("---aaa---", "abc", "ABC")

Execution of this function returns "AAA". Sometimes there is a character in the second argument string with no character at a corresponding position in the third argument string. This happens when the second argument string is longer than the third argument string. In this case, the XPath processor removes occurrences of that character.

If a character occurs more than once in the second argument string, the first occurrence determines the replacement character. If the third argument string is longer than the second argument string, the XPath processor ignores the excess characters.

Converting Objects to Strings

In some situations, you might want to force a string comparison. The XPath processor performs a string comparison only when the operands are neither Boolean nor numeric values. If an operand is numeric or Boolean, call the string() function on it to convert it to a string. The format of the string() function is

string string(object?) 

The string() function can convert any object to a string. If you omit the argument, it defaults to a node set with the context node as the only member. The string value of an element node is the concatenation of the string values of all text node descendants of the element node in document order.

Node Sets

When the string() function converts a node set to a string, it returns the string value of the node in the node set that is first in document order. If the node set is empty, the string() function returns an empty string.

Numbers

The string() function converts numbers to strings as follows:

Boolean Values

The string() function converts the Boolean false value to the string "false", and the Boolean true value to the string "true".

Finding Strings That Start with a Particular String

To determine if a string starts with a particular string, specify the starts-with() function. The format is

boolean starts-with(string, string) 

This function returns true if the first argument string starts with the second argument string, and otherwise returns false.

 
Free Stylus Studio XML Training: