问题描述
考虑简单的 XML 文档:
Consider simple XML document:
<html><body>
<table>
<tr><td> Item 1</td></tr>
<tr><td> Item 2</td></tr>
</table>
</body></html>
使用 XPath /html/body/table/tr/td/text()
我们会得到 p>
Using XPath /html/body/table/tr/td/text()
we will get
[" Item 1", " Item 2"].
是否可以修剪空白,例如使用 normalize-space()
函数来得到这个?
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())
仅生成第一个 td 标签的修剪内容 ["Item 1"]
normalize-space(/html/body/table/tr/td/text())
yields trimmed contents of only the first td tag ["Item 1"]
推荐答案
是否可以修剪空白使用 normalize-space() 的示例函数获取 ["Item 1", "Item 2"]?
Is it possible to trim white space for example using normalize-space() function to get ["Item 1", "Item 2"]?
不在 XPath 1.0 中.
在 Xpath 2.0 中这很简单:
/html/body/table/tr/td/text()/normalize-space(.)
在 XPath 2.0 中,XPath 表达式的位置步骤可能是函数引用.这在上面的表达式中用于生成一系列 xs:string
项,每个项都是将 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).
这篇关于是否可以将 normalize-space 应用于 XPath 表达式找到的所有节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!