Python lxml using xi: includes several Xml snippets

I am developing a simple xml logging class using lxml in Python.

So far, my approach has been to use two files. A well-formed XML file that includes a second file that is an XML fragment. I am using the xi: include element. In this way, the XML fragment can be effectively updated by simply adding <event> elements to the end of the file.

A 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:

 <?xml version="1.0"?> <logfile> <event> <data></data> </event> <event> <data></data> </event> <event> <data></data> </event> </logfile> 

In python, I use 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 is:

 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 see is:

 lxml.etree.XIncludeError: Extra content at the end of the document 

I could have contained events in another element, but it defies adding data to the end of the XML fragment document.

+4
source share
1 answer

The document referenced by xi:xinclude must be a complete xml document ("complete xml infoset"). Your events.xml not a valid xml document because you do not have a single element containing the root.

Perhaps you can only include a subset using the xpointer attribute to select event elements. I'm not sure lxml supports this attribute.

+3
source

Source: https://habr.com/ru/post/1390536/


All Articles