title: PHP and XML finding a node based on an attribute to find another attribute or the index of the node
body: <p>I'm trying to optimize a little php app I wrote for parsing YouTube profiles. Enter a YouTube user name and the app returns a simple listing of the number of videos uploaded, favorites, subscribers and so on by parsing the XML returned by a gdata query for the account profile.
  For example this XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt; 
  &lt;entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:yt="http://gdata.youtube.com/schemas/2007" gd:etag="W/"C0EHSX47eCp7I2A9Wh5aEU4.""&gt; 
  &lt;id&gt;tag:youtube.com,2008:user:H5m_qmnr3dHOO8x7m7dtvw&lt;/id&gt; 
  &lt;published&gt;2006-06-15T22:59:11.000Z&lt;/published&gt; 
  &lt;updated&gt;2014-01-07T21:53:58.000Z&lt;/updated&gt; 
  &lt;category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#userProfile" /&gt; 
  &lt;category scheme="http://gdata.youtube.com/schemas/2007/channeltypes.cat" term="DIRECTOR" /&gt; 
  &lt;title&gt;epontius&lt;/title&gt; 
  &lt;summary&gt;channel page of epontius&lt;/summary&gt; 
  &lt;link rel="alternate" type="text/html" href="https://www.youtube.com/channel/UCH5m_qmnr3dHOO8x7m7dtvw" /&gt; 
  &lt;link rel="self" type="application/atom+xml" href="https://gdata.youtube.com/feeds/api/users/H5m_qmnr3dHOO8x7m7dtvw?v=2" /&gt; 
   &lt;author&gt; 
  &lt;name&gt;epontius&lt;/name&gt; 
  &lt;uri&gt;https://gdata.youtube.com/feeds/api/users/epontius&lt;/uri&gt; 
  &lt;yt:userId&gt;H5m_qmnr3dHOO8x7m7dtvw&lt;/yt:userId&gt; 
  &lt;/author&gt; 
  &lt;yt:channelId&gt;UCH5m_qmnr3dHOO8x7m7dtvw&lt;/yt:channelId&gt; 
  &lt;gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.subscriptions" href="https://gdata.youtube.com/feeds/api/users/epontius/subscriptions?v=2" countHint="161" /&gt; 
  &lt;gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.liveevent" href="https://gdata.youtube.com/feeds/api/users/epontius/live/events?v=2" countHint="0" /&gt; 
  &lt;gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.favorites" href="https://gdata.youtube.com/feeds/api/users/epontius/favorites?v=2" countHint="73" /&gt; 
  &lt;gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.contacts" href="https://gdata.youtube.com/feeds/api/users/epontius/contacts?v=2" countHint="184" /&gt; 
  &lt;gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.inbox" href="https://gdata.youtube.com/feeds/api/users/epontius/inbox?v=2" /&gt; 
  &lt;gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.playlists" href="https://gdata.youtube.com/feeds/api/users/epontius/playlists?v=2" /&gt; 
  &lt;gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.uploads" href="https://gdata.youtube.com/feeds/api/users/epontius/uploads?v=2" countHint="26" /&gt; 
  &lt;gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.newsubscriptionvideos" href="https://gdata.youtube.com/feeds/api/users/epontius/newsubscriptionvideos?v=2" /&gt; 
  &lt;gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.recentactivity" href="https://gdata.youtube.com/feeds/api/users/epontius/events?v=2" /&gt; 
  &lt;yt:googlePlusUserId&gt;105085469892080308187&lt;/yt:googlePlusUserId&gt; 
  &lt;yt:location&gt;US&lt;/yt:location&gt; 
  &lt;yt:statistics lastWebAccess="1970-01-01T00:00:00.000Z" subscriberCount="245" videoWatchCount="0" viewCount="0" totalUploadViews="68815" /&gt; 
  &lt;media:thumbnail url="https://yt3.ggpht.com/-N_Et9Qg1APc/AAAAAAAAAAI/AAAAAAAAAAA/VIuQ_GuzA0Q/s88-c-k-no/photo.jpg" /&gt; 
  &lt;yt:userId&gt;H5m_qmnr3dHOO8x7m7dtvw&lt;/yt:userId&gt; 
  &lt;yt:username display="epontius"&gt;epontius&lt;/yt:username&gt; 
  &lt;/entry&gt;
  </code></pre>
  
  <p>The app as it stands works fine, but every so often YouTube changes the order of some elements or adds/removes items in the  lines, so the app returns incorrect data as I am accessing the 'countHint' attribute.
  For example:</p>
  
  <pre><code>$uploadscount = $gd-&gt;feedLink[6]-&gt;attributes();
  $uploads = $uploadscount['countHint'];
  echo 'Number of uploads: ' . '&lt;span class="datatext"&gt;' . $uploads . '&lt;/span&gt;' . '&lt;br /&gt;';
  </code></pre>
  
  <p>Which would return 26 in this case. But if the number or order of the feedLink lines changes, I'd get incorrect information or an error since the index number of the feedLink is hard coded.
  Each feedLink seems to have a unique rel= attribute and I was hoping to be able to use xpath and some sort of loop like a foreach to search for a specific rel value (ie. rel = "<a href="http://gdata.youtube.com/schemas/2007#user.uploads" rel="nofollow">http://gdata.youtube.com/schemas/2007#user.uploads</a>" ) and then be able to grab its countHint attribute value to assign it to a variable or at least grab its node index number (ie 6 in the case of the uploads) to then access the appropriate countHint attribute. And then repeat this for each of the feedLink lines and attributes for the data I want to grab.
  That way it will be more accurate and dynamic in the event these feedLink lines are modified.
  I just can't get my head around how to do it. The feedLink elements are empty elements in a different namespace (gd) and there are multiples which makes using xpath kind of confusing for me. I keep returning empty values and getting lost.
  Any suggestions would be appreciated.</p>
  
  <p>Ok. Think I'm getting somewhere thanks to suggestions.</p>
  
  <pre><code>  foreach ($gd-&gt;feedLink as $feedLink) {
             $attributes = $feedLink-&gt;attributes();
              if (strpos($attributes['rel'], '#user.uploads')) {
               $uploads = $attributes['countHint'];
               }
  
              elseif (strpos($attributes['rel'], '#user.favorites')) {
               $favs = $attributes['countHint'];
                }
  
              elseif (strpos($attributes['rel'], '#user.subscriptions')) {
               $subscriptions = $attributes['countHint'];
                }
  
              elseif (strpos($attributes['rel'], '#user.liveevent')) {
               $liveevents = $attributes['countHint'];
               }
  
             elseif (strpos($attributes['rel'], '#user.contacts')) {
              $friends = $attributes['countHint'];
               }
            }
  </code></pre>
  
  <p>That will return the proper values I'm looking for, but I'm worried now that I'm doing extra processing doing the loop, since I would assume each loop tests each line regardless of whether it has already found that value in a previous loop? </p>