我正在使用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 protected]</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 protected]</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 protected]</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 protected]</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 protected]</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
我找到的解决方案如下:
xmlstarlet ed -d "//wp:author[wp:author_id != '35290']" file.xml > out.xml
仅从XML文件中获取一个片段并不足以提供完整的答案。我将此示例数据包装在根标记中:
<root xmlns:wp="some.url">
...
</root
然后,您可以提供XPath表达式来查找您正在查找的节点:所有“wp:author”节点包含具有特定值的“wp:author_id”子节点。
$ 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 protected]</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>
我发现this page of XPath examples很有帮助