java - Get Xpath from the org.w3c.dom.Node -


can full xpath org.w3c.dom.node ?

say node pointing middle of xml document. extract xpath element.

the output xpath i'm looking //parent/child1/chiild2/child3/node. parent node xpath. ignore xpath's having expressions , points same node.

there's no generic method getting xpath, because there's no 1 generic xpath identifies particular node in document. in schemas, nodes uniquely identified attribute (id , name common attributes.) in others, name of each element (that is, tag) enough uniquely identify node. in few (unlikely, possible) cases, there's no 1 unique name or attribute takes specific node, , you'd need use cardinality (get n'th child of m'th child of...).

edit: in cases, it's not hard create schema-dependent function assemble xpath given node. example, suppose have document every node uniquely identified id attribute, , you're not using namespaces. (i think) following pseudo-java work return xpath based on attributes. (warning: have not tested this.)

string getxpath(node node) {     node parent = node.getparent();     if (parent == null) {         return "/" + node.gettagname();     }     return getxpath(parent) + "/" + "[@id='" + node.getattribute("id") + "']"; } 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -