在 Python 中进行 XML 转换的现代方法是什么?

发布于 2024-12-04 10:30:01 字数 476 浏览 0 评论 0原文

我需要在 Python 中的一些非常复杂的 XML 和平面文件格式之间进行双向转换。我已经过时了,不知道人们如何在遥远的未来 2011 年解决这个问题。

我已经恢复了各种 Python XML 库的最新状态,但距离我上次更新已经过去 8 年了在 XSLT 地狱中,我在谷歌搜索后感到惊讶,这仍然很常见。

那么如何进行复杂的 XML 数据转换呢?

我想在Python中执行此操作,因为文档不是直接映射,并且需要一些处理和计算。但我仍然希望将尽可能多的内容传递给规则引擎。

编辑:需要明确的是,我对技术比对特定库或工具更感兴趣,但也请发布这些内容。我在这里尽力避免使用单词模式,但这无疑是一个常见问题。

编辑2:我仍然认为在通用技术上没有任何好的答案,但我最初遇到的问题是使用 Bots EDI 框架进行文档翻译解决的。它非常专注于 EDI,但也可用于通用翻译。但这是一个重量级的解决方案。

I need to do bi-direction transforms between some significantly complex XML and flat-file formats in Python. I'm out of date and don't know how people are solving this problem in the far off future year of 2011.

I've got back up to date with the various Python XML libraries, but it's been 8 years since my last time in XSLT hell and I was amazed after googling around that that's still common.

So how do you go about doing complex XML data transforms?

I'd like to do this in Python because the documents are not direct mappings and there is some processing and calculation required. But I'd still like to pass as much off to a rule engine as possible.

Edit: To be clear I'm interested in techniques more so than specific libraries or tools, but please to post those too. I'm trying hard to avoid the word pattern here, but surely this is a common problem.

Edit 2: I still don't think there were any good answers on general techniques, but the original problem I had was solved using the Bots EDI framework for doing document translations. It is quite heavily focussed on EDI but can be used for generic translation. It was a heavy-weight solution though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

迷鸟归林 2024-12-11 10:30:01

对于 python,以下是可用 XML 库/模块的完整列表:

http://wiki.python.org/ moin/PythonXml

如果您正在寻找比 XSLT 更简单的东西,XMLStarlet 是一组您可能感兴趣的命令行工具:

http://xmlstar.sourceforge.net/

与任何命令行工具一样,这并不是专门为 Python 设计的,但可以轻松集成到 python 脚本中。

For python, here is a comprehensive list of available XML libs/modules:

http://wiki.python.org/moin/PythonXml

If you are looking for something simpler than XSLT, XMLStarlet is a set of command line tools which may be of interest for you:

http://xmlstar.sourceforge.net/

As any command line tool, this is not especially made for Python, but can be easily integrated into a python script.

凡间太子 2024-12-11 10:30:01

虽然它只对编写 XML 有用,但 XMLwitch 非常神奇。对于进行非 XML 到 XML 的转换,我强烈推荐它!

Although it’s only useful for writing XML, XMLwitch is freakin’ amazing. For doing non-XML to XML transformations, I highly recommend it!

转身以后 2024-12-11 10:30:01

嗨 Dimitre 我使用 lxml 在 python 中操作 xml。

我发布了一些管理大量 xml 模式、命名空间等的技术。自动 XSD 验证

一个建议是尽可能尝试使用完整的 xpath。

例如,如果我有一个复杂类型:

<Person>
 <name/>
 <age/>
</person>

如果我不使用 /Person/name 我可能会遇到问题,后来复杂类型更改为:

<person>
 <name/>
 <age/>
 <child>
   <son>
     <name/>
     <age/>
   </son>
</child>
</person>

现在“名称”存在于多个位置的原因。

还要注意此示例中允许许多“人”的模式。您可能需要在 xpath 中提供一个“密钥”来确定您引用的人。您的 xml 中可以有 5 或 6 个人,xpath 是相同的,但名称是唯一的,该名称将成为您用来引用每个特定人的密钥。

我还建议围绕 lxml 编写您自己的包装函数以满足您的需求。我所做的是创建了一个 xmlUtil.py 文件,其中包含我需要的 xml 通用函数。然后,我创建了一个 myXML.py 文件,该文件对我的特定 xml 和行为进行了假设。 xmlUtil.py 函数仅接受 xml 内容(如果我决定使用某些内容而不是 lxml,则很容易更改)。

希望其中一些有所帮助。希望我能提供更多帮助,但问题是开放式的。

Hi Dimitre I use lxml to manipulate xml in python.

I have posted some techniques for managing large amounts of xml schemas, namespaces etc.. Automatic XSD validation

One suggestion is to try to use the full xpath when possible.

For example if i had a complex type:

<Person>
 <name/>
 <age/>
</person>

i could run into the problem if i dont use /Person/name and later that complex type changes to:

<person>
 <name/>
 <age/>
 <child>
   <son>
     <name/>
     <age/>
   </son>
</child>
</person>

The reason being now 'name' exists in multiple places.

Also be aware of schemas that allow for many "persons" in this example. you may need to provide a "key" with your xpath to determine which person you are referencing. you could have 5 or 6 Persons in your xml the xpaths would be identical but the names uniqe, the name would then be your key to use to reference each particular person.

I would also suggest writing your own wrapper functions around lxml that suit your needs. What i did was I created an xmlUtil.py file that contained xml generic functions that i needed. I then created a myXML.py file that had assumptions about my specific xml and behavior. the xmlUtil.py functions only accept the xml content ( this is in case i decide to use something instead of lxml it will be easy to change).

Hope some of this helps. wish i could be more helpful but question is very open ended.

撩动你心 2024-12-11 10:30:01

虽然它也“只是”一个库 - 或者更确切地说是一个框架,但 inxs 是一种避免 XSLT 地狱的方法使用 Python 进行基于规则的转换。

Though it's also 'just' a library - or rather a framework, inxs is an approach to avoid the XSLT hell with rule-based transformations with Python.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文