[英]Two different XML namespaces with the same URL
我正在尝试使用 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”声明。 我再次认为这是由于它们具有相同的网址。 有没有什么办法解决这一问题。 任何帮助,将不胜感激。
http://www.loc.gov/mods/v3
是命名空间。 mods
只不过是一个缩写(又名“前缀”)。 您可以根据需要为 XML 文档中的同一名称空间使用任意多个不同的缩写。
例如:
<something xmlns="http://www.loc.gov/mods/v3">
<mods:something_else xmlns:mods="http://www.loc.gov/mods/v3" />
<blah:another_thing xmlns:blah="http://www.loc.gov/mods/v3" />
<last_thing />
</something>
和
<mods:something xmlns:mods="http://www.loc.gov/mods/v3" xmlns:blah="http://www.loc.gov/mods/v3">
<something_else xmlns="http://www.loc.gov/mods/v3" />
<mods:another_thing />
<blah:last_thing />
</mods:something>
和任意数量的其他组合代表完全相同的文档。
当它们被解析,然后再次序列化时,所有这些命名空间声明可以完全保持原样,或者它们可以折叠成一个,前缀可以重命名为ns0
,或者它可以变成默认命名空间 -没关系。 这完全取决于 XML 库的实现方式。
只要结果文档中的每个元素都在http://www.loc.gov/mods/v3
命名空间中,就任何相关指标而言,它都是同一个文档:
<something xmlns="http://www.loc.gov/mods/v3">
<something_else />
<another_thing />
<last_thing />
</something>
换句话说,没有什么坏掉的,所以没有什么需要修理的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.