views:

50

answers:

2

Could some one please tell me how do we use the text function in the XPath query. I needed the information for Hillman Library present in the xml

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

resultNodes = [rssParser nodesForXPath:@"//Library[1]/Hours/TermOrHolidays" error:nil];.

for now I used the [1]. But I wanted to use the text function

NSString *libName = @"Hillman Library";
resultNodes = [rssParser nodesForXPath:@"//Library[LibraryName/text() = libName]/Hours/TermOrHolidays" error:nil];

But this is not working out. Could some one please let me know how I go about doing this.

Thanks

+2  A: 

But I wanted to use the text function

 NSString *libName = @"Hillman Library";  
 resultNodes = [rssParser nodesForXPath:@"//Library[LibraryName/text() = libName]/Hours/TermOrHolidays" error:nil]; 

But this is not working out

The problem is that:

//Library[LibraryName/text() = libName]/Hours/TermOrHolidays

selects all Library elements in the document one of the text-node children of which is equal to one of the libName children of that same Library.

The Library elements in the referenced XML document don't have any libName children, and this is why the above XPath expression selects nothing.

You want:

//Library[LibraryName/text() = 'Hillman Library']/Hours/TermOrHolidays

or

//Library[LibraryName/text() = "Hillman Library"]/Hours/TermOrHolidays

Either quotes or apostrophes can be used.

In an XPath expression a string is specified by surrounding it with quotes or apostrophes. XPath doesn't understand the variables of its hosting language.

Dimitre Novatchev
A: 

Ah..thanks for that Dimitre... It worked out. Like you said XPath doesnt understand the variable of its hosting language. So can I concatenate and form an XPath query and then use it.

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

I am new to C-objective and still struggling with simple conceptsenter code here

raqz
I tried using this as well but in vain. Getting some error.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];
Abdul Raqeeb