如何从与其他字符串共享相同标记的XML文件中提取特定字符串?

时间:2017-04-04 20:39:58

标签: xml linux xpath xmlstarlet

在以下XML代码段中

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xml" href="/static/atom.xsl"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:s="http://dev.splunk.com/ns/rest" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
  <title>serverclients</title>
  <id>https://splfwdprw2:8089/servicesNS/nobody/search/deployment/server/clients</id>
  <updated>2017-04-04T16:14:04-04:00</updated>
  <generator build="f3e41e4b37b2" version="6.3.1"/>
  <author>
    <name>Splunk</name>
  </author>
  <link href="/servicesNS/nobody/search/deployment/server/clients/_acl" rel="_acl"/>
  <link href="/servicesNS/nobody/search/deployment/server/clients/countClients_by_machineType" rel="countClients_by_machineType"/>
  <link href="/servicesNS/nobody/search/deployment/server/clients/countRecentDownloads" rel="countRecentDownloads"/>
  <link href="/servicesNS/nobody/search/deployment/server/clients/getMatchingAppsForClient_dryRun" rel="getMatchingAppsForClient_dryRun"/>
  <link href="/servicesNS/nobody/search/deployment/server/clients/preview" rel="preview"/>
  <opensearch:totalResults>1</opensearch:totalResults>
  <opensearch:itemsPerPage>18446744073709551615</opensearch:itemsPerPage>
  <opensearch:startIndex>0</opensearch:startIndex>
  <s:messages/>
  <entry>
    <title>00031e8f6c883544b8079037c5eba9ec</title>
    <id>https://splfwdprw2:8089/servicesNS/nobody/search/deployment/server/clients/00031e8f6c883544b8079037c5eba9ec</id>
    <updated>2017-04-04T16:14:04-04:00</updated>
    <link href="/servicesNS/nobody/search/deployment/server/clients/00031e8f6c883544b8079037c5eba9ec" rel="alternate"/>
    <author>
      <name>system</name>
    </author>
    <link href="/servicesNS/nobody/search/deployment/server/clients/00031e8f6c883544b8079037c5eba9ec" rel="list"/>
    <link href="/servicesNS/nobody/search/deployment/server/clients/00031e8f6c883544b8079037c5eba9ec" rel="remove"/>
    <content type="text/xml">
      <s:dict>
        <s:key name="applications">
          <s:dict>
            <s:key name="all_deploymentclient">
              <s:dict>
                <s:key name="action">Phonehome</s:key>
                <s:key name="archive">/opt/splunk/var/run/tmp/all_deploymentclient/all_deploymentclient-1491320471.bundle</s:key>
                <s:key name="checksum">0</s:key>
                <s:key name="excludeFromUpdate"></s:key>
                <s:key name="failedReason"></s:key>
                <s:key name="issueReload">0</s:key>
                <s:key name="restartSplunkWeb">0</s:key>
                <s:key name="restartSplunkd">1</s:key>
                <s:key name="result">Ok</s:key>
                <s:key name="serverclasses">
                  <s:list>
                    <s:item>all_deploymentclient</s:item>
                  </s:list>
                </s:key>
                <s:key name="size">10240</s:key>
                <s:key name="stateOnClient">enabled</s:key>
                <s:key name="timestamp">Tue Apr  4 11:42:54 2017</s:key>
              </s:dict>
            </s:key>
            <s:key name="all_fwd_outputs_18indexers">
              <s:dict>
                <s:key name="action">Phonehome</s:key>
                <s:key name="archive">/opt/splunk/var/run/tmp/all_fwd/all_fwd_outputs_18indexers-1491320471.bundle</s:key>
                <s:key name="checksum">0</s:key>
                <s:key name="excludeFromUpdate"></s:key>
                <s:key name="failedReason"></s:key>
                <s:key name="issueReload">0</s:key>
                <s:key name="restartSplunkWeb">0</s:key>
                <s:key name="restartSplunkd">1</s:key>
                <s:key name="result">Ok</s:key>
                <s:key name="serverclasses">
                  <s:list>
                    <s:item>all_fwd</s:item>
                  </s:list>
                </s:key>
                <s:key name="size">10240</s:key>
                <s:key name="stateOnClient">enabled</s:key>
                <s:key name="timestamp">Tue Apr  4 11:42:54 2017</s:key>
              </s:dict>
            </s:key>

...我正在尝试提取任何“s:key name =”字符串,这些字符串出现在“s:key name =”applications“标记下面的第一级。在这个例子中,我正在寻找的字符串要提取的是“all_deploymentclient”和“all_fwd_outputs_18indexers”。如果其他字符串出现在同一级别,我也想捕获它们。

我正在使用xml_grep,但我不确定如何定义参数以获得所需的结果(因为有多个“s:key name =”标记的实例,其中一些服务作为一种标题,以及分配给它们的其他标题)。

所以,当所有的说法和完成时,这个例子的提取输出应该是:

all_deploymentclient
all_fwd_outputs_18indexers

我怎样才能做到这一点?是否需要另一个实用程序(如xpath)?

3 个答案:

答案 0 :(得分:1)

也许首先尝试这个(快速而脏的方法来替换s:名称空间前缀):

cat /var/tmp/content.xml | sed 's/s://g' > cat /var/tmp/content2.xml 

然后尝试

xmllint  --xpath "//key[@name='all_deploymentclient' or @name='all_fwd_outputs_18indexers']/@name" /var/tmp/content2.xml \
| sed "s| name|\nname|g; s/name=\"//; s/\"\$//"

答案 1 :(得分:1)

当您对任何类型的数据源进行任何类型的搜索时,它不足以知道实际数据是什么(如果您知道,您不需要搜索它):您需要要知道它与所示示例的不同之处。

因此,我们必须查看您对问题的描述:&#34; s:key name =&#34;在&#34; s之后的缩进中出现的字符串:key name =&#34; applications&#34;标记&#34;,并试着理解你的意思。

  • 通过缩进,您是字面意思是分页布局,还是这是您讨论XML数据模型的树结构的方式?

  • 当你说&#34;&#34;之后,我们是否将这个(基于你的例子)解释为含义&#34;第一个后代&#34;元素,也就是我们在树木漫步中遇到的第一个匹配的后代?

我们可以假设这些&#34;第一个后代&#34;将始终是从原点节点下降两级(即孙子?)。如果是这样,XPath解决方案就是

//s:key[@name="applications"]/*/*/@name

但如果&#34;第一个后代&#34;可能处于不同的深度,然后变得更加困难,解决方案也可能取决于您使用的XPath版本。所以我们需要更多信息。

我不知道xml_grep能够做什么。

答案 2 :(得分:0)

在考虑了@MichaelKay和@knb提供的信息后,我能够确定一个解决方案。我最终使用xmlstarlet来获取我需要的信息,如下所示:

xmlstarlet sel -T -t -m "//*[local-name()='key'][@name='applications']/*/*/@name" -v . -n <XML filename>

这产生了以下输出:

all_deploymentclient
all_fwd_outputs_18indexers

感谢大家的贡献!