Select node by its text value in xmlstarlet

后端 未结 2 1263
借酒劲吻你
借酒劲吻你 2021-01-05 10:19

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



        
相关标签:
2条回答
  • 2021-01-05 10:50

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

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

    Or, in xmlstarlet:

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

    Will return WatchingLiveTV as requested.

    0 讨论(0)
  • 2021-01-05 10:50

    I was able to find that node using the following XPath:

    /FrontendStatus/State/String[Value = 'WatchingLiveTV']/Value
    

    Which will return:

    <Value>WatchingLiveTV</Value>
    

    Note you could also use:

    //String[Value = 'WatchingLiveTV']/Value
    

    Which is slightly smaller.

    To select the Value element and parent/siblings, you could use:

    //String[Value = 'WatchingLiveTV']
    

    Which returns:

    <String>
      <Key>state</Key>
      <Value>WatchingLiveTV</Value>
    </String>
    

    Edit

    I just re-read your original question. You would like to select the XML based on the value of the Key node. You can do this using the above, but changing the predicate from Value to Key:

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

    @kjhughes has put this into the syntax format you're after.

    I hope that helps.

    0 讨论(0)
提交回复
热议问题