简体   繁体   中英

Writing a xml:id attribute with lxml

I'm trying to rebuild a TEI-XML file with lxml.

The beginning of my file looks like this:

<?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 --> >

The first four lines should not matter too much in my opinion, but I included them for completeness. My problem starts with the TEI-Element. So my code to copy this looks like this:

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"

The String xml_id is assigned at some point before and does not matter for my question. So my codes returns me this line:

<TEI xmlns:xi="http://www.w3.org/2001/XInclude"
     n=""
     id="StAAG_U-17_0006"
     xmlns="http://www.tei-c.org/ns/1.0">

So the only thing that is missing is this xml:id attribute. I found this specification page: https://www.w3.org/TR/xml-id/ and I know it is mentioned to be supported in lxml in its FAQ .

Btw, root.attrib["xml:id"] does not work, as it is not a viable attribute name.

So, does anyone know how I can assign my id to an elemnt's xml:id attribute?

You need to specify that id is part of the default xml namespace. Try this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM