我想使用touchXML解析ios中的xhtml文件。我正在嘗試第一次。我在XHTML中有以下格式:在iOS中使用touchXML的XPath問題
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" lang="en">
<head>
<meta http-equiv="default-style" content="text/html; charset=utf-8"/>
<title>Contents</title>
<link rel="stylesheet" href="css/igp-widget-world.css" type="text/css"/>
</head>
<body>
<nav epub:type="toc"><h2>Contents</h2>
<ol epub:type="list">
<li><a href="s001-BookTitlePage-01.xhtml">Widget World</a></li>
<li><a href="s002-Copyright-01.xhtml">Copyright</a></li>
<li><a href="s003-TableOfContents-01.xhtml">Contents</a></li>
<li><a href="s004-Introduction-01.xhtml">Introduction</a></li>
<li><a href="s005-Part-001.xhtml">PART 1: EVENTS AND COMMANDS</a></li>
<li><a href="s006-Topic-001.xhtml">1: Timeline Events</a></li>
<li><a href="s007-Topic-002.xhtml">2: Counter Events</a></li>
<li><a href="s008-Topic-003.xhtml">3: Sprite Events</a></li>
<li><a href="s009-Part-002.xhtml">PART 2: QUESTIONS AND ANSWERS</a></li>
<li><a href="s010-Topic-004.xhtml">4: QAA True-False</a></li>
<li><a href="s011-Topic-005.xhtml">5: QAA True-False Multi</a></li>
<li><a href="s012-Topic-006.xhtml">6: QAA-Multichoice</a></li>
<li><a href="s013-Topic-007.xhtml">7: QAA Multi-Response</a></li>
<li><a href="s014-Topic-008.xhtml">8: QAA Association</a></li>
<li><a href="s015-Topic-009.xhtml">9: QAA Sequence</a></li>
<li><a href="s016-Topic-010.xhtml">10: QAA-Textmatch</a></li>
<li><a href="s017-Topic-011.xhtml">11: QAA-Textmatch Multi</a></li>
<li><a href="s018-Topic-012.xhtml">12: QAA Sort Word</a></li>
<li><a href="s019-Topic-013.xhtml">13: QAA Set</a></li>
<li><a href="s020-Part-003.xhtml">PART 3: INTERACTIVE WIDGETS</a></li>
<li><a href="s021-Topic-014.xhtml">14: Widgets: Horizontal Sliding Panel</a></li>
<li><a href="s022-Topic-015.xhtml">15: Widgets: Vertical Sliding Panel</a></li>
<li><a href="s023-Topic-016.xhtml">16: Widgets: Horizontal Tutorial Panel</a></li>
<li><a href="s024-Topic-017.xhtml">17: Widgets: Vertical Tutorial Panel</a></li>
<li><a href="s025-Topic-018.xhtml">18: Widgets: Vertical Scrolling Panel</a></li>
<li><a href="s026-Topic-019.xhtml">19: Widgets: Horizontal Scrolling Panel</a></li>
<li><a href="s027-Topic-020.xhtml">20: Widgets: XY Scrolling Panel</a></li>
<li><a href="s028-Topic-021.xhtml">21: Widgets: Locked Panel</a></li>
<li><a href="s029-Topic-022.xhtml">22: Widgets: Popups</a></li>
<li><a href="s030-Topic-023.xhtml">23: Widgets: Reveal</a></li>
<li><a href="s031-Topic-024.xhtml">24: Widgets: PopUp Panel</a></li>
<li><a href="s032-Colophon-01.xhtml">Colophon</a></li>
</ol>
</nav>
</body>
</html>
這是我的xhtml文件。我實現如下:
CXMLDocument* xmlDoc = [[CXMLDocument alloc] initWithContentsOfURL:myURL options:0 error:nil];
NSString* xpath = [NSString stringWithFormat:@"//html:a[contains(@href,'%@')]/../html:a", myValue];
NSArray* navPoints = [ncxToc nodesForXPath:xpath namespaceMappings:[NSDictionary dictionaryWithObject:@"http://www.w3.org/1999/xhtml/" forKey:@"xhtml"] error:nil];
我試圖找到<a>
其中<a href =
我的價值的價值。
我不知道我在哪裏錯了錯誤的是:
XPath error : Undefined namespace prefix
XPath error : Invalid expression
比我改變了的XPath如下:
NSString* xpath = [NSString stringWithFormat:@"//html/a[contains(@href,'%@')]/../html/a", myValue];
:
取代/
。它不會像上面那樣給我提供錯誤,但也不會獲得我的內容。
我不知道XPATH。在XML中,我只知道NSXMLParser
。請幫我確定這是什麼問題?
更新
隨着我到現在我更新了我的代碼,做了如下的回答:
NSString* xpath = [NSString stringWithFormat:@"//html:a[@href = '%@']", href];
NSArray* navPoints = [ncxToc nodesForXPath:xpath namespaceMappings:[NSDictionary dictionaryWithObject:@"http://www.w3.org/1999/xhtml/" forKey:@"html"] error:nil];
和
NSString* xpath = [NSString stringWithFormat:@"//xhtml:a[@href = '%@']", href];
NSArray* navPoints = [ncxToc nodesForXPath:xpath namespaceMappings:[NSDictionary dictionaryWithObject:@"http://www.w3.org/1999/xhtml/" forKey:@"xhtml"] error:nil];
沒有錯誤,但我得到任何數組中的對象。我不認爲我的XPath正在工作。
更新2
我得到的XPath是如下:
//xhtml:a[@href = 's001-BookTitlePage-01.xhtml']
or
//html:a[@href = 's001-BookTitlePage-01.xhtml']
還當我嘗試了XPath的,如://html:a
儘管我沒有得到所有錨標籤。
**更新3 **
我試圖檢查的XPath在線http://www.xpathtester.com/test
我發現下面的錯誤在我的XPath //html:a
Exception occurred evaluting XPath: //html:a. Exception: XPath expression uses unbound namespace prefix html
不知道是什麼意思。
感謝
沒有重複,只是意識到註冊是對下面的XPath表達式的命名空間,我沒」不要期待它。 –