[英]xmlstarlet: create an attribute if it does not exist and edit it otherwise
我想使用xmlstarlet查找包含属性inkscape:label =“ L2”的xml文件的元素,并将其属性“ style”设置为值“ display.inline”。 困难在于属性“样式”可能已经定义,也可能尚未定义。
我当前正在使用以下命令:
xmlstarlet edit --inplace --update "//*[@inkscape:label=\"L2\"]/@style" --value "display:inline" ex.svg
如果属性样式已经定义,它将起作用
// It works on this
<g inkscape:groupmode="layer"
id="layer2"
inkscape:label="L2"
style="display:none">
但它不能正常工作:
// Does not work
<g inkscape:groupmode="layer"
id="layer2"
inkscape:label="L2">
我还定义了一个命令,可以添加所需的属性:
xmlstarlet ed --insert "//*[@inkscape:label=\"L2\"]" --type attr -n style -v "display:inline" ex.svg > output.svg
不幸的是,如果该属性已经存在,则会添加第二个属性:
// The element now contains two attributes style
<g inkscape:groupmode="layer"
id="layer2"
inkscape:label="L2"
style="display:none"
style="display:inline">
如果属性不存在,是否可以创建该属性,否则可以对其进行编辑?
您可以同时使用--update
和--insert
,但是仅在元素没有style
属性( not(@style)
)时才插入。
例:
xmlstarlet edit --inplace --update "//*[@inkscape:label=\"L2\"]/@style" --value "display:inline" --insert "//*[@inkscape:label=\"L2\"][not(@style)]" --type attr -n style -v "display:inline" ex.svg
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.