【问题标题】:How to get all the text inside an XML tag using XPath?如何使用 XPath 获取 XML 标记内的所有文本?
【发布时间】:2017-12-04 21:04:00
【问题描述】:
我在 XML 中有这样的标签:
<command name="config">config show</command>
例如,要使用 xpath 读取那个,我会这样做:
//command[@name='config']
但这会输出两个字符串:“config”和“show”。我希望能够得到一个唯一的字符串:“config show”。
我尝试了/text() 和/node(),但没有成功。任何想法?
【问题讨论】:
标签:
xml
xpath
tcl
xpathnavigator
【解决方案1】:
就我而言,它的工作原理是:
//command[@name='check_config']/text()
我认为我之前尝试使用函数 text() 时在语法上做错了! :)
【解决方案2】:
对于
<command name="check_config">config show</command>
这个 XPath,
//command[@name='check_config']
选择文档中所有command 属性值等于'config' 的@name 元素;在这种情况下,只有上面显示的元素:
<command name="check_config">config show</command>
应该在自动转换为字符串的上下文中评估此元素,还是应该通过string() 函数强制进行此类转换,
string(//command[@name='check_config'])
它将被转换为该元素的字符串值,
config show
这是一个字符串,而不是两个。