The text() node test retains text nodes, which are children and siblings of element nodes or are children of the root node. In relation to our main example, the following expression returns a single text node, and the string value is Buick.
/Insured/CarPolicy/Vehicle[@Category = 'Coupe']/Make/text()
If you wish to access multiple text nodes for subsequent string processing, you can select an element node that has descendant element nodes. For example, assume that the Material element includes no line break in the next XML document.
<?xml version="1.0" encoding="ISO-8859-1"?> <Material>Leather<Product>Bucket Seats</Product> <Quantity>20</Quantity></Material>
The XPath expression /Material provides the following string value, which represents the content of the three text nodes that are descendants of the Material node.
Also, the string value of the root node (which is expressed by a virgule) resolves to the same text nodes as does the root element node (which is subordinate). Either of the following expressions resolves to the string Bucket Seats.
/descendant::text()[2] /Material/descendant::text()[2]
Last, you may become perplexed about the location of text nodes that include or are composed of white space (carriage return, spaces, tabs). Consider the following Material element, for example.
<Material>Leather <Product>Bucket Seats</Product> <Quantity>20</Quantity> </Material>
Three text nodes are children of that element:
the string Leather followed by the white space that is between the end of Leather and the left angle bracket of the Product start-tag
the white space between the right angle bracket that ends the Product element and the left angle bracket of the Quantity start-tag
the white space between the right angle bracket that ends the Quantity element and the left angle bracket of the Material end-tag
You can access any of those text nodes by a positional predicate. Here's a location path that returns white space.
/Material/text()[2]
XPath does not retain any space that precedes the first element node.