[XML-SIG] get the abolute path for a node

xmlsig at codeweld.com xmlsig at codeweld.com
Thu Aug 5 14:51:09 CEST 2004


> Alexandre CONRAD wrote:
> > I've been looking around, and apparently, there is a function that
> > returns all the ascensor of a node. But I need this as a string path.
> >
> > Any ideas ?
>
> There is no such function, and it would be difficult to define one.
> For example, /rootnode/node/sub_node5 might refer to a different node,
> if node has multiple children with a name of sub_node5. So one could
> try to find a better-matching string, such as /rootnode/node/sub_node5[3].
>
> Or, such a function might generate something like
> /following::node()[1564], which is probably not what you want, but
> would match what you have requested.
>
> Regards,
> Martin
Does this help?

def abs_path( node ):
    successors = 1
    parent = node.previousSibling
    while parent:
        if parent.nodeName == node.nodeName: successors += 1
        parent = parent.previousSibling
    name = node.nodeName == '#text' and 'text()' or node.nodeName
    path = successors>1 and '/%s[%s]'%(name,successors) or '/%s'%name
    if node.parentNode and node.parentNode.nodeName != '#document':
        return abs_path( node.parentNode )+path
    return path

Kind Regards
Florian





More information about the XML-SIG mailing list