Skip to content

Commit 7070f40

Browse files
author
Nico Verwer
committed
[bugfix] another problem in determining tree indexes
1 parent 2b32c9c commit 7070f40

File tree

1 file changed

+17
-2
lines changed
  • exist-core/src/main/java/org/exist/xquery/functions/fn/transform

1 file changed

+17
-2
lines changed

exist-core/src/main/java/org/exist/xquery/functions/fn/transform/TreeUtils.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import net.sf.saxon.s9api.XdmNode;
2626
import org.exist.xquery.value.NodeValue;
27+
import org.w3c.dom.Attr;
2728
import org.w3c.dom.Document;
2829
import org.w3c.dom.Node;
2930

@@ -74,17 +75,31 @@ static List<Integer> treeIndex(final Node node) {
7475
return index;
7576
}
7677
final List<Integer> index = treeIndex(parent);
77-
Node sibling = node.getPreviousSibling();
78+
Node sibling = previousSiblingNotAttribute(node);
7879
int position = 0;
7980
while (sibling != null) {
8081
position += 1;
81-
sibling = sibling.getPreviousSibling();
82+
sibling = previousSiblingNotAttribute(sibling);
8283
}
8384
index.add(position);
8485

8586
return index;
8687
}
8788

89+
/**
90+
* A org.exist.dom.persistent.StoredNode returns attributes of an element as previous siblings of the element's children.
91+
* This is not compatible with the way xdmNodeAtIndex works, so we need to compensate for this.
92+
* @param node
93+
* @return the previous sibling of `node` that is not an attribute.
94+
*/
95+
private static Node previousSiblingNotAttribute(Node node) {
96+
Node sibling = node.getPreviousSibling();
97+
if (sibling instanceof Attr) {
98+
return null;
99+
}
100+
return sibling;
101+
}
102+
88103
static XdmNode xdmNodeAtIndex(final XdmNode xdmNode, final List<Integer> index) {
89104
if (index.isEmpty()) {
90105
return xdmNode;

0 commit comments

Comments
 (0)