XML 具有多个 DOCTYPE 声明

XML with multiple DOCTYPE declarations

您好,我有一个相当大的 XML 文件 10-15gb。它包含多个根 Doctype 标签,我的猜测是制作它的人只是将一堆单独的文件连接在一起。这绝对不是最佳实践,但有时这就是您必须使用的全部内容。我想知道是否有人有解析文件或将文件分成每个单独的 DocType 的解决方案。

到目前为止,我已经尝试将整个文件包装在一个根标签中,但这没有用。我在 Python.

工作

如有任何解决方案或意见,我们将不胜感激。


<?xml version="1.0" ?>
<!DOCTYPE pmc-articleset PUBLIC "-//NLM//DTD ARTICLE SET 2.0//EN" "https://dtd.nlm.nih.gov/ncbi/pmc/articleset/nlm-articleset-2.0.dtd">

<pmc-articleset><article xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" article-type="research-article">
  <?properties open_access?>
  <front>
    <p>
    Apple
    </p>
  </front>
</article>
</pmc-articleset>
<?xml version="1.0" ?>
<!DOCTYPE pmc-articleset PUBLIC "-//NLM//DTD ARTICLE SET 2.0//EN" "https://dtd.nlm.nih.gov/ncbi/pmc/articleset/nlm-articleset-2.0.dtd">
<pmc-articleset><article xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" article-type="research-article">
  <?properties open_access?>
  <front>
    <p>
    Banana
    </p>
  </front>
</article>
</pmc-articleset>

  


可以使用 csplit(1) 将文件拆分为多个部分,这是该任务的实用程序。

在 XML 声明处 <?xml ...

csplit -z --prefix output_file --suffix-format '%02d.xml' your_large.xml '/^<[?]xml[ ]/' {*}

或者,如果没有重复,在 <!DOCTYPE

csplit -z --prefix output_file --suffix-format '%02d.xml' your_large.xml '/<!DOCTYPE/' {*}

这将导致 output_file00.xmloutput_file01.xml

如果您的输入文档序言实际上包含多个文档类型声明(多个 DOCTYPE),或者看起来没有文档元素,那么它很可能是完整的 SGML 而不是 XML。尽管您的示例代码两者都没有。