Search code examples
iphonexmlxpathhpple

XPath XML query


I have trouble working with xml query. Below is the xml document. All I need to do is to display the text inside the cdata section. I wrote this query: /item/title/text(), but it crashed and doesn't work.

<title>
 <![CDATA[Envious at Envie! 50% off anything you want, anytime you want!]]>
</title>

I am using hpple library for iphone xcode. Here is my code:

    NSData *urlData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[@"http://www.twangoo.com/userfiles/rss/rss_hong-kong.xml" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

    TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:urlData];

    NSArray *title = [xpathParser search:@"//rss/channel/item[1]/title/text()"];

   TFHppleElement *element2 = [title objectAtIndex:0];

    NSString *titleString = [element2 content];

    NSLog(@"@%", titleString)

It returns nil.


Solution

  • If you could provide a bit more information as to your environment, language, which libraries use are using and the actual error message produced that would help a lot.

    However, I think a valid xslt statement in an xsl stylesheet would be

    ...

    <xsl:value-of select="/item/title"/>
    

    ...

    If you were programmatically accessing that data in PHP it would be something like:

    $doc = new DOMDocument;
    $doc->loadXML("<item><title>My name is Kevin</title></item>");
    $xpath = new DOMXPath($doc);
    $query = "/item/title";
    $entries = $xpath->query($query);
    foreach ($entries as $entry) { print $entry->nodeValue; }