如何在python中使用lxml处理XLink引用? [英] How should I process XLink references with lxml in python?

查看:46
本文介绍了如何在python中使用lxml处理XLink引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求编写一些脚本,这些脚本读取XML配置文件,这些配置文件广泛使用XLink来包含存储在多个文件中的XML.例如:

I've been asked to write some scripts that read in XML configuration files that make liberal use of XLink to include XML stored in multiple files. For example:

<Environment xlink:href="#{common.environment}" />

(#{common.environment}是一个属性占位符,它首先会被解析,在这里可以忽略.)该公司已经在lxml上进行了标准化,以便在python中进行高级XML处理.

(#{common.environment} is a property placeholder that gets resolved first and can be ignored here.) The company has standardized on lxml for advanced XML processing in python.

我一直在寻找有关如何在这些限制条件下处理这些事件的示例或文档,并且至少将它们的内容包括在父XML文档中,就像它们实际上是在此时插入一样.我有点惊讶地发现那里几乎没有珍贵的东西,以至于我想知道我是否缺少明显的东西.我找到了有关 XLink 是什么的通用文档,并且我找到了一些在 XSLT 处理上下文中使用它的示例.但这对我没有帮助.

I've been looking for examples or docs on how to process these occurrences under these restraints and, at a minimum, include their content in the parent XML document as if they were actually inserted at that point. I've been a bit surprised to find precious little out there to the point that I'm wondering if I'm missing something obvious. I've found generic docs on what XLink is and I've found a few example of it being used in the context of XSLT processing. That hasn't been helpful to me though.

任何人都可以就如何最好地实现这一点提供任何建议,无论是文档,示例还是经验中的一些建议?谢谢.

Could anyone offer any advice on how to best implement this whether it be docs, examples or just some advice from experience? Thanks.

更新:这是之前和之后的示例:

UPDATE: Here is a before and after example:

之前.这就是正在解析的文件中的实际内容:

Before. This is what is actually in the file being parsed:

<Root>
    <Environment xlink:href="#{common.environment}" />
</Root>

这是#{common.environment}解析为的文件中的内容:

This is what is in the file that #{common.environment} resolves to:

<?xml version="1.0" encoding="UTF-8"?>
<Environment>
    <Property key="hello.world" value="foo" />
    <Property key="bar.baz" value="fred" />
</Environment>

之后.这是解析器在完成所有处理后看到"它的方式:

After. This is how the parser "sees" it after all processing is done:

<Root>
    <Environment>
        <Property key="hello.world" value="foo" />
        <Property key="bar.baz" value="fred" />
    </Environment>
</Root>

这是其中发生的事情的根本简化示例.

This is a radically simplified example of what goes on in there.

推荐答案

这个答案可能与您真正需要的答案相去甚远,但是也许可以有所帮助.下面的小程序是我基于彻底简化"的示例所能想到的.

This answer is likely to be a far cry from what you really need, but perhaps it can be of some help. The small program below is what I could come up with based on the "radically simplified" example.

from lxml import etree

parent = etree.parse("parent.xml").getroot()
penv = parent.xpath("Environment")

for e in penv:
    child = e.get("{http://www.w3.org/1999/xlink}href")
    c = etree.parse(child).getroot()
    parent.replace(e, c)

print etree.tostring(parent)

parent.xml:

parent.xml:

<Root xmlns:xlink="http://www.w3.org/1999/xlink">
  <Environment xlink:href="child.xml"/>
</Root>

child.xml:

child.xml:

<Environment>
  <Property key="hello.world" value="foo" />
  <Property key="bar.baz" value="fred" />
</Environment>

程序运行时,输出:

<Root xmlns:xlink="http://www.w3.org/1999/xlink">
  <Environment>
  <Property key="hello.world" value="foo"/>
  <Property key="bar.baz" value="fred"/>
</Environment></Root>

这篇关于如何在python中使用lxml处理XLink引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆