XML XPath String Operators
- XPath string operators are used to compare and manipulate strings in XPath expressions.
- They are mainly used to search for and select the specific nodes based on the text they contain.
Syntax
The following are the XPath string operators available in XPath:
=
,!=
- Used for string comparisonconcat()
- Used to concatenate two or more stringssubstring()
- Used to extract a part of a stringstarts-with()
- Used to check if a string starts with a specific substringcontains()
- Used to check if a string contains a specific substringends-with()
- Used to check if a string ends with a specific substring
Example
Assume the following XML document:
<root>
<item>apple</item>
<item>banana</item>
<item>carrot</item>
</root>
The following XPath expression will select all item
elements that contain the substring "a":
/root/item[contains(.,'a')]
Output
The output of the above XPath expression will be:
<item>apple</item>
<item>banana</item>
<item>carrot</item>
Explanation
The above XPath expression selects all item
elements under the root
element that contain the string "a".
The contains()
function checks if the text content of an element contains a specified substring. If it does, the function returns true
, and the element is selected.
Use
XPath string operators are mainly used to search for and select specific nodes based on their text content. They can be used in conjunction with other XPath axes and operators to create powerful and flexible XPath expressions.
Important Points
- XPath string operators are used to compare and manipulate strings in XPath expressions.
- XPath string operators are mainly used to search for and select specific nodes based on their text content.
- XPath string operators can be used in conjunction with other XPath axes and operators to create powerful and flexible XPath expressions.
Summary
XPath string operators are essential for selecting specific nodes based on their text content. They are useful for performing string comparisons, concatenation, extraction, and searching operations in XPath expressions. XPath string operators can be manipulated and used in conjunction with other XPath operators to create more powerful and flexible XPath expressions.