我有一些XML(例如文件minimal.xml),其中包含以下格式的错误和警告消息:
<?xml version="1.0" encoding="UTF-8"?>
<messages>
<message subMessage="RSC-004">RSC-004, ERROR, [File 'OEBPS/Text/pdfMigration.html' could not be decrypted.], epub20_encryption_binary_content.epub</message>
<message subMessage="RSC-012">RSC-012, ERROR, [Fragment identifier is not defined.], OEBPS/toc.ncx (24-67)</message>
<message subMessage="RSC-012">RSC-012, ERROR, [Fragment identifier is not defined.], OEBPS/toc.ncx (30-82)</message>
<message subMessage="RSC-012">RSC-012, ERROR, [Fragment identifier is not defined.], OEBPS/toc.ncx (36-81)</message>
<message subMessage="RSC-012">RSC-012, ERROR, [Fragment identifier is not defined.], OEBPS/toc.ncx (42-75)</message>
<message subMessage="RSC-012">RSC-012, ERROR, [Fragment identifier is not defined.], OEBPS/toc.ncx (48-61)</message>
<message subMessage="HTM-023">HTM-023, WARN, [An invalid XHTML Named Entity was found: '&0;'.], OEBPS/Text/pdfMigration.html (18-199)</message>
<message subMessage="HTM-023">HTM-023, WARN, [An invalid XHTML Named Entity was found: '&l0xb'.], OEBPS/Text/pdfMigration.html (291-6)</message>
</messages>
我正在寻找一种方法来提取所有表示ERROR的消息元素的subMessage属性值(可以从消息元素的文本值中存在ERROR的方式识别出来)。我正在使用xmlstarlet。经过一番搜索后,我发现了this somewhat similar case,因此我将其修改为:
xmlstarlet sel -t -v '/messages[contains(message,"ERROR")]/message/@subMessage' minimal.xml
结果:
RSC-004
RSC-012
RSC-012
RSC-012
RSC-012
RSC-012
HTM-023
HTM-023
这不是我所期望的,因为这些是所有消息元素的subMessage值!作为进一步的测试,我修改了查询以仅提取警告:
xmlstarlet sel -t -v '/messages[contains(message,"WARN")]/message/@subMessage' minimal.xml
在这种情况下,结果为空!我是xmlstarlet的新手,我怀疑我在这里忽略了一些明显的问题。任何帮助,不胜感激!
顺便说一句我正在使用的xmlstarlet版本的一些信息:
针对libxml2 2.9.2编译,链接为20903
针对libxslt 1.1.28编译,链接为10128
最佳答案
试试这个
xmlstarlet sel -t -v '/messages/message[contains(.,"ERROR")]/@subMessage' minimal.xml
使用
/messages[contains(message,"WARN")]
您错误地尝试检查messages
元素而不是每个message
元素的内容。