简体   繁体   中英

Adding an attribute to every XML element in an XLIFF file

I want to add xml:space="preserve" to every element in Xliff files like this one:

<?xml version="1.0" encoding="utf-8"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">

<file datatype="plaintext" source-language="en-US" target-language="de-DE" date="2023-01-19T14:30:55Z" xml:space="preserve">
<body>

<trans-unit size-unit="char" approved="no" maxwidth="70" id="TITLE">
    <source>Add test scripts to execution queue</source>
    <target state="translated">Testskripte zur Ausführungs-Queue hinzufügen</target>
</trans-unit>
<trans-unit size-unit="char" approved="no">
    <source>Dynamic ID</source>
    <target state="translated">Dynamic-ID</target>
</trans-unit>
<trans-unit size-unit="char" approved="no" maxwidth="132">
    <source>Identification</source>
    <target state="translated">Identifikation</target>
</trans-unit>

</body>
</file>
</xliff>

The Python script I have looks like this, but also adds ns0: to the beginning of each element in the XML file.

import xml.etree.ElementTree as ET

tree = ET.parse("input.xlf")
root = tree.getroot()

for trans_unit in root.iter("{urn:oasis:names:tc:xliff:document:1.2}trans-unit"):
    trans_unit.attrib["{http://www.w3.org/XML/1998/namespace}space"] = "preserve"

tree.write("output.xlf")

Why does that happen and can someone help to improve the script?

OK, adding ET.register_namespace("", "urn:oasis:names:tc:xliff:document:1.2") and then also encoding='utf-8', xml_declaration=True) to the tree.write statement did the trick. Thanks, @ mzjn !

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