GDML 的创建包括使用 python 和 lxml

Creation of GDML include using python and lxml

根据 GDML 手册,我可以包含另一个文件,如下所示

我怎样才能create/add &materials;使用 python 和 lxml?

到元素

您应该可以使用 etree.Entity...

from lxml import etree

docString = '<!DOCTYPE gdml [\n<!ENTITY materials SYSTEM "materials.xml">\n]>'

NS = "http://www.w3.org/2001/XMLSchema-instance"
location_attribute = f"{{{NS}}}noNameSpaceSchemaLocation"
gdml = etree.Element("gdml",
                     attrib={location_attribute:
                             "blahblahblah/gdml.xsd"})

ent_ref = etree.Entity("materials")

gdml.append(ent_ref)

print(etree.tostring(gdml, doctype=docString).decode())

打印...

<!DOCTYPE gdml [
<!ENTITY materials SYSTEM "materials.xml">
]>
<gdml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:noNameSpaceSchemaLocation="blahblahblah/gdml.xsd">&materials;</gdml>