1

I am using lxml to parse an xml file in python. The xml has the purpose of configuring my code. Since I need to use many different configurations which slightly differ, I am using the xi:include feature.

It would be really useful to be able to declare some entities in the "main" xml file and let the included ones to know about them. Here is my working example:

main.xml:

<?xml version="1.0"?>
<!DOCTYPE doc> 
<configuration xmlns:xi="http://www.w3.org/2001/XInclude" name="foo" version="0.1" >
 <xi:include href="./external.xml" />
</configuration>

external.xml:

<?xml version="1.0"?>
<!DOCTYPE doc [
       <!ENTITY bar "example">
       ]>
<objects>
   <object name="&bar;" />
</objects>

what I want to do is to declare bar inside main.xml, is it possible to do this? Thanks in advance, Matteo

mzjn
  • 48,958
  • 13
  • 128
  • 248
Matteo S.
  • 111
  • 5
  • I cannot think of a good solution. If you remove the entity declaration from external.xml, then the inclusion will fail. The included document is parsed before it is included, and unresolved entity references will produce errors. – mzjn Dec 15 '17 at 18:52
  • Hi @mzjn, thank you for your comment. I found a workaround. I collected all the different configurations in a new xml and wrapped them is some fictitious nodes, which I select through the xpointer functionality. If this could be of general interest, I can put an example here – Matteo S. Dec 16 '17 at 11:30
  • If you have a solution that solved the problem, I think you should post it as an answer. – mzjn Dec 18 '17 at 19:25

1 Answers1

0

This is not exactly the solution I was looking for, but kinda solved my problem:

main.xml

<?xml version="1.0"?>
<!DOCTYPE doc>
<configurations>
    <configuration>
    <xi:include href="./external/confs.xml" xpointer="xpointer(/ext/conf1)" />
    </configuration> 

    <configuration>
    <xi:include href="./external/confs.xml" xpointer="xpointer(/ext/conf2)" />
    </configuration> 
</configurations>

confs.xml

<?xml version="1.0"?>
<!DOCTYPE doc[
    <!ENTITY foo "bar">
]>
    <ext>
       <conf1>
           <inc name="apple_&foo;" />   
       </conf1>

       <conf2>
           <inc name="orange_&foo;" />
       </conf2>

    </ext> 
Matteo S.
  • 111
  • 5