Python elementtree 添加自定义属性
我在 Win10 上使用 Python ElementTree 3.3 和 Python 3.9。
我正在开发一个项目,为此我创建了一些自定义 xml 属性以添加到 svg 文件中,以便使用外部脚本修改它们。当然,这些属性不是标准 xml“接受”属性的一部分。现在我认为这并不重要,因为我自己处理它们,ElementTree 只需在我想要的点解析属性,但是我收到错误消息:
ValueError: Invalid attribute name 'this:custom-tag'
调用错误的代码行如下:
e.set('this:custom-tag' , 'transform({{' + 'var ' + '}},' + rPoint1 + ',' + rPoint2 + ')')
是否存在我可以扩展可接受的属性列表吗?
I am using Python ElementTree 3.3 with Python 3.9 on Win10.
I am working on a project for which I created some custom xml-attributes to add to svg files to modify them with an external script. These attributes, of course, are not part of the standard xml 'accepted' attributes. Now I thought that did not matter since I would be handling them myself and ElementTree simply has to parse the attributes at the points I want them, however I get the Error message:
ValueError: Invalid attribute name 'this:custom-tag'
The line of code that invokes the error is the following:
e.set('this:custom-tag' , 'transform({{' + 'var ' + '}},' + rPoint1 + ',' + rPoint2 + ')')
Is there a way for me to expand the list of accepted attributes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以它的工作原理就像 @mzjn 发送的线程所说的那样。我必须添加一个新的命名空间,定义新的命令,然后设置该命令。它看起来像这样:
然后在我的例子中,我迭代了 xml 文件根的元素:
So it works just as the thread @mzjn sent says. I had to add a new namespace, define the new command and then set that command. It looks like this:
and then in my case I iterated over the elements of the root of the xml-file: