RaptureXML を使用して ePub ドキュメント内のファイルを解析しようとしています。XML ドキュメントを子から子へトラバースできますが、XPath 経由で要素を取得しようとすると、要素が返されません。
XML (container.xml) は次のとおりです。
<?xml version="1.0"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
ここに私の解析コードがあります:
NSString *containerPath = [path stringByAppendingPathComponent:@"META-INF/container.xml"];
NSData *containerData = [NSData dataWithContentsOfFile:containerPath];
RXMLElement *containerRoot = [RXMLElement elementFromXMLData:containerData];
NSLog(@"root %@ ", [containerRoot tag]);
RXMLElement *rootfiles = [containerRoot child:@"rootfiles"];
NSLog(@"root %@ ", [rootfiles tag]);
RXMLElement *rootfile = [rootfiles child:@"rootfile"];
NSLog(@"root %@ ", [rootfile tag]);
NSLog(@"attr %@ ", [rootfile attribute:@"full-path"]);
[containerRoot
iterateWithRootXPath:@"//rootfile"
usingBlock:^(RXMLElement *elem) {
NSLog(@"path=%@", [elem attribute:@"full-path"]);
}];
/* I've also tried this.... */
[rootfiles
iterateWithRootXPath:@"//rootfile"
usingBlock:^(RXMLElement *elem) {
NSLog(@"path=%@", [elem attribute:@"full-path"]);
}];
この出力は次のとおりです。
2013-01-14 22:03:43.299 MLReader[9027:c07] root container
2013-01-14 22:03:43.299 MLReader[9027:c07] root rootfiles
2013-01-14 22:03:43.300 MLReader[9027:c07] root rootfile
2013-01-14 22:03:43.300 MLReader[9027:c07] attr content.opf
ご覧のとおり、子から子へと移動すると、探している rootfile 要素に到達できます。ただし、 iterateWithRootXPath:usingBlock: 内のブロックには決して入りません。
私は Java で XML/XPath をたくさんやったことがありますが、Objective-C/iOS は初めてです。