xmlxpathxmlstarlet

xmlstarlet select sibling based on text value


I've been trying to get through this with different options and couldn't find the correct way of doing it. Here is the xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
  <types>
        <members>PriceRule__c.All</members>
        <members>PriceRule__c.None</members>
        <members>ProductRule__c.All</members>
        <members>Quote__c.All_Quotes</members>
        <members>SummaryVariable__c.All</members>
        <name>ListView</name>
  </types>
  <types>
        <members>*</members>
        <name>AnalyticSnapshot</name>
  </types>
</Package>

I want to be able to get the text of the "name" node that is in the same "types" node for any "members" node that starts with PriceRule__c. Here is the furthest I could get which was to actually find the values of those nodes:

echo $(xmlstarlet sel -N x="http://soap.sforce.com/2006/04/metadata" -t -v "//x:types/*[starts-with(text(),'PriceRule__c')]" test.xml)

This prints out:

PriceRule__c.All PriceRule__c.None

But what I need is a way to get the value ListView Any idea how the XPath should be to get that? Thank you in advance!


Solution

  • Change your XPath to

    //x:types[starts-with(x:members,'PriceRule__c')]/x:name
    

    and you will select the x:name with value, ListView, as requested.