Python 使用lxml编写xml:id属性
我正在尝试用lxml重建TEI-XML文件 我的文件的开头如下所示:Python 使用lxml编写xml:id属性,python,xml,lxml,w3c,Python,Xml,Lxml,W3c,我正在尝试用lxml重建TEI-XML文件 我的文件的开头如下所示: <?xml version="1.0" encoding="UTF-8"?> <?xml-model href="https://www.ssrq-sds-fds.ch/tei/TEI_Schema_SSRQ.rng" type="application/xml" schematypens="http://relaxng.org/ns/structure/1
<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="https://www.ssrq-sds-fds.ch/tei/TEI_Schema_SSRQ.rng"
type="application/xml"
schematypens="http://relaxng.org/ns/structure/1.0"?>
<?xml-model href="https://www.ssrq-sds-fds.ch/tei/TEI_Schema_SSRQ.rng"
type="application/xml"
schematypens="http://purl.oclc.org/dsdl/schematron"?>
<?xml-stylesheet type="text/css"
href="https://www.ssrq-sds-fds.ch/tei/Textkritik_Version_tei-ssrq.css"?>
<TEI xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns="http://www.tei-c.org/ns/1.0" n=""
xml:id="[To be generated]" <!-- e.g. StAAG_U-17_0007a --> >
NSMAP = {"xml":"http://www.tei-c.org/ns/1.0",
"xi":"http://www.w3.org/2001/XInclude"}
root = et.Element('TEI', n="", nsmap=NSMAP)
root.attrib["id"] = xml_id
root.attrib["xmlns"] = "http://www.tei-c.org/ns/1.0"
字符串xml\u id
是在之前的某个时间分配的,与我的问题无关。因此,我的代码返回这一行:
<TEI xmlns:xi="http://www.w3.org/2001/XInclude"
n=""
id="StAAG_U-17_0006"
xmlns="http://www.tei-c.org/ns/1.0">
所以唯一缺少的就是这个xml:id
属性。我发现了这个规范页面:我知道在its中提到lxml支持它
顺便说一句,root.attrib[“xml:id”]
不起作用,因为它不是一个可行的属性名
那么,有人知道如何将id分配给元素的
xml:id
属性吗?您需要指定id
是默认xml
命名空间的一部分。试试这个:
root.attrib["{http://www.w3.org/XML/1998/namespace}id"] = xml_id
参考: