2008年12月31日水曜日

XQueryをためす 2 (FLWOR表現)

こちら(XQuery FLWOR Expressions)を参考に.

XML(document, bookstore)の生成はこちら

  NSString* output= [document XMLStringWithOptions:(NSXMLNodePrettyPrint|NSXMLNodeCompactEmptyElement)];
NSLog(@"%@", output);
{
NSArray *nodes= [bookstore objectsForXQuery:@"/bookstore/book[price>30]/title" error:NULL];
NSLog(@"%d", [nodes count]);
int i;
for(i= 0;i<[nodes count];i++) {
NSLog(@"%@", [[nodes objectAtIndex:i] XMLString]);
}
}
{
NSString *query= @"for $x in /bookstore/book where $x/price>30 order by $x/title return $x/title";
NSArray *nodes= [bookstore objectsForXQuery:query error:NULL];
NSLog(@"%d", [nodes count]);
int i;
for(i= 0;i<[nodes count];i++) {
NSLog(@"%@", [[nodes objectAtIndex:i] XMLString]);
}
}


結果
> <?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
> 2
> <title lang="en">XQuery Kick Start</title>
> <title lang="en">Learning XML</title>
> 2
> <title lang="en">Learning XML</title>
> <title lang="en">XQuery Kick Start</title>

0 件のコメント: