0

我正在使用 pugixml 来解析以下 xml:

<td class="title">
     <div class="random" />
     <a href="link">Link1 </a>
</td>

<td class="title">
     <div class="random" />
     <a href="link">Link2 </a>
</td>

ETC...

我想要 td class ="title" 中每个 'a href' 的值(出​​现次数不定),但只有第一个这样的实例。

我正在使用以下代码来尝试获取这些值:

pugi::xpath_node_set link_nodes = list_doc.select_nodes("//td[@class='title']");

    for (pugi::xpath_node_set::const_iterator it = link_nodes.begin();it != link_nodes.end();++it)
    {
        pugi::xpath_node single_link_node = *it;

        std::cout << single_link_node.node().select_single_node("//a").node().attribute("href").value()<<std::endl;


    }

这似乎不起作用(它输出的次数,但其值似乎甚至没有出现在该元素中)。

谢谢。

4

1 回答 1

0

“//a”选择文档中的所有“a”节点;您可能的意思是“.//a”,它选择子树中的所有“a”节点。

您还可以使用一个 XPath 表达式而不是多个:

//td[@class='title']//a[1]

这会为每个td选择第一个标签- 即 [1] 仅适用于 //a,而不适用于完整表达式。

于 2013-12-15T22:23:07.833 回答