xpath
xpath copied to clipboard
Little informative examples.
Hello.
The idea of using namespaces is certainly original, but it is not always convenient. In general, I load the page from somewhere and I do not want to set namespaces. It would be convenient to turn them off optionally.
Further. I'm trying to parse the next line:
const htmlString=<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Galleries</title></head><body></body></html>;
I can not select /html/head/title for this line using namespace.
In your examples, a search is made for part of the path (for example, "/namespace:DeepChild"). I want/need to use the full path - /html/body/child1/child2/....
If I use namespaces, it does not work.
If you use namespaces correctly, you should be able to use a full path just fine:
/ns:html/ns:body/ns:child/ns:child2
However, this library has an HTML mode which ignores namespaces on unprefixed node tests and is case insensitive on node names. I would advise using that in your case:
var expr = xpath.parse('/html/body/child1/child1');
var result = expr.select({ node: dom, isHtml: true });
"isHtml" flag is working. But I do not see it in the documentation.
Alternatively, I tried to do this without a flag, and nothing works for me. But maybe it's planned :)
https://pastebin.com/Fz1ETiPb
I will add it to the documentation. Thank you for pointing that out.
The example in your pastebin doesn't work because the path is wrong. head and title are both in the http://www.w3.org/1999/xhtml namespace so the path needs to be /ns:html/ns:head/ns:title.
Excellent. Thanks (this works).
I do not know if it's worth describing the situation with my example and the '/ns:html/ns:head/ns: title' solution in the documentation. This I leave to your discretion.
topic may be closed.