Queries on non-root nodes

Help
gavri
2007-07-18
2013-04-30
  • gavri
    gavri
    2007-07-18

    Do queries on non-root nodes work? I pass a TiXmlElement pointer to an xpath_processor constructor and make queries and the results are as if the xpath_processor is acting on the entire document that the TiXmlElement belongs to and not on this individual element.

    There have been two posts that included questions about this, but no answers.

    http://sourceforge.net/forum/forum.php?thread_id=1567873&forum_id=341080
    http://sourceforge.net/forum/forum.php?thread_id=1675083&forum_id=341080

     
    • Yves Berquin
      Yves Berquin
      2007-07-18

      TinyXPath should work fine with non-root elements.
      Could you post a more specific code example of what is not working?

       
    • gavri
      gavri
      2007-07-18

      string xml("<a><b><c></c><c></c></b><b><c></c><c></c></b></a>");

      TiXmlDocument doc;
      doc.Parse(xml.c_str());

      TiXmlElement *p_root_element = doc.RootElement();

      TinyXPath::xpath_processor xpath_w(p_root_element, "/a/b");  //One
      unsigned int number_of_nodes1 = xpath_w.u_compute_xpath_node_set();

      TiXmlNode * t = xpath_w.XNp_get_xpath_node(0); //Two

      TinyXPath::xpath_processor xpath_t(t, "/b/c");
      unsigned int number_of_nodes2 = xpath_t.u_compute_xpath_node_set();
      assert(number_of_nodes2 == 2); //I expect 2 and get 4

      The first xpath expression is "/a/b" which as expected gives me two nodes. I pick the first node and apply the expression "/b/c" on it expecting to get two nodes. I instead get four nodes.

       
    • gavri
      gavri
      2007-07-18

      Posting this code made me realise that I hadn't changed my code and was still passing xml.c_str() to TiXmlDocument::Parse() even though I now am using TinyXPath compiled with STL support.

       
    • gavri
      gavri
      2007-07-18

      Ignore previous post. TiXmlDocument::Parse() doesn't accept an std::string

       
    • gavri
      gavri
      2007-07-19

      So...is this a bug? Is there a way I can work around it?