使用 xmllint 从 XML 文件中提取属性

Extracting attributes from XML file using xmllint

这是我的一部分 XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<nlpcmp>
 <word string="getuserscount" occurrences="2">
  <systems>
   <system name="mks">
    <pos file="IrcChannel.cpp" part="adjective" line="414" occ="2"/>
   </system>
  </systems>
 </word>
 <word string="setuserprivilege" occurrences="2" >
  <systems>
   <system name="mks">
    <pos file="IrcChannel.cpp" part="adjective" line="375" occ="2"/>
   </system>
  </systems>
 </word>
 <word string="hasprivilege" occurrences="2" >
  <systems>
   <system name="mks">
    <pos file="IrcChannel.cpp" part="verb" line="360" occ="2"/>
   </system>
  </systems>
 </word>
 <word string="partmessage" occurrences="2" >
  <systems>
   <system name="TEST">
    <pos file="IrcChannel.cpp" part="verb" line="308" occ="2"/>
   </system>
  </systems>
 </word>
</nlpcmp>

我正在尝试仅使用 xmllint 查找名为 mks 的系统的字符串列表

输出应该给我:

getuserscount
setuserprivilege
hasprivilege

所以我尝试了这个命令:

xmllint --xpath 'string(//nlpcmp/word/@name)' file.xml

没有输出,然后我试了:

xmllint --xpath "//*[local-name()='word'][system/@name[mks]" file.xml

它说: XPath 错误:无效谓词 xmlXPathEval:评估失败

然后我试了

xmllint --xpath "//*[local-name()='word']" file.xml

它给了我 "word" 标签的所有内容,这是我不想要的。我只需要字符串。我怎样才能做到这一点?

这个 xmllint 命令

xmllint --xpath "/nlpcmp/word[systems/system/@name = 'mks']/@string" file.xml

将产生此输出

getuserscount
setuserprivilege
hasprivilege

根据要求。