【发布时间】:2020-07-13 16:37:47
【问题描述】:
我正在尝试使用 python 中的 xml 元素树库进行一些数据清理。
我的 xml 输入文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<mods:mods xmlns:mods="http://www.loc.gov/mods/v3" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.loc.gov/mods/v3" version="3.5" xsi:schemaLocation="http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-5.xsd">
<mods:titleInfo>
<mods:title>1971, Human Events</mods:title>
</mods:titleInfo>
<mods:name type="personal" authority="naf" valueURI="https://lccn.loc.gov/n88172648">
<mods:namePart>Kellems, Vivien, 1896-1975</mods:namePart>
<mods:role>
<mods:roleTerm authority="marcrelator" authorityURI="http://id.loc.gov/vocabulary/relators" valueURI="http://id.loc.gov/vocabulary/relators/col" type="text">Collector</mods:roleTerm>
</mods:role>
</mods:name>
<mods:typeOfResource>text</mods:typeOfResource>
<mods:genre authority="aat" valueURI="300111999">publications (documents)</mods:genre>
<mods:originInfo>
<mods:dateIssued encoding="w3cdtf" keyDate="yes">1971</mods:dateIssued>
</mods:originInfo>
<mods:physicalDescription>
<mods:digitalOrigin>reformatted digital</mods:digitalOrigin>
<mods:internetMediaType>image/jp2</mods:internetMediaType>
</mods:physicalDescription>
<mods:note type="ownership">Archives & Special Collections at the Thomas J. Dodd Research Center, University of Connecticut Library</mods:note>
<mods:identifier type="local">1992-0033/SeriesIII:Activism/SubseriesA:PoliticalCampaigns/Box138:6</mods:identifier>
<mods:identifier type="local">MSS 1992.0033</mods:identifier>
<mods:identifier type="local">39153030468468</mods:identifier>
<mods:accessCondition type="use and reproduction">In Copyright</mods:accessCondition>
<mods:recordInfo>
<mods:recordContentSource>University of Connecticut Library</mods:recordContentSource>
<mods:recordCreationDate encoding="w3cdtf">2018-07-09-04:00</mods:recordCreationDate>
<mods:languageOfCataloging>
<mods:languageTerm authority="iso639-2b" type="code">eng</mods:languageTerm>
</mods:languageOfCataloging>
</mods:recordInfo>
<mods:note type="source note">Vivien Kellems Papers</mods:note>
<mods:note type="source identifier">MSS 1992.0033</mods:note>
<identifier type="hdl">http://hdl.handle.net/11134/20002:860633493</identifier>
</mods:mods>
我所要做的就是将最后的标识符标签更改为与其余标签具有相同的前缀,即“mods”前缀。并将特定的 hlink 属性添加到 accessCondition 标记。我已经成功地完成了这两件事。但是在我将这些修改写回文件并尝试使用 xml 元素树解析器后,我收到以下错误:
xml.etree.ElementTree.ParseError: unbound prefix: line 25, column 2
现在我认为这是一个命名空间问题,因为“xmlns:mods”命名空间和“xmlns”命名空间具有相同的 url,所以当我将命名空间注册到解析器时,如下所示:
ET.register_namespace('', "http://www.loc.gov/mods/v3")
ET.register_namespace('mods', "http://www.loc.gov/mods/v3")
ET.register_namespace('xlink', "http://www.w3.org/1999/xlink")
ET.register_namespace('xsi', "http://www.w3.org/2001/XMLSchema-instance")
当我写回 xml 文件时,它还会删除其中一个命名空间,命名空间声明如下所示:
<mods:mods xmlns:mods="http://www.loc.gov/mods/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.5" xsi:schemaLocation="http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-5.xsd">
即“xmlns”声明。仅显示“xmlns:mods”声明。我再次认为这是由于它们具有相同的 url。有没有什么办法解决这一问题。任何帮助将不胜感激。
【问题讨论】:
-
就个人而言,我会避免使用
etree使用for循环和if逻辑直接在Python 中处理XML 转换,尤其是使用命名空间。相反,我会使用 Python 的第三方lxml库来运行 XSLT。见demo。
标签: python xml database data-cleaning