0

根据 GDML 手册,我可以包含另一个文件,如下所示 在此处输入图像描述

如何创建/添加 &materials; 使用 python 和 lxml 到元素?

4

1 回答 1

1

你应该可以使用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>
于 2020-08-28T18:23:14.580 回答