c++xmllinuxtinyxmltinyxpath

Why does TinyXPath give different results for the same object when called in two different classes?


I'm building a project on Ubuntu in which I'm using TinyXPath library along with TinyXml to parse the following xml:

<nodes>
    <node attribute1="1" attribute2="2" />
    <node attribute1="2" attribute2="3" />
    ...
    <node attribute1="10" attribute2="11" />
</nodes>

To find out the number of node entries contained, I'm using:

TinyXPath::i_xpath_int( xml_root,"count(/nodes/node)")

Apparently, using this function call in one object returns the actual number of nodes, 10; but using it in another object (a different class type), it always returns 0. I have checked to see if xml_root is the same object in both cases and both objects have the same address. Printing the contents gave me the same xml.

Instead, if I use TinyXML, I get the right result and I can even access all the atributes and get the right results. The following code gives the right no_nodes:

for(node = xml_root->FirstChild(); node; node = node->NextSibling())
    no_nodes++;

Here comes the weird part. If I build this project on Windows 7, it works just fine. The function call always returns the right number of nodes. Has anyone encountered this sort of problem before?

P.S.: I know that I haven't given many specifics on this problem, but it is a huge project and it would take me days to explain it all. So this is just a shot in the dark.


Solution

  • By using gdb I found out that there was no parsing problem with the "count(/products/product)", as I initially thought.

    The problem was that my project uses a lot of different libraries. One of these libraries used a different version of tinyxml from the one used by tinyxpath.

    When I linked the binary for the class from my project where tinyxpath didn't work, i used the following command in my makefile:

    g++ -o binary -lahttplib -ltinyxpath [...]
    

    ahttplib already included an older version of tinyxml. So, when tinyxpath was linked in, it linked to the tinyxml library which was already included from ahttplib, instead of it's own version. The tinyxml version from ahttplib and the one from tinyxpath were incompatible.

    The binary with the class from my project where tinyxpath worked fine only included tinyxpath, so the problem didn't occur there.

    The simple solution was to change the link order: tinxypath before ahttplib. The right way to solve this issue is to make ahttplib and tinyxpath refer to the same tinyxml library.