c++ - 无法使用 libxml2 和 xpath 获取节点

标签 c++ xpath libxml2

我有以下代码可以从 HTML 页面中查找所有元素:

string AParser::cleanHTMLDocument(const string& aDoc) {
   vector<xmlNodePtr> nodesToRemove;
   xmlDocPtr doc = xmlParseDoc((xmlChar *)aDoc.c_str());
   xmlXPathContextPtr context = xmlXPathNewContext(doc);
   xmlXPathObjectPtr result = xmlXPathEvalExpression(
                                 (const xmlChar *)string("//link").c_str(), context);
   if (xmlXPathNodeSetIsEmpty(result->nodesetval)) {
       xmlXPathFreeObject(result);
       xmlXPathFreeContext(context);
       xmlFreeDoc(doc);
       LOG(WARNING)<< "XPath is invalid, bailing out.";
       return string();
   }
   const int size = result->nodesetval->nodeNr;
   for(int i = size - 1; i >= 0; i--) {
      LOG(DEBUG)<< result->nodesetval->nodeTab[i]->name;
   }
}

但由于某些原因,xmlXPathNodeSetIsEmpty 始终为真。我在这里遗漏了什么吗?

更新:输入文档

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta name="generator" content="HTML Tidy for Linux (vers 7 December 2008), see www.w3.org"/>
<title>The Republic, by Plato</title>
<link href="0.css" type="text/css" rel="stylesheet"/>
<link href="1.css" type="text/css" rel="stylesheet"/>
<link href="pgepub.css" type="text/css" rel="stylesheet"/>
<meta content="EpubMaker 0.3.20a6 by Marcello Perathoner &lt;webmaster@gutenberg.org&gt;" name="generator"/>
</head>
<body>
<div xml:space="preserve" class="pgmonospaced pgheader"><br/>The Project Gutenberg EBook of The Republic, by Plato<br/><br/>This eBook is for the use of anyone anywhere at no cost and with<br/>almost no restrictions whatsoever.  You may copy it, give it away or<br/>re-use it under the terms of the Project Gutenberg License included<br/>with this eBook or online at www.gutenberg.org<br/><br/><br/>Title: The Republic<br/><br/>Author: Plato<br/><br/>Translator: B. Jowett<br/><br/>Release Date: August 27, 2008 [EBook #1497]<br/>Last Updated: November 5, 2012<br/><br/>Language: English<br/><br/><br/>*** START OF THIS PROJECT GUTENBERG EBOOK THE REPUBLIC ***<br/><br/><br/><br/><br/>Produced by Sue Asscher, and David Widger<br/><br/><br/><br/><br/><br/></div>
<p><br/>
<br/></p>
<h1 id="pgepubid00000">THE REPUBLIC</h1>
<p><br/></p>
<h2>By Plato</h2>
<p><br/></p>
<h3 id="pgepubid00001">Translated by Benjamin Jowett</h3>
<p><br/>
<br/>
<br/>
Note: See also "The Republic" by Plato, Jowett, etext #150<br/>
<br/></p>
<hr/>
<p><br/>
<br/></p>
<h2 id="pgepubid00002">Contents</h2>
<table summary="">
<tbody><tr>
<td>
<p class="toc"><a href="@public@vhost@g@gutenberg@html@files@1497@1497-h@1497-h-0.htm.html#link2H_INTR" class="pginternal">INTRODUCTION AND ANALYSIS.</a></p>
<br/>
<p class="toc"><a class="c1 pginternal" href="@public@vhost@g@gutenberg@html@files@1497@1497-h@1497-h-7.htm.html#link2H_4_0002">THE REPUBLIC.</a></p>
<p class="toc"><a href="@public@vhost@g@gutenberg@html@files@1497@1497-h@1497-h-7.htm.html#link2H_4_0003" class="pginternal">PERSONS OF THE DIALOGUE.</a></p>
<p class="toc"><a href="@public@vhost@g@gutenberg@html@files@1497@1497-h@1497-h-7.htm.html#link2H_4_0004" class="pginternal">BOOK I.</a></p>
<p class="toc"><a href="@public@vhost@g@gutenberg@html@files@1497@1497-h@1497-h-8.htm.html#link2H_4_0005" class="pginternal">BOOK II.</a></p>
<p class="toc"><a href="@public@vhost@g@gutenberg@html@files@1497@1497-h@1497-h-9.htm.html#link2H_4_0006" class="pginternal">BOOK III.</a></p>
<p class="toc"><a href="@public@vhost@g@gutenberg@html@files@1497@1497-h@1497-h-10.htm.html#link2H_4_0007" class="pginternal">BOOK IV.</a></p>
<p class="toc"><a href="@public@vhost@g@gutenberg@html@files@1497@1497-h@1497-h-11.htm.html#link2H_4_0008" class="pginternal">BOOK V.</a></p>
<p class="toc"><a href="@public@vhost@g@gutenberg@html@files@1497@1497-h@1497-h-12.htm.html#link2H_4_0009" class="pginternal">BOOK VI.</a></p>
<p class="toc"><a href="@public@vhost@g@gutenberg@html@files@1497@1497-h@1497-h-14.htm.html#link2H_4_0010" class="pginternal">BOOK VII.</a></p>
<p class="toc"><a href="@public@vhost@g@gutenberg@html@files@1497@1497-h@1497-h-14.htm.html#link2H_4_0011" class="pginternal">BOOK VIII.</a></p>
<p class="toc"><a href="@public@vhost@g@gutenberg@html@files@1497@1497-h@1497-h-16.htm.html#link2H_4_0012" class="pginternal">BOOK IX.</a></p>
<p class="toc"><a href="@public@vhost@g@gutenberg@html@files@1497@1497-h@1497-h-16.htm.html#link2H_4_0013" class="pginternal">BOOK X.</a></p>
</td>
</tr>
</tbody></table>
<p><br/>
<br/></p>
<hr/>
<p><br/>
<br/>
<a id="link2H_INTR"><!--  H2 anchor --></a></p>
<h2 id="pgepubid00003">INTRODUCTION AND ANALYSIS.</h2>
<p>The Republic of Plato is the longest of his works with the exception of the Laws, and is certainly the greatest of them. There are nearer approaches to modern metaphysics in the Philebus and in the Sophist; the Politicus or Statesman is more ideal; the form and institutions of the State are more clearly drawn out in the Laws; as works of art, the Symposium and the Protagoras are of higher excellence. But no other Dialogue of Plato has the same largeness of view and the same perfection of style; no other shows an equal knowledge of the world, or contains more of those thoughts which are new as well as old, and not of one age only but of all. Nowhere in Plato is there a deeper irony or a greater wealth of humour or imagery, or more dramatic power. Nor in any other of his writings is the attempt made to interweave life and speculation, or to connect politics with philosophy. The Republic is the centre around which the other Dialogues may be grouped; here philosophy reaches the highest point (cp, especially in Books V, VI, VII) to which ancient thinkers ever attained. Plato among the Greeks, like Bacon among the moderns, was the first who conceived a method of knowledge, although neither of them always distinguished the bare outline or form from the substance of truth; and both of them had to be content with an abstraction of science which was not yet realized. He was the greatest metaphysical genius whom the world has seen; and in him, more than in any other ancient thinker, the germs of future knowledge are contained. The sciences of logic and psychology, which have supplied so many instruments of thought to after-ages, are based upon the analyses of Socrates and Plato. The principles of definition, the law of contradiction, the fallacy of arguing in a circle, the distinction between the essence and accidents of a thing or notion, between means and ends, between causes and conditions; also the division of the mind into the rational, concupiscent, and irascible elements, or of pleasures and desires into necessary and unnecessary—these and other great forms of thought are all of them to be found in the Republic, and were probably first invented by Plato. The greatest of all logical truths, and the one of which writers on philosophy are most apt to lose sight, the difference between words and things, has been most strenuously insisted on by him (cp. Rep.; Polit.; Cratyl), although he has not always avoided the confusion of them in his own writings (e.g. Rep.). But he does not bind up truth in logical formulae,—logic is still veiled in metaphysics; and the science which he imagines to 'contemplate all truth and all existence' is very unlike the doctrine of the syllogism which Aristotle claims to have discovered (Soph. Elenchi).</p>
</body></html>

最佳答案

您正在查询的文档使用 XML 命名空间。您必须忽略命名空间或注册并使用它。

忽略 namespace ,查询所有节点并比较谓词中的本地名称(没有 namespace ),如 //*[local-name(.) = 'link' ]

注册命名空间,请调用xmlXPathRegisterNs,然后在所有具有该命名空间的节点前加上[ns-prefix]:。例如:

xmlXPathContextPtr context = xmlXPathNewContext(doc);
xmlXPathRegisterNs(context, 'xhtml', 'http://www.w3.org/1999/xhtml');
xmlXPathObjectPtr result = xmlXPathEvalExpression(
                       (const xmlChar *)string("//xhtml:link").c_str(), context);

关于c++ - 无法使用 libxml2 和 xpath 获取节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21461087/

相关文章:

c - XML : Get an array of string from text cut by element

objective-c - LibXML2 剥离属性内的新行

c++ - 为什么 "using namespace std;"被认为是不好的做法?

c++ - 按值或右值引用传递

c++ - 使用 std::count 计算字符串 vector 中特定字符的数量 (C++)

c++ - C++中的私有(private)继承

python - 使用 Xpath (HtmlXPathSelector) 获取父文本和子文本

PHP SIMPLEXML XPATH 匹配具有相同子字符串的多个标签

Java XPath 变音/元音解析

mingw/libxml2 问题