Depending on how you want to use XPath. In any case, you will need to go through the tree and build XPath along the way.
If you want to have a readable string for dispatching - just combining the names of the node nodes (see BrokenGlass suggestion) works fine
If you want to choose later in XPath
- positional XPath (indicate the position of each node in its parent) is one option (something like // [3] / *). You should consider attributes as a special case, since there is no order defined for attributes
- XPath with predefined prefixes (the namespace for the prefix must be stored separately) - / my: root / my: child [3] / o: prop1 / @ t: attr3
- XPath with built-in namespaces when you want to get the received and portable XPath / * [name () = 'root' and namespace-uri () = 'http: //my.namespace'] / .... (see specification for name and uri namespace functions http://www.w3.org/TR/xpath/#function-namespace-uri )
Note that you must consider special nodes, such as comments and processing instructions, if you want a truly universal version of XPath for node inside XML.
source share