在xpath查询中使用变量..有可能吗?

时间:2010-06-27 16:36:18

标签: iphone xml parsing touchxml

有人可以告诉我如何在i-c的c-objective中使用XPath查询中的变量的text函数。我需要xml中提供的Engineering Library的信息

http://www.sis.pitt.edu/~arazeez/Librarydata.xml

NSString *libName = @"Engineering Library";
    NSMutableString  *xpathquery = [[NSMutableString alloc] init];
    [xpathquery appendString:@"//Library[LibraryName/text() = '"];
    [xpathquery appendString:libName];
    [xpathquery appendString:@"']/../Hours/TermOrHolidays"];
    resultNodes = [rssParser nodesForXPath:xpathquery error:nil];

或其他变体

NSString *string1 = @"//LibraryName[text() = '"; 
NSString *string2 = @"']/../Hours/TermOrHolidays"; 
NSString *newString; NSString *newString1; 
newString = [string1 stringByAppendingString:libName]; 
newString1 = [newString stringByAppendingString:string2]; 
NSLog(newString1);
 //correct one below //resultNodes = [rssParser nodesForXPath:@"//LibraryName[text()= 'Engineering Library']/../Hours/TermOrHolidays" error:nil]; 

resultNodes = [rssParser nodeForXPath:newString1 error:nil];

如果这种方法不对,有人可以告诉我如何获取工程库的数据。我不想直接使用这个词,但想通过变量使用它。

由于

2 个答案:

答案 0 :(得分:0)

如何使用这个xpath:

//Library[@name='Engineering Library']/Hours/TermOrHolidays

或者这个:

//Library[LibraryName='Engineering Library']/Hours/TermOrHolidays

答案 1 :(得分:0)

使用[NSString stringWithFormat:@“// LibraryName [text()='%@'] /../ Hours / TermOrHolidays”,someVariable]

构造字符串可能更容易

然后只要xpath查询没问题就行了。