我正在处理wordpress xml转储,无论出于何种原因,wordpress都已将数据库中的每个用户导出为每个帖子的“作者”。为了使xml文件更易于使用,我想删除除一个以外的所有作者节点。
这是我所拥有的一个例子:
<rss version="2.0" xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:wp="http://wordpress.org/export/1.2/">
<wp:author>
<wp:author_id>35622</wp:author_id>
<wp:author_login>some_username_1</wp:author_login>
<wp:author_email>email@address.com</wp:author_email>
<wp:author_display_name><![CDATA[some_username_1]]></wp:author_display_name>
<wp:author_first_name><![CDATA[]]></wp:author_first_name>
<wp:author_last_name><![CDATA[]]></wp:author_last_name>
</wp:author>
<wp:author>
<wp:author_id>35290</wp:author_id>
<wp:author_login>my_unique_username</wp:author_login>
<wp:author_email>email@address.com</wp:author_email>
<wp:author_display_name><![CDATA[my_unique_username]]></wp:author_display_name>
<wp:author_first_name><![CDATA[]]></wp:author_first_name>
<wp:author_last_name><![CDATA[]]></wp:author_last_name>
</wp:author>
<wp:author>
<wp:author_id>35289</wp:author_id>
<wp:author_login>some_username_2</wp:author_login>
<wp:author_email>email@address.com</wp:author_email>
<wp:author_display_name><![CDATA[some_username_2]]></wp:author_display_name>
<wp:author_first_name><![CDATA[]]></wp:author_first_name>
<wp:author_last_name><![CDATA[]]></wp:author_last_name>
</wp:author>
<wp:author>
<wp:author_id>33404</wp:author_id>
<wp:author_login>some_username_3</wp:author_login>
<wp:author_email>email@address.com</wp:author_email>
<wp:author_display_name><![CDATA[some_username_3]]></wp:author_display_name>
<wp:author_first_name><![CDATA[]]></wp:author_first_name>
<wp:author_last_name><![CDATA[]]></wp:author_last_name>
</wp:author>
要再添加几千个条目
我想删除除此节点之外的所有节点:
<wp:author>
<wp:author_id>35290</wp:author_id>
<wp:author_login>my_unique_username</wp:author_login>
<wp:author_email>email@address.com</wp:author_email>
<wp:author_display_name><![CDATA[my_unique_username]]></wp:author_display_name>
<wp:author_first_name><![CDATA[]]></wp:author_first_name>
<wp:author_last_name><![CDATA[]]></wp:author_last_name>
</wp:author>
尝试在shell脚本中执行此操作,但是我真的不确定从哪里开始,因为我以前从未使用过xmlstarlet,所以希望获得帮助。
已更新以反映我发现的数据根和解决方案:
xmlstarlet ed -d "//wp:author[wp:author_id != '35290']" file.xml > out.xml
答案 0 :(得分:1)
我找到的解决方案如下:
xmlstarlet ed -d "//wp:author[wp:author_id != '35290']" file.xml > out.xml
答案 1 :(得分:0)
仅从XML文件中摘录并不能真正为我们提供完整的答案。我将此示例数据包装在根标签中:
<root xmlns:wp="some.url">
...
</root
然后,您可以提供XPath表达式来查找您要查找的节点:所有包含“ wp:author_id”子项并具有特定值的“ wp:author”节点。
$ xmlstarlet sel -t -c '//wp:author[wp:author_id = "35289"]' file.xml
<wp:author xmlns:wp="some.url">
<wp:author_id>35289</wp:author_id>
<wp:author_login>some_username_2</wp:author_login>
<wp:author_email>email@address.com</wp:author_email>
<wp:author_display_name>some_username_2</wp:author_display_name>
<wp:author_first_name></wp:author_first_name>
<wp:author_last_name></wp:author_last_name>
</wp:author>