通过xmlstarlet中的文本值选择节点 [英] Select node by its text value in xmlstarlet

查看:187
本文介绍了通过xmlstarlet中的文本值选择节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提取bash shell中键"节点为状态"的值"节点的值:

I am trying to extract the value of the 'Value' node, where the 'Key' node is 'state' within a bash shell:

<FrontendStatus xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" serializerVersion="1.1">
<script/>
 <State>
  <String>
   ...
   ...
  </String>
  <String>
   ...
   ...
  </String>
  <String>
   <Key>state</Key>
   <Value>WatchingLiveTV</Value>
  </String>
  <String>
   <Key>studiolevels</Key>
   <Value>1</Value>
  </String>
  <String>
   ...
   ...
  </String>
  <String>
   ...
   ...
  </String>
 </State>
</FrontendStatus>

如果直接引用节点,则可以提取值:

I can extract the value if I reference the node directly:

$ xmlstarlet sel -t -m '/FrontendStatus[1]/State[1]/String[31]' -v Value <status.xml
WatchingLiveTV

但是我想通过'Key'节点的值来选择它

But I would like to select it by the value of the 'Key' node instead

推荐答案

此XPath将基于StateKey等于state来选择StateValue:

This XPath will select Value of a State based on its Key equalling state:

/FrontendStatus/State/String[Key='state']/Value

或者,在xmlstarlet中:

Or, in xmlstarlet:

$ xmlstarlet sel -t -m "/FrontendStatus/State/String[Key='state']" -v Value <status.xml

将按要求返回WatchingLiveTV.

这篇关于通过xmlstarlet中的文本值选择节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆