如何过滤 XMLStarlet 中的多个属性?
How to filter on multiple attributes in XMLStarlet?
例如,给定:
<fruit>
<banana source='Ecuador' category='I'>
<quantity>1</quantity>
</banana>
<banana source='Costa Rica' category='I'>
<quantity>1</quantity>
</banana>
</fruit>
说我要改
<banana source='Costa Rica' category='I'>
到
<banana source='Costa Rica' category='II'>
或者它的数量为2,如果我想同时过滤源和初始类别值,我该如何引用它?
我正在尝试执行以下操作:
xmlstarlet ed -u "/fruit/banana[@source='Ecuador' @category='I']/quantity" -v 2
...但这会导致语法错误,如下:
Invalid predicate: /fruit/banana[@source='Ecuador' @category='I']/quantity
Invalid expression: /fruit/banana[@source='Ecuador' @category='I']/quantity
经过反复试验,得出了解决方案:
我需要使用下一个属性关闭并重新打开 []。例如:
xmlstarlet ed -u "/fruit/banana[@source='Ecuador'][@category='I']/quantity" -v 2 example.xml
并且会正确输出:
<?xml version="1.0"?>
<fruit>
<banana source="Ecuador" category="I">
<quantity>2</quantity>
</banana>
<banana source="Costa Rica" category="I">
<quantity>1</quantity>
</banana>
</fruit>
编辑:也有效:
xmlstarlet ed -u "/fruit/banana[@source='Ecuador' and @category='I']/quantity" -v 2 example.xml
例如,给定:
<fruit>
<banana source='Ecuador' category='I'>
<quantity>1</quantity>
</banana>
<banana source='Costa Rica' category='I'>
<quantity>1</quantity>
</banana>
</fruit>
说我要改
<banana source='Costa Rica' category='I'>
到
<banana source='Costa Rica' category='II'>
或者它的数量为2,如果我想同时过滤源和初始类别值,我该如何引用它?
我正在尝试执行以下操作:
xmlstarlet ed -u "/fruit/banana[@source='Ecuador' @category='I']/quantity" -v 2
...但这会导致语法错误,如下:
Invalid predicate: /fruit/banana[@source='Ecuador' @category='I']/quantity
Invalid expression: /fruit/banana[@source='Ecuador' @category='I']/quantity
经过反复试验,得出了解决方案:
我需要使用下一个属性关闭并重新打开 []。例如:
xmlstarlet ed -u "/fruit/banana[@source='Ecuador'][@category='I']/quantity" -v 2 example.xml
并且会正确输出:
<?xml version="1.0"?>
<fruit>
<banana source="Ecuador" category="I">
<quantity>2</quantity>
</banana>
<banana source="Costa Rica" category="I">
<quantity>1</quantity>
</banana>
</fruit>
编辑:也有效:
xmlstarlet ed -u "/fruit/banana[@source='Ecuador' and @category='I']/quantity" -v 2 example.xml