I have to do a case-insensitive XML search. I’ve written XPath expressions which are working fine, but when I use the translate function inside an XPath expression I get an error. Below are the XPath expressions that are working fine:
string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string lower = "abcdefghijklmnopqrstuvwxyz";
string xpath = "/data/item[AID/@iskey='true' and AID/text() = 'fep'][not(*[@iskey][local-name() != 'AID' ])]";
string xpath1 = "/data/item[translate('Aid','" + upper + "','" + lower + "')]";
And here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<data>
<item>
<AID iskey="true">fep</AID>
<account>MS</account>
</item>
</data>
I want to place the translate function in the first XPath expression like this:
string xpath = "/data/item[translate('AID','" + upper + "','" + lower + "')/@iskey='true' and translate('AID','" + upper + "','" + lower + "')/text() = 'fep'][not(*[@iskey][local-name() != translate('aid','" + upper + "','" + lower + "') ])]";
When I do this, I get the following error:
Expression must evaluate to a node-set.
This xpath is working fine for me.
string col="AID";
string xpath = "/data/item[col/@iskey='true' and col/text() = 'fep'][not(*[@iskey][local-name() != 'col' ])]";
Now my requirement is that the col value can be in any case upper case or lower case, so I want a change in my xpath with which it returns me the result irrespective of in which case data is present in the xml.
If a user gives aid or Aid or aiD it returns me the result.
The error is due to the fact that in your XPath expression you have a location step like this:
The subexpression:
is syntactically illegal in XPath, because the left-hand-side of the
/operator must be a node-set — here it is just a string.You want to have an XPath expression like this — not the one you are generating at present:
XSLT – based verification:
When this transformation is applied on the provided XML document:
the XPath expression is evaluated and the selected nodes (just one in this case) are copied to the output:
Update:
The OP has difficulty understanding this solution. This is an example to show him that the solution works for any capitalisation of the string “aid”:
When the transformation above is applied on an XML document, that contains element names that are any capitalization of “aid” — such as this:
again the expected result is produced:
Update 2:
The OP still doesn’t understand the solution and claims that for a specific XML document — provided below — “I used your expression it returns null “
Here is the XML document as provided in the comment of the OP:
When the same XSLT verification is run against this document, again the correct element is selected and output: