[英]xmlstarlet: create an attribute if it does not exist and edit it otherwise
I would like to use xmlstarlet to find the element of an xml file which contains the attribute inkscape:label="L2" and set the its attribute "style" to the value "display.inline". 我想使用xmlstarlet查找包含属性inkscape:label =“ L2”的xml文件的元素,并将其属性“ style”设置为值“ display.inline”。 The difficulty is that the attribute "style" may or may not already be defined.
困难在于属性“样式”可能已经定义,也可能尚未定义。
I am currently using this command: 我当前正在使用以下命令:
xmlstarlet edit --inplace --update "//*[@inkscape:label=\"L2\"]/@style" --value "display:inline" ex.svg
It will work if the attribute style is already defined 如果属性样式已经定义,它将起作用
// It works on this
<g inkscape:groupmode="layer"
id="layer2"
inkscape:label="L2"
style="display:none">
but it will not work otherwise: 但它不能正常工作:
// Does not work
<g inkscape:groupmode="layer"
id="layer2"
inkscape:label="L2">
I also defined a command which enables to add the desired attribute: 我还定义了一个命令,可以添加所需的属性:
xmlstarlet ed --insert "//*[@inkscape:label=\"L2\"]" --type attr -n style -v "display:inline" ex.svg > output.svg
Unfortunately, if the attribute already exists, a second one will be added: 不幸的是,如果该属性已经存在,则会添加第二个属性:
// The element now contains two attributes style
<g inkscape:groupmode="layer"
id="layer2"
inkscape:label="L2"
style="display:none"
style="display:inline">
Is there a way to create the attribute if it does not exist and to edit it otherwise? 如果属性不存在,是否可以创建该属性,否则可以对其进行编辑?
You can use both the --update
and --insert
, but only insert when the element does not have a style
attribute ( not(@style)
). 您可以同时使用
--update
和--insert
,但是仅在元素没有style
属性( not(@style)
)时才插入。
Example: 例:
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.