haysclark / as3-xpath

1 stars 0 forks source link

element index incorrect for equal XML elements with same parent #26

Closed haysclark closed 10 years ago

haysclark commented 10 years ago

From tra...@circutus.com on October 05, 2011 09:55:41

Given two identical


elements, with the same parent node, calling XPathUtils.findPath(to, from, context) will always return an xpath expression pointing to the same
node index.


text

The reason is because XPathUtils function getPeerPositionPredicate(node, context) compares the contents of XML elements to determine the node index. Instead, it should compare the "childIndex" of the XML element.

Here's a fixed version of the function:

private static function getPeerPositionPredicate(node:XML, context:XPathContext):String { var parent:XML = node.parent(); if(!parent){ return ""; }

var peers:XMLList = node.parent()[node.name()]; var n:int = peers.length(); var i:int = 0; if (n > 1){ var nodeIndex:int = node.childIndex(); for(; i < n; ++i){ if (nodeIndex == peers[i].childIndex()) break; } } return "[" + (context.zeroIndexPosition ? i : ++i) + "]"; }

Original issue: http://code.google.com/p/xpath-as3/issues/detail?id=26

haysclark commented 10 years ago

From peterj...@gmail.com on August 27, 2012 21:31:41

Hi Travis, I've committed the fix and made a new build. Apologies for the delay, I wasn't getting notifications on new issues.

Status: Fixed