Permalink
Browse files

[#156] Match.xpath() expressions are 0-indexed when there is a namesp…

…ace prefix
  • Loading branch information...
lukaseder committed Sep 10, 2018
1 parent b000f95 commit 343b46effdee47d03c45e09ace24a7eb0a18fcf2
@@ -300,7 +300,7 @@ private static final int siblingIndex(Element element) {
// All other elements are compared with siblings with the same name
// TODO: How to deal with namespaces here? Omit or keep?
else
return $(element).parent().children(element.getTagName()).get().indexOf(element);
return $(element).parent().children(JOOX.tag(element.getTagName(), false)).get().indexOf(element);
}

/**
@@ -2078,6 +2078,34 @@ public void testNamespacesXPath() {
.xpath("//root-ns:node").size());
}

@Test
public void testNamespacesXPathListing() {
$ = $(xmlNamespacesDocument);

List<String> xpaths = new ArrayList<String>();
for (Match m : $.find("*").each())
xpaths.add(m.xpath());

assertEquals(
asList(
"/root[1]/node[1]",
"/root[1]/node[1]/node[1]",
"/root[1]/node[1]/ns:node[1]",
"/root[1]/ns:node[1]",
"/root[1]/nested1[1]",
"/root[1]/nested1[1]/node[1]",
"/root[1]/nested1[1]/node[1]/node[1]",
"/root[1]/nested1[1]/node[1]/ns:node[1]",
"/root[1]/nested1[1]/ns:node[1]",
"/root[1]/nested2[1]",
"/root[1]/nested2[1]/node[1]",
"/root[1]/nested2[1]/node[1]/node[1]",
"/root[1]/nested2[1]/node[1]/xx:node[1]",
"/root[1]/nested2[1]/xx:node[1]"
),
xpaths);
}

@Test
public void testChain() {
final List<String> result = new ArrayList<String>();
@@ -300,7 +300,7 @@ private static final int siblingIndex(Element element) {
// All other elements are compared with siblings with the same name
// TODO: How to deal with namespaces here? Omit or keep?
else
return $(element).parent().children(element.getTagName()).get().indexOf(element);
return $(element).parent().children(JOOX.tag(element.getTagName(), false)).get().indexOf(element);
}

/**
@@ -2078,6 +2078,34 @@ public void testNamespacesXPath() {
.xpath("//root-ns:node").size());
}

@Test
public void testNamespacesXPathListing() {
$ = $(xmlNamespacesDocument);

List<String> xpaths = new ArrayList<String>();
for (Match m : $.find("*").each())
xpaths.add(m.xpath());

assertEquals(
asList(
"/root[1]/node[1]",
"/root[1]/node[1]/node[1]",
"/root[1]/node[1]/ns:node[1]",
"/root[1]/ns:node[1]",
"/root[1]/nested1[1]",
"/root[1]/nested1[1]/node[1]",
"/root[1]/nested1[1]/node[1]/node[1]",
"/root[1]/nested1[1]/node[1]/ns:node[1]",
"/root[1]/nested1[1]/ns:node[1]",
"/root[1]/nested2[1]",
"/root[1]/nested2[1]/node[1]",
"/root[1]/nested2[1]/node[1]/node[1]",
"/root[1]/nested2[1]/node[1]/xx:node[1]",
"/root[1]/nested2[1]/xx:node[1]"
),
xpaths);
}

@Test
public void testChain() {
final List<String> result = new ArrayList<String>();

0 comments on commit 343b46e

Please sign in to comment.