【问题标题】:Two different XML namespaces with the same URL具有相同 URL 的两个不同 XML 命名空间
【发布时间】: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 &amp; 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


【解决方案1】:

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>

换句话说,没有任何损坏,因此无需修复。

【讨论】:

    猜你喜欢
    • 2011-04-10
    • 2014-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 2010-11-12
    • 2014-09-27
    相关资源
    最近更新 更多