title: Is this XPath query on parsing XHTML wrong? using TouchXML
body: <p>I have been trying to parse a <code>XHTML</code> doc via <code>TouchXML</code>, but it always can't find any tags via <code>XPath query</code>.</p>
  
  <p>Below is the XHTML:</p>
  
  <pre><code>XHTML &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
     &lt;head&gt;
        &lt;meta name="generator" content=
           "HTML Tidy for Mac OS X (vers 25 March 2009), see www.w3.org" /&gt;
        &lt;title&gt;&lt;/title&gt;
        &lt;/head&gt;
     &lt;body&gt;
        &lt;p&gt;
            &lt;a href="http://www.flickr.com/photos/55397648@N00/5987335786/"
               title="casavermeer5.jpg by the style files, on Flickr"&gt;
            &lt;img src="http://farm7.static.flickr.com/6127/5987335786_abec990554_o.jpg"
                 width="500" height="750" border="0" alt="casavermeer5.jpg" /&gt;
            &lt;/a&gt;
        &lt;/p&gt;
     &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>So, we can see there are a "p" tag, "a" tag and "img" tag</p>
  
  <p>What I did then is shown as the code below:</p>
  
  <pre><code>CXHTMLDocument *doc = [[[CXHTMLDocument alloc] initWithXHTMLString:XHTML options:0 error:&amp;error] autorelease];
  NSLog(@"error %@", [error localizedDescription]);
  NSLog(@"doc children count = %d", [doc childCount]);
  NSArray *imgNodeArray = [doc nodesForXPath:@"//img" error:&amp;error];
  NSLog(@"imgNodeArray = %d", [imgNodeArray count]);
  NSLog(@"error %@", [error localizedDescription]);
  </code></pre>
  
  <p>The results are </p>
  
  <pre><code>error (null)
  doc children count = 2
  imgNodeArray = 0
  error (null)
  </code></pre>
  
  <p>So, there are no error at all in parsing the <code>XHTML</code> doc and no error for the <code>XPath query</code>. Also this doc has two children under the root ("body" tag and "head" tag). But the problem is it cannot find the "img" tag. I have tried to replace "img" with other possible tag names (such as p, a, even body, head), no luck at all.</p>
  
  <p>Can someone help me here?</p>
  
  <p>P.S.</p>
  
  <p>Actually the original doc is a HTML, I have used CTidy class in TouchXML lib to tidy the HTML to XHTML first. The XHTML above came from that CTidy results.</p>
  
  <p>I also tried to add a namespace thing to the XPath query, like this</p>
  
  <pre><code>NSMutableDictionary *namespaceDict = [NSMutableDictionary dictionary];
  [namespaceDict setValue:@"http://www.w3.org/1999/xhtml" forKey:@"xhtml"];
  </code></pre>
  
  <p>And change the XPath query to</p>
  
  <pre><code>NSArray *imgNodeArray = [doc nodesForXPath:@"//xhtml:img" namespaceMappings:namespaceDict error:&amp;error];
  </code></pre>
  
  <p>Still no luck, can't find any results.</p>