Skip to content

Commit 3ee1cfd

Browse files
inonegoangelozerr
authored andcommitted
Support XPath predicates with wildcards
1 parent f3ed35f commit 3ee1cfd

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/xpath/matcher/XPathElementMatcher.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@ public String getLocalName() {
143143
* org.eclipse.wst.xml.search.core.xpath.matcher.IXPathNodeMatcher#isAny()
144144
*/
145145
public boolean isAny() {
146-
return anyElementName;
146+
var hasAttributes = attributes != null && !attributes.isEmpty();
147+
148+
// if the element has attribute predicates, it is no longer a wildcard "any" element.
149+
return anyElementName && !hasAttributes;
147150
}
148151

149152
/**

org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/xpath/matcher/XPathMatcherTest.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,52 @@ public void matchAnyText() {
128128
assertFalse(matcher.match(textOfBar));
129129
}
130130

131+
@Test
132+
public void matchAnyWithPredicate() {
133+
String xml = "<foo>\r\n" + //
134+
" <bar attr1=\"value1\" attr2=\"value1\">ABCD</bar>\r\n" + //
135+
" <bar attr1=\"bar-value1\" attr2=\"value1\">EFGH</bar>\r\n" + //
136+
" <baz attr1=\"value1\" attr2=\"value1\">IJKL</baz>\r\n" + //
137+
" <baz attr1=\"baz-value1\" attr2=\"value1\">MNOP</baz>\r\n" + //
138+
"</foo>";
139+
DOMDocument document = DOMParser.getInstance().parse(xml, "test.xml", null);
140+
DOMElement root = document.getDocumentElement();
141+
142+
DOMElement bar1 = (DOMElement) root.getChildren().get(0);
143+
DOMElement bar2 = (DOMElement) root.getChildren().get(1);
144+
DOMElement baz1 = (DOMElement) root.getChildren().get(2);
145+
DOMElement baz2 = (DOMElement) root.getChildren().get(3);
146+
147+
XPathMatcher matcher = new XPathMatcher("//*[@attr1='value1']");
148+
assertTrue(matcher.match(bar1), "Expected bar1 to match //*[@attr1='value1']");
149+
assertFalse(matcher.match(bar2), "Expected bar2 NOT to match //*[@attr1='value1'] due to mismatched attr1 value");
150+
assertTrue(matcher.match(baz1), "Expected baz1 to match //*[@attr1='value1']");
151+
assertFalse(matcher.match(baz2), "Expected baz2 NOT to match //*[@attr1='value1'] due to mismatched attr1 value");
152+
}
153+
154+
@Test
155+
public void matchOneWithPredicate() {
156+
String xml = "<foo>\r\n" + //
157+
" <bar attr1=\"value1\" attr2=\"value1\">ABCD</bar>\r\n" + //
158+
" <bar attr1=\"bar-value1\" attr2=\"value1\">EFGH</bar>\r\n" + //
159+
" <baz attr1=\"value1\" attr2=\"value1\">IJKL</baz>\r\n" + //
160+
" <baz attr1=\"baz-value1\" attr2=\"value1\">MNOP</baz>\r\n" + //
161+
"</foo>";
162+
DOMDocument document = DOMParser.getInstance().parse(xml, "test.xml", null);
163+
DOMElement root = document.getDocumentElement();
164+
165+
DOMElement bar1 = (DOMElement) root.getChildren().get(0);
166+
DOMElement bar2 = (DOMElement) root.getChildren().get(1);
167+
DOMElement baz1 = (DOMElement) root.getChildren().get(2);
168+
DOMElement baz2 = (DOMElement) root.getChildren().get(3);
169+
170+
XPathMatcher matcher = new XPathMatcher("//bar[@attr1='value1']");
171+
assertTrue(matcher.match(bar1), "Expected bar1 to match //bar[@attr1='value1']");
172+
assertFalse(matcher.match(bar2), "Expected bar2 NOT to match //bar[@attr1='value1'] due to mismatched attr1 value");
173+
assertFalse(matcher.match(baz1), "Expected baz1 NOT to match //bar[@attr1='value1'] due to mismatched tag name");
174+
assertFalse(matcher.match(baz2), "Expected baz2 NOT to match //bar[@attr1='value1'] due to mismatched tag name and attr1 value");
175+
}
176+
131177
private static DOMElement getElementByTagName(DOMElement parent, String tagName) {
132178
List<DOMNode> children = parent.getChildren();
133179
if (children != null) {
@@ -139,5 +185,4 @@ private static DOMElement getElementByTagName(DOMElement parent, String tagName)
139185
}
140186
return null;
141187
}
142-
143188
}

0 commit comments

Comments
 (0)