Python lxml 使用 xi:include 包含多个 Xml 片段
我正在 Python 中使用 lxml 开发一个简单的 xml 日志文件类。
到目前为止,我的方法是使用两个文件。格式良好的 XML 文件,其中包括第二个文件(XML 片段)。我正在使用 xi:include 元素。这样,只需将
元素附加到文件末尾,就可以有效地更新 XML 片段。
格式良好的 XML 文件('logfile.xml')如下所示:
<?xml version="1.0"?>
<logfile>
<event xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include href="events.xml"/>
</event>
</logfile>
xml 片段('events.xml')如下所示:
<event>
<data></data>
</event>
<event>
<data></data>
</event>
<event>
<data></data>
</event>
我的目标是最终得到:
<?xml version="1.0"?>
<logfile>
<event>
<data></data>
</event>
<event>
<data></data>
</event>
<event>
<data></data>
</event>
</logfile>
在 python 中,我使用 xinclude 方法来处理 xi :include 元素包含在我格式良好的 XML 文件('logfile.xml')中。这但仅当有一个
元素是 XML 片段('events.xml')时有效
我的 python 代码:
tree = etree.parse('logfile.xml')
tree.xinclude()
root = tree.getroot()
print etree.tostring(self.logfileNode, pretty_print=True, xml_declaration=True, encoding='UTF-8')
我看到的错误:
lxml.etree.XIncludeError: Extra content at the end of the document
我可以将事件包含在另一个元素中,但这并不适合将数据附加到 XML 片段文档的末尾。
I am developing a simple xml logfile class using lxml in Python.
My approach so far has been to use two files. A well-formed XML file which includes a second file which is a XML fragment. I am using an xi:include element. This way XML fragment can be updated efficiently by simply appending <event>
elements to the end of the file.
The well formed XML file ('logfile.xml') looks like this:
<?xml version="1.0"?>
<logfile>
<event xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include href="events.xml"/>
</event>
</logfile>
The xml fragment ('events.xml') looks like this:
<event>
<data></data>
</event>
<event>
<data></data>
</event>
<event>
<data></data>
</event>
My goal is to end up with:
<?xml version="1.0"?>
<logfile>
<event>
<data></data>
</event>
<event>
<data></data>
</event>
<event>
<data></data>
</event>
</logfile>
In python I am using the xinclude method to process the xi:include element in my well formed XML file ('logfile.xml'). This works but only if there is one <event>
element is the XML fragment ('events.xml')
My python code:
tree = etree.parse('logfile.xml')
tree.xinclude()
root = tree.getroot()
print etree.tostring(self.logfileNode, pretty_print=True, xml_declaration=True, encoding='UTF-8')
The error I am seeing:
lxml.etree.XIncludeError: Extra content at the end of the document
I could contain the events in another element, but this doesn't lend itself well to appending data to the end of the XML fragment document.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
xi:xinclude
引用的文档必须是一个完整的 xml 文档(“完整的 xml 信息集”)。您的events.xml
不是有效的 xml 文档,因为您没有包含单个根元素。您可以使用
xpointer
属性来选择事件元素,从而仅包含一个子集。不过,我不确定 lxml 支持此属性。The document referenced by
xi:xinclude
must be a complete xml document ("complete xml infoset"). Yourevents.xml
is not a valid xml document because you don't have a single root containing element.You might be able to include just a subset by using the
xpointer
attribute to select the event elements. I'm not sure lxml supports this attribute, however.