Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
804 views
in Technique[技术] by (71.8m points)

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

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"]

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...