我正在尝试提取“值”节点的值,其中“键”节点是 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>
如果我直接引用节点,我可以提取值:
$ xmlstarlet sel -t -m '/FrontendStatus[1]/State[1]/String[31]' -v Value <status.xml
WatchingLiveTV
但我想通过'Key'节点的值来选择它
最佳答案
此 XPath 将根据 Key
等于 state
选择 Value
的 State
:
/FrontendStatus/State/String[Key='state']/Value
或者,在 xmlstarlet 中:
$ xmlstarlet sel -t -m "/FrontendStatus/State/String[Key='state']" -v Value <status.xml
将按要求返回WatchingLiveTV
。
关于xml - 通过 xmlstarlet 中的文本值选择节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28755179/