gpt4 book ai didi

python - 写一个 xml :id attribute with lxml

转载 作者:太空宇宙 更新时间:2023-11-04 02:40:30 24 4
gpt4 key购买 nike

我正在尝试用 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 --> >

在我看来,前四行应该无关紧要,但为了完整起见,我将它们包括在内。我的问题始于 TEI-Element。所以我复制它的代码如下所示:

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_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 属性。我找到了这个规范页面:https://www.w3.org/TR/xml-id/我知道它在其 FAQ 中被提到在 lxml 中受支持.

顺便说一句,root.attrib["xml:id"] 不起作用,因为它不是一个可行的属性名称。

那么,有人知道如何将我的 id 分配给元素的 xml:id 属性吗?

最佳答案

您需要指定 id 是默认 xml 命名空间的一部分。试试这个:

root.attrib["{http://www.w3.org/XML/1998/namespace}id"] = xml_id

引用:https://www.w3.org/TR/xml-names/#ns-decl

关于python - 写一个 xml :id attribute with lxml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46673171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com