Considérez le XML simple suivant:
<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>
Je veux obtenir une liste de <Customer>
séquences où l' address
attribut de l' <email>
élément ne contient pas de @
.
Donc, je veux une sortie qui ressemble à:
<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>';
Cette requête:
SELECT WithValidEmail = @x.query('/xml/customer/email[contains(@address, "@")]')
, WithInvalidEmail = @x.query('/xml/customer/email[contains(@address, "@")] = False');
Retour:
╔═══════════════════════════════════════╦══════════════════╗
║ WithValidEmail ║ WithInvalidEmail ║
╠═══════════════════════════════════════╬══════════════════╣
║ <email address="[email protected]" /> ║ ║
║ <email address="[email protected]" /> ║ false ║
╚═══════════════════════════════════════╩══════════════════╝
Cette requête:
SELECT WithInValidEmail = @x.query('/xml/customer/email')
WHERE @x.exist('/xml/customer/email[contains(@address, "@")]') = 0;
Retour:
╔══════════════════╗
║ WithInValidEmail ║
╚══════════════════╝
(no results)
La WHERE
clause de la requête ci-dessus élimine l'ensemble complet de XML car au moins une seule séquence existe où l'adresse e-mail contient un signe "@".
la source