考虑简单的 XML 文档:
<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()
我们会得到
[" Item 1", " Item 2"].
是否可以修剪空白,例如使用 normalize-space()
函数得到这个?
["Item 1", "Item 2"]
normalize-space(/html/body/table/tr/td/text())
只产生第一个 td 标签的修剪内容 ["Item 1"]
最佳答案
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"]?
不在 XPath 1.0 中。
在 Xpath 2.0 中这很简单:
/html/body/table/tr/td/text()/normalize-space(.)
在 XPath 2.0 中,XPath 表达式的位置步骤可能是函数引用。这在上面的表达式中用于生成一系列 xs:string
项,每个项都是在上下文节点上应用 normalize-space()
的结果(任何由最后一个定位步骤之前的子表达式选择的节点)。
关于xml - 是否可以将 normalize-space 应用于 XPath 表达式找到的所有节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3359512/