Get XPath query row / column in Pugixml

We want to get the row / column of the xpath query result in pugixml:

pugi::xpath_query query_child(query_str);
std::string value = Convert::toString(query_child.evaluate_string(root_node));

We can get the offset, but not the row / column:

unsigned int = query_child.result().offset;

If we reanalyze the file, we can convert offset => (row, column), but this is not efficient.

Is there an effective way to achieve this?

+3
source share
1 answer
  • result (). offset is the last parsed offset in the query string; it will be 0 if the request has been successfully analyzed; therefore, this is not an offset in the XML file.

  • For XPath queries returning strings, the concept of "offset in the XML file" is not defined - that is, what do you expect for the query concat("a", "b")?

  • XPath, , node . , - . TODO (.. ), .

, node, XPath, - XPath node (query.evaluate_node_set node.select_single_node/select_nodes), (node.offset_debug()) / .

→ / , ; , :

#include <vector>
#include <algorithm>
#include <cassert>
#include <cstdio>

typedef std::vector<ptrdiff_t> offset_data_t;

bool build_offset_data(offset_data_t& result, const char* file)
{
    FILE* f = fopen(file, "rb");
    if (!f) return false;

    ptrdiff_t offset = 0;

    char buffer[1024];
    size_t size;

    while ((size = fread(buffer, 1, sizeof(buffer), f)) > 0)
    {
        for (size_t i = 0; i < size; ++i)
            if (buffer[i] == '\n')
                result.push_back(offset + i);

        offset += size;
    }

    fclose(f);

    return true;
}

std::pair<int, int> get_location(const offset_data_t& data, ptrdiff_t offset)
{
    offset_data_t::const_iterator it = std::lower_bound(data.begin(), data.end(), offset);
    size_t index = it - data.begin();

    return std::make_pair(1 + index, index == 0 ? offset : offset - data[index - 1]);
}

Mac ; , , .

+2

Source: https://habr.com/ru/post/1788144/


All Articles