如何在 xmlstarlet 中声明 XPath 命名空间?
我是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文件的适当语法是什么?
I am new to xmlstarlet so hoping this answer is a simple one.
I am writing a script to modify Inkscape SVG files from the command line. I chose the tool xmlstarlet.
After testing the command syntax on test files, I am having trouble on the real SVG files. I think the use of namespaces is throwing me off.
Example file:
<?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>
I want to change Gradient6204
to Gradient9999
.
I wrote this command, which does not work (just returns the original file).
xmlstarlet ed -u "/svg/defs/linearGradient[@id='linearGradient6204']/@id" -v 'linearGradient9999' text.txt
I also tried again, adding the namespaces with -N but no luck. I found that if I delete the line:
xmlns="http://www.w3.org/2000/svg"
from the file then the command I wrote above works.
What is the proper syntax for updating the SVG file above in the way I described?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显式命名空间声明
添加
-N s=http://www.w3.org/2000/svg
然后使用s:
命名空间前缀有效:默认命名空间的隐式声明
从 XMLStarlet v1.2.1 开始,可以通过使用自动绑定来避免默认名称空间的显式命令行定义(例如 OP 的 SVG 文件的情况)
_
到默认命名空间:因此,上面的命令行可以重写为:
另请参阅
Explicit Namespace Declaration
Adding
-N s=http://www.w3.org/2000/svg
and then using thes:
namespace prefix works:Implicit Declaration of Default Namespace
Starting with XMLStarlet v1.2.1, an explicit command line definition for the default namespace (such as is the case with OP's SVG file) can be avoided via use of an automated binding of
_
to the default namespace:So, the above command line could be re-written as:
See also