Python elementtree 添加自定义属性

发布于 2025-01-11 06:34:05 字数 441 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏天碎花小短裙 2025-01-18 06:34:05

所以它的工作原理就像 @mzjn 发送的线程所说的那样。我必须添加一个新的命名空间,定义新的命令,然后设置该命令。它看起来像这样:

ET.register_namespace('ns-name-xml', namespace)

然后在我的例子中,我迭代了 xml 文件根的元素:

for e in outputRoot:
        if e.get('id') == selectedRow:
            e.set(ET.QName(namespace,'your-command') , 'some-value')

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:

ET.register_namespace('ns-name-xml', namespace)

and then in my case I iterated over the elements of the root of the xml-file:

for e in outputRoot:
        if e.get('id') == selectedRow:
            e.set(ET.QName(namespace,'your-command') , 'some-value')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文