Question

I'm new to touchxml and xpath and i just want to know if is there any way to get an attribute of a node dynamically? I have this xml

<?xml version="1.0" encoding="UTF-8"?>
<countries>
   <country id="Philippines">
      <countryname>Philippines</countryname>
      <subsidiaries>
          <subsidiary>
              <name>Sartorius Philippines Inc.</name>
              <address>Unit 20-A The World Centre Building, 330 Senator Gil Puyat Avenue Makati 1209 City Philippines, Philippines</address>
              <phone>+63.2.8640.929</phone>
              <fax>+63.28640.932</fax>
              <email>enquiry.philippines@sartorius.com</email>
              <website>http://www.sartorius-mechatronics.com.ph</website>
          </subsidiary>
     </subsidiaries>
  </country>
  <country id="Denmark">
       <countryname>Denmark</countryname>
       <subsidiaries>
          <subsidiary>
              <name>Sartorius Stedim Nordic A|S</name>
              <address>stedim Hoerskaetten 6d 2630 Taastrup, Denmark</address>
              <phone>+45.7023.4400</phone>
              <fax>+45.4630.4030</fax>
              <email>ne.customersupport@sartorius.com</email>
              <website></website>
         </subsidiary>
         <subsidiary>
              <name>Sartorius Nordic A|S</name>
              <address>Hoerskaetten 6D 2630 Taastrup, Denmark</address>
              <phone>+45.7023.4400</phone>
              <fax>+45.4630.4030</fax>
              <email>ne.customersupport@sartorius.com</email>
              <website></website>
       </subsidiary>
     </subsidiaries>
    </country>
</countries>`

I have a table view that shows all the country name and what I would like to happen is when I click the word denmark it will show the with an attribute of id="denmark"

I have this xpath query as for now:

resultNodes = [rssParser2 nodesForXPath:@"/countries/country[@id]/subsidiaries/subsidiary" error:nil];
Was it helpful?

Solution

I'm not 100% sure of the best way to concatenate strings in Objective-C, but the idea is, assuming you have a variable countryId with the id you're looking for, you would do something like this:

NSString *xpath =
    [NSString stringWithFormat:@"%@/%@/%@",
         @"/countries/country[@id = '", countryId, @"']/subsidiaries/subsidiary"];
resultNodes = [rssParser2 nodesForXPath:xpath error:nil];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top