1

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.

kevin
  • 129
  • 2
  • 11
  • kirill is saying correctly.once post your header of xml – ajay Jul 25 '11 at 08:42
  • I don't know your framework, but XPath from your code `//rss/channel/item[1]/title/text()` should retrieve `Envious at Envie! 50% off anything you want, anytime you want!` – Kirill Polishchuk Jul 25 '11 at 09:02

2 Answers2

0

Use this XPath: /item/title/text()

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
0

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; }
Ryan
  • 614
  • 4
  • 11
  • Sorry, I didn't see the IPhone tag, can't help you with this one as I am completely unfamiliar with that syntax – Ryan Jul 25 '11 at 09:16