Dimitre Novatchev がends-with
XPath 1.0 式で複製する方法を示しているこの質問を見てきました。SelectNodes
ただし、通話内でのコンテキストでの実装に問題があります。
以前、私が使用していた
XmlElement root = doc.DocumentElement;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("x", root.NamespaceURI);
XmlNodeList nodeList = doc.SelectNodes("//x:*[contains(name(.), '-notification')]", nsmgr);
必要なすべてのノードと、末尾に「s」が追加された必要のないノードが返されました(has-more-notifications
)。
だから私は私に与えられた Dimitre 式を使用してみました:
XmlNodeList nodeList = doc.SelectNodes("//x:*[substring(name(.), string-length(name(.)) - string-length('-notification') +1)]", nsmgr);
これは惨めに失敗し、のルートノードが表示されnotification-data-response
ます。
これは XPath への私の最初の進出であり、正規表現のように思えます - あなたはそれを理解しているか、理解していないかのどちらかです。
で終わるノードのみを返すように式を実装するにはどうすればよい-notification
ですか?
アップデート
入力のサンプル:
<?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>