orderEvents 要素があり、その要素に orderEvent または空のテキストではない子ノードがある場合、エラーを追加する次のコードがあります。テストケースは、私が求めているものをよりよく示しています。
orderEvents
XPATH 式は、要素内に空のスペースがある場合を除き、すべての場合に機能します。以下のテスト ケースvalidVendorPaymentFormat7を参照してください。これが私のXPATH式です。
"./expectedVendorPaymentTransactions/orderEvents/node()[not(self::orderEvent) or self::text()[normalize-space(.)!='']]"
テストケース:
<TestData>
<TestcaseList>
<Testcase>
<rootIdentifier>validVendorPaymentFormat1</rootIdentifier>
<expectedVendorPaymentTransactions>
<orderEvents></orderEvents>
</expectedVendorPaymentTransactions>
</Testcase>
<Testcase>
<rootIdentifier>validVendorPaymentFormat2</rootIdentifier>
</Testcase>
<Testcase>
<rootIdentifier>validVendorPaymentFormat3</rootIdentifier>
<expectedVendorPaymentTransactions>
</expectedVendorPaymentTransactions>
</Testcase>
<Testcase>
<rootIdentifier>validVendorPaymentFormat4</rootIdentifier>
<expectedVendorPaymentTransactions>
<orderEvents><orderEvent>SOME_EVENT</orderEvent></orderEvents>
</expectedVendorPaymentTransactions>
</Testcase>
<Testcase>
<rootIdentifier>validVendorPaymentFormat5</rootIdentifier>
<expectedVendorPaymentTransactions>
<orderEvents><orderEvent>SOME_EVENT</orderEvent></orderEvents>
</expectedVendorPaymentTransactions>
</Testcase>
<Testcase>
<rootIdentifier>validVendorPaymentFormat6</rootIdentifier>
<expectedVendorPaymentTransactions>
<orderEvents><orderEvent>SOME_EVENT</orderEvent><orderEvent>SOME_EVENT</orderEvent></orderEvents>
</expectedVendorPaymentTransactions>
</Testcase>
<Testcase>
<rootIdentifier>validVendorPaymentFormat7</rootIdentifier>
<expectedVendorPaymentTransactions>
<orderEvents>
<orderEvent>SOME_EVENT</orderEvent>
</orderEvents>
</expectedVendorPaymentTransactions>
</Testcase>
</TestcaseList>
</TestData>
失敗するケースはvalidVendorPaymentFormat7 です
失敗メッセージ:
[exec] Failure:
[exec] Order vendor payments format contains elements other than orderEvent for validVendorPaymentFormat7 ["\n ", "\n "].
[exec] <false> is not true.
失敗したケースを次のように変更します。
<Testcase>
<rootIdentifier>validVendorPaymentFormat7</rootIdentifier>
<expectedVendorPaymentTransactions>
<orderEvents><orderEvent>SOME_EVENT</orderEvent></orderEvents>
</expectedVendorPaymentTransactions>
</Testcase>
結果は合格。
残念ながら、http://www.freeformatter.com/xpath-tester.htmlテスト ケース 7 では、期待どおりに空のリストが返されます。
更新 - コードの追加
Ruby バージョン: 1.9
irb -v = 0.9.6
テスト ファイル:
require 'rexml/document'
xpath = "//expectedVendorPaymentTransactions/orderEvents/node()[not(self::orderEvent) or self::text()[translate(., ' ', '')!='']]"
document = REXML::Document.new <<EOF
<Testcase>
<rootIdentifier>validVendorPaymentFormat7</rootIdentifier>
<expectedVendorPaymentTransactions>
<orderEvents>
<orderEvent>SOME_EVENT</orderEvent>
</orderEvents>
</expectedVendorPaymentTransactions>
</Testcase>
EOF
document2 = REXML::Document.new <<EOF
<Testcase>
<rootIdentifier>validVendorPaymentFormat7</rootIdentifier>
<expectedVendorPaymentTransactions>
<orderEvents><orderEvent>SOME_EVENT</orderEvent></orderEvents>
</expectedVendorPaymentTransactions>
</Testcase>
EOF
puts "1: #{REXML::XPath.match(document, xpath).inspect}"
puts "2: #{REXML::XPath.match(document2, xpath).inspect}"
出力:
irb(main):001:0> load './test/test_rexp.rb'
1: ["\n ", "\n "]
2: []
=> true
Jens の最新リビジョン:
irb(main):009:0> load './test/test_rexp.rb'
1: ", "
2: ", "
=> true