xmllint提取没有xpath的值

时间:2019-03-20 15:01:34

标签: xml-parsing xmllint

我需要验证xml中元素的xml文本值,如下所示,到目前为止,我已经获得了所有dmdindex:field的值,但是我只需要最后一个 drep.rightsFacet 结果输出。请记住,我拥有的xmllint版本没有xpath,因此我不得不求助于xmllint --shell。任何帮助表示赞赏。

这是xml文件片段:

<?xml version="1.0" encoding="UTF-8"?>
<dmdindex:dmdindex xmlns:dmdindex="http://www.example.com/example/dmdindex/"
                   xmlns:functx="http://www.functx.com"
                   xmlns:xs="http://www.w3.org/2001/XMLSchema"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <dmdindex:record>
      <dmdindex:field name="drep.yearStart">1881</dmdindex:field>
      <dmdindex:field name="drep.pubConcat">[Detroit : Parke, Davis &amp; Co., 1881?]</dmdindex:field>
      <dmdindex:field name="drep.rights">blahblahblah</dmdindex:field>
      <dmdindex:field name="drep.rightsLink">https://creativecommons.org/publicdomain/mark/1.0/</dmdindex:field>
      <dmdindex:field name="drep.rightsFacet">Public domain</dmdindex:field>
   </dmdindex:record>
</dmdindex:dmdindex>

这是我使用的命令

echo "cat //*[local-name()='dmdindex']/*[local-name()='record']/*[local-name()='field']" | xmllint --shell example.xml | sed '/^\/ >/d' | sed 's/<[^>]*.//g'

返回了

 -------
Philadelphia : Sunshine Press, [1897]
 -------
blahblahblah
 -------
https://creativecommons.org/publicdomain/mark/1.0/
 -------
Public domain

我只需要获取“公共域”的值,而忽略其余部分。谢谢!

1 个答案:

答案 0 :(得分:1)

您可以在xpath中添加谓词以测试Windows Push Notifications System Service User Profile Service User Manager Themes Task Scheduler System Event Notificiation Service Shell hardware Detection Remote Desktop Configuration Network Setup Service IP Helper IKE and AuthIP IPsec Keying Modules Group Policy Client Geolocation Service Certificate Propagation Application Information 属性的值...

name