I've seen this question where Dimitre Novatchev shows a way of replicating ends-with
with an XPath 1.0 expression. However I am having trouble implementing it in context of within a SelectNodes
call.
Previously I was using
XmlElement root = doc.DocumentElement;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("x", root.NamespaceURI);
XmlNodeList nodeList = doc.SelectNodes("//x:*[contains(name(.), '-notification')]", nsmgr);
Which returned all the nodes I wanted plus one I didn't which had an additional 's' on the end (has-more-notifications
).
So I tried using Dimitre expression which gave me:
XmlNodeList nodeList = doc.SelectNodes("//x:*[substring(name(.), string-length(name(.)) - string-length('-notification') +1)]", nsmgr);
Which fails miserably giving me the root node of notification-data-response
.
This is my first foray into XPath and it seems to be like regex - you either understand it or you don't.
How do I implement the expression so it returns only the nodes that end with -notification
?
UPDATE
A sample of the input:
<?xml version="1.0" encoding="UTF-8"?>
<notification-data-response xmlns="http://checkout.google.com/schema/2" serial-number="16ceae10-a9f1-4ff0-a77b-c3407f2d684a">
<notifications>
<new-order-notification serial-number="653417067275702-00001-7">
</new-order-notification>
<order-state-change-notification serial-number="653417067275702-00005-1">
</order-state-change-notification>
<risk-information-notification serial-number="653417067275702-00005-5">
</risk-information-notification>
<authorization-amount-notification serial-number="653417067275702-00005-6">
</authorization-amount-notification>
</notifications>
<continue-token>CP6u9NeQJxC2y72h-MiUARgG</continue-token>
<has-more-notifications>false</has-more-notifications>
</notification-data-response>