我是 xmlstarlet 的新手,所以希望这个答案很简单。
我正在编写一个脚本来从命令行修改 Inkscape SVG 文件。我选择了工具 xmlstarlet。
在测试文件上测试命令语法后,我在实际 SVG 文件上遇到了问题。我认为命名空间的使用让我失望。
示例文件:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:osb="http://www.openswatchbook.org/uri/2009/osb"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="603"
height="1000"
viewBox="0 0 159.54375 264.58334"
version="1.1"
id="svg8"
inkscape:version="0.92.1 r"
sodipodi:docname="test.svg"
inkscape:export-filename="/home/user/dev/inkscape/test/1.png"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96">
<defs
id="defs2">
<linearGradient
inkscape:collect="always"
id="linearGradient6204">
<stop
style="stop-color:#8f1a22;stop-opacity:1;"
offset="0"
id="stop6200" />
<stop
style="stop-color:#8f1a22;stop-opacity:0;"
offset="1"
id="stop6202" />
</linearGradient>
</defs>
</svg>
我想将 Gradient6204
更改为 Gradient9999
。
我写了这个命令,它不起作用(只返回原始文件)。
xmlstarlet ed -u "/svg/defs/linearGradient[@id='linearGradient6204']/@id" -v 'linearGradient9999' text.txt
我也再次尝试,使用 -N 添加命名空间,但没有成功。我发现如果我删除该行:
xmlns="http://www.w3.org/2000/svg"
从文件然后我上面写的命令起作用。
按照我描述的方式更新上面的 SVG 文件的正确语法是什么?
最佳答案
显式命名空间声明
添加 -N s=http://www.w3.org/2000/svg
然后使用 s:
命名空间前缀:
xmlstarlet ed -N s=http://www.w3.org/2000/svg -u "/s:svg/s:defs/s:linearGradient[@id='linearGradient6204']/@id" -v 'linearGradient9999' text.txt
默认命名空间的隐式声明
从 XMLStarlet v1.2.1 开始,可以通过使用 的自动绑定(bind)来避免默认命名空间的显式命令行定义(例如 OP 的 SVG 文件的情况) _
到默认命名空间:
1.3. A More Convenient Solution
XML documents can also use different namespace prefixes, on any element in the document. In order to handle namespaces with greater ease, XMLStarlet (versions 1.2.1+) will use the namespace prefixes declared on the root element of the input document. The default namespace will be bound to the prefixes
_
andDEFAULT
(in versions 1.5.0+).
所以,上面的命令行可以重写为:
xmlstarlet ed -u "/_:svg/_:defs/_:linearGradient[@id='linearGradient6204']/@id" -v 'linearGradient9999' text.txt
另见
关于xml - 如何在 xmlstarlet 中声明 XPath 命名空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44186213/