如何解决具有多个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>
解决方法
可以使用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
等
如果输入文档的序言实际上包含多个文档类型声明(多个DOCTYPE),或者似乎没有文档元素,那么它很可能是完整的SGML而不是XML。尽管您的示例代码都没有。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。