具有多个DOCTYPE声明的XML

2024-04-28 21:27:00 发布

您现在位置:Python中文网/ 问答频道 /正文

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

  



Tags: 文件标记orghttpwwwarticlenlm解决方案
2条回答

可以使用^{}将文件拆分为多个部分,这是任务的实用工具

或者在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/' {*}

这将导致{}、{}等

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

相关问题 更多 >