我怎样才能简单地使用这个 xpath 以便 xmlstartlet 可以使用它?
$ xmlstarlet sel -t -c "//str[@name="id" and .="http://localhost:8080/index.html"]/../str[@name="doc-norm"]"/value results.xml
我的理解是 xmlstarlet
不完全支持 xpath 表达式。顺便说一句,还有其他命令行工具吗?
<doc>
<str name="id">http://localhost:8080/index.html</str>
<str name="doc-norm">6</str>
</doc>
$ xmlstarlet sel -t -c "//str[@name="id" and .="http://localhost:8080/index.html"]/../str[@name="doc-norm"]"/value results.xml
My understanding is that xmlstarlet
doesn't fully support xpath expressions. Is there any other command-line tool that does BTW?
<doc>
<str name="id">http://localhost:8080/index.html</str>
<str name="doc-norm">6</str>
</doc>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试从示例中返回
6
?我没有 xmlstarlet 来测试它,但尝试这个 XPath:
//*[str[@name='id']='http://localhost:8080/index.html']/str[ @name='doc-norm']
这应该返回
str
元素的值,该元素的name
属性的值为doc-当其父元素具有子
. (我希望这是有道理的!)str
元素时,该子元素具有值为http://localhost:8080/index.norm
的id
属性。 html我还应该补充一点,如果您知道父元素的级别是什么,请尽量避免使用
//
。类似/doc[str[@name='id']='http://localhost:8080/index.html']/str[@name='doc-norm']
的内容是更有效率。更新
我下载了 xmlstartlet 进行测试,它工作正常,但它返回整个
str
元素。如果您只需要文本,请将text()
添加到 XPath 的末尾://*[str[@name='id']='http://localhost:8080 /index.html']/str[@name='doc-norm']/text()
Are you trying to return the
6
from your example?I don't have xmlstarlet to test this on, but try this XPath:
//*[str[@name='id']='http://localhost:8080/index.html']/str[@name='doc-norm']
This should return the value of a
str
element that has aname
attribute with a value ofdoc-norm
when its parent element has a childstr
element that has anid
attribute with a value ofhttp://localhost:8080/index.html
. (I hope that makes sense!)I should also add that if you know what the level of the parent element is, try to avoid using the
//
. Something like/doc[str[@name='id']='http://localhost:8080/index.html']/str[@name='doc-norm']
would be more efficient.UPDATE
I downloaded xmlstartlet to test and it works fine, however it returns the entire
str
element. If you want the text only, addtext()
to the end of the XPath://*[str[@name='id']='http://localhost:8080/index.html']/str[@name='doc-norm']/text()