Reputation: 21
For the following XML snippet -
<response uri="http://campaigns.zoho.com/api/recentsentcampaigns" version="1">
<status>success</status>
<code>0</code>
<recent-sent-campaigns>
<campaign no="1">
<fl val="campaign_key">somekeyvalue</fl>
<fl val="sent_date_string">08 Jun 2015, 11:55 PM</fl>
<fl val="sent_time">1433850936000</fl>
<fl val="campaign_name">Test for Mailing List</fl>
<fl val="created_date_string">08 Jun 2015, 10:38 PM</fl>
<fl val="campaigntype">normal</fl><fl val="created_time">1433846306000</fl>
</campaign>
</recent-sent-campaigns>
I want to get the campaign_key text by specifying the campaign_name text. I know how to return the campaign_key text by specifying the campaign no (in this case, "1") but I need to be able to pass the campaign_name as an argument (in this case, "Test for Mailing List") and return the key (in this case, "somekeyvalue ").
Upvotes: 2
Views: 35
Reputation: 26153
I would not relied on the exact knowledge of the xml structure. We can find the relevant fl and take fl campaign_key
under the same parent
//fl[@val="campaign_name" and .="Test for Mailing List"]/../fl[@val="campaign_key"]/text()
Upvotes: 0
Reputation: 22617
To retrieve a campaign key based on its name, use
/response/recent-sent-campaigns/campaign[fl[@val = 'campaign_name'] = 'Test for Mailing List']/fl[@val = 'campaign_key']
which means
/response select the `response` element
/recent-sent-campaigns select its children `recent-sent-campaigns`
/campaign select its children `campagin`
[fl[@val = 'campaign_name'] but only if there is a child element called `fl` which has an
attribute called `val` whose value is "campaign_name"
= 'Test for Mailing List'] and only if the text content of that `fl` element is "Test for
Mailing List"
/fl[@val = 'campaign_key'] of any elements satisfying this condition, select their
children `fl` where there is an attribute `val` whose value is
equal to "campaign_key"
and yields as a result:
<fl val="campaign_key">somekeyvalue</fl>
if you meant to only return the text content of this element, append /text()
at the end:
/response/recent-sent-campaigns/campaign[fl[@val = 'campaign_name'] = 'Test for Mailing List']/fl[@val = 'campaign_key']/text()
and the result will be
somekeyvalue
Upvotes: 1