【问题标题】:How could I simply this xpath so that xmlstartlet works with it?我怎么能简单地使用这个 xpath 以便 xmlstartlet 使用它?
【发布时间】:2011-05-20 18:37:35
【问题描述】:
$ 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>

【问题讨论】:

  • 你能把相关的 XML sn-p 也发一下吗?
  • @Tomalak - 抱歉,尝试发布最短的示例。

标签: xml command-line xpath xmlstarlet


【解决方案1】:

您是否尝试从您的示例中返回 6

我没有 xmlstarlet 来测试这个,但是试试这个 XPath:

//*[str[@name='id']='http://localhost:8080/index.html']/str[@name='doc-norm']

当它的父元素有一个子 str 元素,该元素的 id 属性值为http://localhost:8080/index.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()

【讨论】:

  • +1 以获得好的答案。我猜嵌套引号可能是 OP 问题的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
  • 2011-01-19
  • 2016-09-18
  • 1970-01-01
  • 2022-01-06
相关资源
最近更新 更多