Reputation: 1
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
Upvotes: 0
Views: 311
Reputation: 2959
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
Upvotes: 0
Reputation: 243479
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.
Upvotes: 2