在bash中,如何逃避"{}"?

cui*_*hao 0 bash xpath

我想在bash中解析XML,xpath可以做到.

(要获得带有子元素"em:minVersion = 2.1"的"Description"元素)这样的查询效果很好:

xpath install.rdf /RDF/Description/em:targetApplication/Description[em:minVersion=2.1]
Run Code Online (Sandbox Code Playgroud)

但这不起作用:

xpath install.rdf /RDF/Description/em:targetApplication/Description[em:id={ec8030f7-c20a-464f-9b0e-13a3a9e97384}]
Run Code Online (Sandbox Code Playgroud)

输出如下:

Query:
/RDF/Description/em:targetApplication/Description[em:id={{ec8030...
.......................................................^^^
Invalid query somewhere around here (I think)
Run Code Online (Sandbox Code Playgroud)

我认为这是因为大括号"{}"需要转义,所以我尝试了'{{..}}','{...}'......它们都不起作用.

我根本不熟悉xpath或perl ......

cjm*_*cjm 6

尝试

xpath install.rdf '/RDF/Description/em:targetApplication/Description[em:id="{ec8030f7-c20a-464f-9b0e-13a3a9e97384}"]'
Run Code Online (Sandbox Code Playgroud)

这实际上与Perl没有任何关系,除了xpath实用程序是用它写的.一切都bashXPath有关.

为防止bash查看它,请将整个查询放在单引号中.

在XPath中,必须引用字符串(使用单引号或双引号).在XPath表达式中使用双引号通常最简单,然后在其周围加上单引号bash.

既然你正在寻找

<em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
Run Code Online (Sandbox Code Playgroud)

你想要的条件是

em:id="{ec8030f7-c20a-464f-9b0e-13a3a9e97384}"
Run Code Online (Sandbox Code Playgroud)

你正在逃避em:minVersion=2.1因为2.1看起来像一个数字,所以XPath进行数字比较.但这不适用于任意字符串.