views:

51

answers:

2

Could some one please tell me how do we use the text function with variable in the XPath query in c-objective for iphone. I needed the information for Engineering Library present in the xml

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];

or another variant

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];

If this method is not right, could some one please tell me how to fetch the data for Engineering Library. I dont want to use the word directly but want to use it through a variable.

Thanks

A: 

how about using this xpath:

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

or this one:

//Library[LibraryName='Engineering Library']/Hours/TermOrHolidays
jenningj
thank you jenningj....but i need to use a variable instead of the actual value by itself
raqz
A: 

It might be easier to construct the string using [NSString stringWithFormat:@"//LibraryName[text() = '%@']/../Hours/TermOrHolidays", someVariable]

Then as long as the xpath query is okay it will work.

Matt Delves
I init a variableNSString s* = [NSString stringWithFormat:@"//LibraryName[text() = '%@']/../Hours/TermOrHolidays", libName];I tried using this string variable s in the XPath query but I get an error, countByEnumeratingWithState:objects:count:]Kindly let me know what could be done.ThanksRaqeeb
raqz
I'm not too familiar with xpath. What would be beneficial is to log the output of the string and make sure that it is being created as you expect. Also, what object is calling the countByEnumeratingWithState:objects:count message?
Matt Delves