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 ; , , .