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

时间:2015-10-20 00:32:58

标签: xml xpath xml-parsing 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”标签的所有内容,我不想要。我只需要字符串。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:3)

这个xmllint命令

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

将产生此输出

getuserscount
setuserprivilege
hasprivilege

按要求。

相关问题