Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to apply normalize-space to all nodes XPath expression finds?

Tags:

xml

xpath

Consider simple XML document:

<html><body>
<table>
<tr><td>   Item 1</td></tr>
<tr><td>  Item 2</td></tr>
</table>
</body></html>

Using XPath /html/body/table/tr/td/text() we will get

["   Item 1", "  Item 2"]. 

Is it possible to trim white space, for example using normalize-space() function to get this?

["Item 1", "Item 2"]

normalize-space(/html/body/table/tr/td/text()) yields trimmed contents of only the first td tag ["Item 1"]

like image 557
Victor Olex Avatar asked Jul 29 '10 04:07

Victor Olex


People also ask

WHAT IS A in XPath?

What is XPath? XPath is a major element in the XSLT standard. XPath can be used to navigate through elements and attributes in an XML document. XPath stands for XML Path Language. XPath uses "path like" syntax to identify and navigate nodes in an XML document.


1 Answers

Using XPath "/html/body/table/tr/td/text()" we will get [" Item 1", " Item 2"].

Is it possible to trim white space for example using normalize-space() function to get ["Item 1", "Item 2"]?

Not in XPath 1.0.

In Xpath 2.0 this is simple:

/html/body/table/tr/td/text()/normalize-space(.) 

In XPath 2.0 a location step of an XPath expression may be a function reference. This is used in the expression above to produce a sequence of xs:string items, each of which is the result of applying normalize-space() on the context node (any node selected by the subexpression that precedes the last location step).

like image 178
Dimitre Novatchev Avatar answered Sep 22 '22 09:09

Dimitre Novatchev