我需要在HTML文档中搜索可可中的两个特定文本字符串。我正在创建一个网页:Page Example的NSXMLDocument,然后我试图搜索它的应用程序标题和图标的网址。我现在使用这个代码来搜索特定的字符串:
NSString *xpathQueryStringTitle = @"//div[@id='desktopContentBlockId']/div[@id='content']/div[@class='padder']/div[@id='title' @class='intro has-gcbadge']/h1";
NSString *xpathQueryStringIcon = @"//div[@id='desktopContentBlockId']/div[@id='content']/div[@class='padder']/div[@id='left-stack']/div[@class='lockup product application']/a";
NSArray *titleItemsNodes = [document nodesForXPath:xpathQueryStringTitle error:&error];
if (error)
{
[[NSAlert alertWithError:error] runModal];
return;
}
error = nil;
NSArray *iconItemsNodes = [document nodesForXPath:xpathQueryStringIcon error:&error];
if (error)
{
[[NSAlert alertWithError:error] runModal];
return;
}当我尝试搜索这些字符串时,我得到错误:"XQueryError:3 - "invalid token (@) - ./*/div@id='desktopContentBlockId'/div@id='content'/div@class='padder'/div@id='title‘@class='intro -gcbadge’/h1“,第1行:”
我正在松散地关注这个tutorial。
我还尝试了在xPath中不使用所有@符号的情况下,它也返回了一个错误。对于xPath,我的语法显然是错误的。此路径的基本语法是什么。我见过很多使用基本XML树的示例,但不是html。
发布于 2011-11-09 03:48:52
我猜想这就是你对两个属性进行测试的地方
/div[@id='title' @class='intro has-gcbadge']/h1";尝试将其更改为:
/div[@id='title'][@class='intro has-gcbadge']/h1";发布于 2011-11-09 13:12:23
OP的其他问题(来自评论):
,但我需要修改返回的字符串。对于第一个字符串,我得到了
"<h1>App Title</h1>,为了只得到<h1>中的文本,我应该添加什么?
使用的
/div[@id='title' and @class='intro has-gcbadge']/h1/text()或使用:
string(/div[@id='title' and @class='intro has-gcbadge']/h1)在第二个字符串中,I get
<img width="111" src="link">如何从src标记返回Link值?
使用的
YorSecond-Not-Shown-Expression/@src或使用:
string(YorSecond-Not-Shown-Expression/@src)https://stackoverflow.com/questions/8055817
复制相似问题