title: How to get 1 attribute from XPath in Objective-C
body: <p>I'm using the GDataXML lib for XPath and this tutorial: <a href="http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml" rel="nofollow">http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml</a></p>
  
  <p>His xml looks like this:</p>
  
  <pre><code>&lt;Party&gt;
    &lt;Player&gt;
        &lt;Name&gt;Butch&lt;/Name&gt;
        &lt;Level&gt;1&lt;/Level&gt;
        &lt;Class&gt;Fighter&lt;/Class&gt;
        &lt;HP&gt;20&lt;/HP&gt;
        &lt;MaxHP&gt;20&lt;/MaxHP&gt;
    &lt;/Player&gt;
    &lt;Player&gt;
        &lt;Name&gt;Shadow&lt;/Name&gt;
        &lt;Level&gt;2&lt;/Level&gt;
        &lt;Class&gt;Rogue&lt;/Class&gt;
        &lt;HP&gt;15&lt;/HP&gt;
        &lt;MaxHP&gt;15&lt;/MaxHP&gt;
    &lt;/Player&gt;
    &lt;Player&gt;
        &lt;Name&gt;Crak&lt;/Name&gt;
        &lt;Level&gt;3&lt;/Level&gt;
        &lt;Class&gt;Wizard&lt;/Class&gt;
        &lt;HP&gt;10&lt;/HP&gt;
        &lt;MaxHP&gt;10&lt;/MaxHP&gt;
    &lt;/Player&gt;
  </code></pre>
  
  <p></p>
  
  <p>But what if there is an attribute in the Name tag, like this:</p>
  
  <pre><code>&lt;Player&gt;
      &lt;Name color="red"&gt;Butch&lt;/Name&gt; // Color attribute 
      &lt;Level&gt;1&lt;/Level&gt;
      &lt;Class&gt;Fighter&lt;/Class&gt;
      &lt;HP&gt;20&lt;/HP&gt;
      &lt;MaxHP&gt;20&lt;/MaxHP&gt;
  &lt;/Player&gt;
  </code></pre>
  
  <p>If you look in the tutorial he got this:</p>
  
  <pre><code>Party *party = [[[Party alloc] init] autorelease];
  NSArray *partyMembers = [doc.rootElement elementsForName:@"Player"];
  for (GDataXMLElement *partyMember in partyMembers) {
  
  // Let's fill these in!
  NSString *name;
  int level;
  RPGClass rpgClass;
  
  // Name
  NSArray *names = [partyMember elementsForName:@"Name"];
  if (names.count &gt; 0) {
      GDataXMLElement *firstName = (GDataXMLElement *) [names objectAtIndex:0];
      name = firstName.stringValue;
  } else continue;
  ...etc
  </code></pre>
  
  <p>He's using a for loop with all the partyMembers in it, and gets each tag, but what if a tag got an attribute like the color one I explained before?</p>
  
  <p>I've tried this:</p>
  
  <pre><code>    NSArray *colors = [partyMember nodesForXPath:@"string(/Player/Name/@color)"   error:nil] ;
      if (colors != nil &amp;&amp; colors.count &gt; 0) {
          GDataXMLElement *firstColor = (GDataXMLElement *) [colors objectAtIndex:0];
          NSString *color = firstColor.stringValue;
          NSLog(@"%@",color);
      }
  </code></pre>
  
  <p>But this does nothing. Any idea what I'm doing wrong?</p>
  
  <p><strong>EDIT:</strong>
  This works:</p>
  
  <pre><code>        NSArray *colors = [partyMember nodesForXPath:@"//Player/Name/@color" error:nil] ;
      if (colors != nil &amp;&amp; colors.count &gt; 0) {
          GDataXMLElement *firstColor = (GDataXMLElement *) [colors objectAtIndex:0];
          NSString *color = firstColor.stringValue;
          NSLog(@"%@",color);
      }
  </code></pre>
  
  <p>But if you got more color tags like this:</p>
  
  <pre><code>    &lt;Player&gt;
      &lt;Name color="red"&gt;Butch&lt;/Name&gt; // Color attribute 
      &lt;Level&gt;1&lt;/Level&gt;
      &lt;Class&gt;Fighter&lt;/Class&gt;
      &lt;HP&gt;20&lt;/HP&gt;
      &lt;MaxHP&gt;20&lt;/MaxHP&gt;
  &lt;/Player&gt;
      &lt;Player&gt;
      &lt;Name color="red"&gt;Butch&lt;/Name&gt; // Color attribute 
      &lt;Level&gt;1&lt;/Level&gt;
      &lt;Class&gt;Fighter&lt;/Class&gt;
      &lt;HP&gt;20&lt;/HP&gt;
      &lt;MaxHP&gt;20&lt;/MaxHP&gt;
  &lt;/Player&gt;
      &lt;Player&gt;
      &lt;Name color="red"&gt;Butch&lt;/Name&gt; // Color attribute 
      &lt;Level&gt;1&lt;/Level&gt;
      &lt;Class&gt;Fighter&lt;/Class&gt;
      &lt;HP&gt;20&lt;/HP&gt;
      &lt;MaxHP&gt;20&lt;/MaxHP&gt;
  &lt;/Player&gt;
  </code></pre>
  
  <p>Then it will print out 3 times red, and I only need it from the first window, then the second iteration in the for loop the second one and so one.</p>
  
  <p>So I was thinking of this:</p>
  
  <pre><code>        NSArray *colors = [partyMember nodesForXPath:@"//Player[0]/Name/@color" error:nil] ;
      if (colors != nil &amp;&amp; colors.count &gt; 0) {
          GDataXMLElement *firstColor = (GDataXMLElement *) [colors objectAtIndex:0];
          NSString *color = firstColor.stringValue;
          NSLog(@"%@",color);
      }
  </code></pre>
  
  <p>I added the Player[0] and later on I need to change it with the iteration number of the for loop but this does not work.</p>