XML : How do I create an xPath statement from a NodeInfo?

I'm using the S9API with Saxon 9.7 HE, and I have a NodeInfo object. I need to create an xPath statement that uniquely corresponds to this NodeInfo. It's easy enough to get the prefix, the node name, and the parent:

  String prefix = node.getPrefix();  String localPart = node.getLocalPart();  NodeInfo parent = node.getParent();    

So I can walk up the tree, building the xPath as I go. But what I can't find is any way to get the positional predicate info. IOW, it's not sufficient to create:

  /persons/person/child    

because it might match multiple child elements. I need to create:

  /persons/person[2]/child[1]    

which will match only one child element. Any ideas on how to get the positional predicate info? Or maybe there's a better way to do this altogether?

BTW, for those who use the generic DOM and not the S9API, here's an easy solution to this problem: http://practicalxml.sourceforge.net/apidocs/net/sf/practicalxml/DomUtil.html#getAbsolutePath(org.w3c.dom.Element)

No comments:

Post a Comment