Pertimbangkan XML sederhana berikut ini:
<xml>
<customer name="Max">
<email address="[email protected]" />
</customer>
<customer name="Erik">
<email address="[email protected]" />
</customer>
<customer name="Brent">
<email address="brentcom" />
</customer>
</xml>
Saya ingin mendapatkan daftar <Customer>
urutan di mana address
atribut <email>
item tidak mengandung @
.
Jadi, saya ingin output yang terlihat seperti:
<customer name="Brent">
<email address="brentcom" />
</customer>
mcve :
DECLARE @x XML = '<xml>
<customer name="Max"><email address="[email protected]" /></customer>
<customer name="Erik"><email address="[email protected]" /></customer>
<customer name="Brent"><email address="brentcom" /></customer>
</xml>';
Kueri ini:
SELECT WithValidEmail = @x.query('/xml/customer/email[contains(@address, "@")]')
, WithInvalidEmail = @x.query('/xml/customer/email[contains(@address, "@")] = False');
Pengembalian:
╔═══════════════════════════════════════╦══════════════════╗
║ WithValidEmail ║ WithInvalidEmail ║
╠═══════════════════════════════════════╬══════════════════╣
║ <email address="[email protected]" /> ║ ║
║ <email address="[email protected]" /> ║ false ║
╚═══════════════════════════════════════╩══════════════════╝
Kueri ini:
SELECT WithInValidEmail = @x.query('/xml/customer/email')
WHERE @x.exist('/xml/customer/email[contains(@address, "@")]') = 0;
Pengembalian:
╔══════════════════╗
║ WithInValidEmail ║
╚══════════════════╝
(no results)
The WHERE
klausul dalam query di atas adalah menghilangkan seluruh himpunan XML karena setidaknya urutan tunggal ada di mana alamat email berisi tanda "@".
sumber