I have an XML with entities like this :
<Entity>
<Name>Lwresd_Dns_Server|LwresdDnsServer</Name>
</Entity
<Entity>
<Name>Lwresd_Dns_Server_Data|LwresdDnsServerData</Name>
</Entity>
My xpath expression is
XPathExpression expr = xpath1.compile("//Entity[matches(Name,'" +line+ "')]");
where line is a variable with value LwresdDnsServer.
The above xpath expression matches both entities , where I need it to match only the first one, i.e
Lwresd_Dns_Server|LwresdDnsServer
How should I frame the expression to do that ??
I believe this should do the trick:
This compares the entity Name enclosed in
|s with the variable name enclosed in|s, so you get something like:And resultingly, only the first of the two Entities is selected.
If you only want to find entities that end with
line(and not just those that contain an exact match for it), then you can do this (assuming the values are guaranteed to not contain the character$– if there’s the possibility it would contain a$, you should choose a different delimiter that it definitely won’t contain, or use Dimitre Novatchev’s answer to this question):I haven’t used the
matches()function in XPath (it’s not supported in XPath 1.0), but I suspect the following would also work for finding a value at the end of an Entity name, if your XPath evaluator supportsmatches():Here,
$is the RegEx symbol for the end of a string.