Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用lxml编写xml:id属性_Python_Xml_Lxml_W3c - Fatal编程技术网

Python 使用lxml编写xml:id属性

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

我正在尝试用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.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
参考: