Question

I'm using pugixml to parse the following 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...

I want the value of every 'a href' in a td class ="title" (which appears an indeterminate number of times) but only the first such instance.

I am using the following code to try and get these values:

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;


    }

which doesn't seem to work (it outputs number of times but with a value that doesn't even seem to appear within that element).

Thanks.

Was it helpful?

Solution

"//a" selects all "a" nodes in the document; you probably meant ".//a" that selects all "a" nodes in the subtree.

You can also use one XPath expression instead of multiple:

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

This selects the first tag for each td - i.e. [1] only applies to //a, not to the full expression.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top