首页 > 解决方案 > 具有多个 DOCTYPE 声明的 XML

问题描述

您好,我有一个相当大的 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>

  


标签: pythonxmlparsingformattingdoctype

解决方案


可以使用 将文件拆分为多个部分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.xml,output_file01.xml等。


推荐阅读