【发布时间】:2019-05-14 15:44:01
【问题描述】:
使用 xml2 包我想从一个 XML 文件开始编写两个 XML 文件 - 一个用于语言。例如,我有以下 XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<title xml:lang="it">Title IT</title>
<title xml:lang="en">Title EN</title>
<author>Author</author>
</book>
</books>
我想保存以下两个文件:
FILE IT
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<title xml:lang="it">Title IT</title>
<author>Author</author>
</book>
</books>
FILE EN
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<title xml:lang="en">Title EN</title>
<author>Author</author>
</book>
</books>
如果我这样做:
txt <- "<books><book><title xml:lang='it'>Title IT</title><title xml:lang='en'>Title EN</title><author>Author</author></book></books>"
XML <- xml2::read_xml(txt)
it <- xml2::xml_find_all(XML, "//*[@xml:lang = 'it']")
en <- xml2::xml_find_all(XML, "//*[@xml:lang = 'en']")
XML_orig <- XML
xml2::xml_remove(en)
xml2::write_xml(XML, file = "book_it.xml")
XML <- XML_orig
xml2::xml_remove(it)
xml2::write_xml(XML, file = "book_en.xml")
当我创建XML 对象的副本时,它会继续引用原始对象。有没有办法在不引用的情况下创建新副本?或者您知道使用 xml2 库解决问题的更好方法吗?
谢谢!
【问题讨论】: