2008年12月31日水曜日

XQueryをためす

こちら(XQuery Example)を参考に.

  NSXMLDocument* document= [NSXMLNode document];
[document setVersion:@"1.0"];
[document setCharacterEncoding:@"ISO-8859-1"];

NSXMLElement* bookstore= [NSXMLNode elementWithName:@"bookstore"];
[document setRootElement:bookstore];
{
NSXMLElement* book= [NSXMLNode elementWithName:@"book"];
[bookstore addChild:book];
[book addAttribute:[NSXMLNode attributeWithName:@"category"
stringValue:@"COOKING"]];
{
NSXMLElement* title= [NSXMLNode elementWithName:@"title"];
[book addChild:title];
[title addAttribute:[NSXMLNode attributeWithName:@"lang"
stringValue:@"en"]];
[title addChild:[NSXMLNode textWithStringValue:@"Everyday Italian"]];
}
{
NSXMLElement* author= [NSXMLNode elementWithName:@"author"];
[book addChild:author];
[author addChild:[NSXMLNode textWithStringValue:@"Giada De Laurentiis"]];
}
{
NSXMLElement* year= [NSXMLNode elementWithName:@"year"];
[book addChild:year];
[year addChild:[NSXMLNode textWithStringValue:@"2005"]];
}
{
NSXMLElement* price= [NSXMLNode elementWithName:@"price"];
[book addChild:price];
[price addChild:[NSXMLNode textWithStringValue:@"30.00"]];
}
}
{
NSXMLElement* book= [NSXMLNode elementWithName:@"book"];
[bookstore addChild:book];
[book addAttribute:[NSXMLNode attributeWithName:@"category"
stringValue:@"CHILDREN"]];
{
NSXMLElement* title= [NSXMLNode elementWithName:@"title"];
[book addChild:title];
[title addAttribute:[NSXMLNode attributeWithName:@"lang"
stringValue:@"en"]];
[title addChild:[NSXMLNode textWithStringValue:@"Harry Potter"]];
}
{
NSXMLElement* author= [NSXMLNode elementWithName:@"author"];
[book addChild:author];
[author addChild:[NSXMLNode textWithStringValue:@"J K. Rowling"]];
}
{
NSXMLElement* year= [NSXMLNode elementWithName:@"year"];
[book addChild:year];
[year addChild:[NSXMLNode textWithStringValue:@"2005"]];
}
{
NSXMLElement* price= [NSXMLNode elementWithName:@"price"];
[book addChild:price];
[price addChild:[NSXMLNode textWithStringValue:@"29.99"]];
}
}
{
NSXMLElement* book= [NSXMLNode elementWithName:@"book"];
[bookstore addChild:book];
[book addAttribute:[NSXMLNode attributeWithName:@"category"
stringValue:@"WEB"]];
{
NSXMLElement* title= [NSXMLNode elementWithName:@"title"];
[book addChild:title];
[title addAttribute:[NSXMLNode attributeWithName:@"lang"
stringValue:@"en"]];
[title addChild:[NSXMLNode textWithStringValue:@"XQuery Kick Start"]];
}
{
NSXMLElement* author= [NSXMLNode elementWithName:@"author"];
[book addChild:author];
[author addChild:[NSXMLNode textWithStringValue:@"James McGovern"]];
}
{
NSXMLElement* author= [NSXMLNode elementWithName:@"author"];
[book addChild:author];
[author addChild:[NSXMLNode textWithStringValue:@"Per Bothner"]];
}
{
NSXMLElement* author= [NSXMLNode elementWithName:@"author"];
[book addChild:author];
[author addChild:[NSXMLNode textWithStringValue:@"Kurt Cagle"]];
}
{
NSXMLElement* author= [NSXMLNode elementWithName:@"author"];
[book addChild:author];
[author addChild:[NSXMLNode textWithStringValue:@"James Linn"]];
}
{
NSXMLElement* author= [NSXMLNode elementWithName:@"author"];
[book addChild:author];
[author addChild:[NSXMLNode textWithStringValue:@"Vaidyanathan Nagarajan"]];
}
{
NSXMLElement* year= [NSXMLNode elementWithName:@"year"];
[book addChild:year];
[year addChild:[NSXMLNode textWithStringValue:@"2003"]];
}
{
NSXMLElement* price= [NSXMLNode elementWithName:@"price"];
[book addChild:price];
[price addChild:[NSXMLNode textWithStringValue:@"49.99"]];
}
}
{
NSXMLElement* book= [NSXMLNode elementWithName:@"book"];
[bookstore addChild:book];
[book addAttribute:[NSXMLNode attributeWithName:@"category"
stringValue:@"WEB"]];
{
NSXMLElement* title= [NSXMLNode elementWithName:@"title"];
[book addChild:title];
[title addAttribute:[NSXMLNode attributeWithName:@"lang"
stringValue:@"en"]];
[title addChild:[NSXMLNode textWithStringValue:@"Learning XML"]];
}
{
NSXMLElement* author= [NSXMLNode elementWithName:@"author"];
[book addChild:author];
[author addChild:[NSXMLNode textWithStringValue:@"Erik T. Ray"]];
}
{
NSXMLElement* year= [NSXMLNode elementWithName:@"year"];
[book addChild:year];
[year addChild:[NSXMLNode textWithStringValue:@"2003"]];
}
{
NSXMLElement* price= [NSXMLNode elementWithName:@"price"];
[book addChild:price];
[price addChild:[NSXMLNode textWithStringValue:@"39.95"]];
}
}

NSString* output= [document XMLStringWithOptions:(NSXMLNodePrettyPrint|NSXMLNodeCompactEmptyElement)];
NSLog(@"%@", output);

NSArray *nodes= [bookstore objectsForXQuery:@"/bookstore/book[price<30]" 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>
> 1
> <book category="CHILDREN"><title lang="en">Harry Potter</title><author>J K. Rowling</author><year>2005</year><price>29.99</price></book>


これだけだとXPathと差がないな.

0 件のコメント: