考虑以下简单的XML:
<xml>
<customer name="Max">
<email address="me@you.com" />
</customer>
<customer name="Erik">
<email address="erik@your-mom.com" />
</customer>
<customer name="Brent">
<email address="brentcom" />
</customer>
</xml>
我想获取<Customer>
序列列表,其中address
该<email>
项目的属性不包含@
。
所以,我想要的输出看起来像:
<customer name="Brent">
<email address="brentcom" />
</customer>
mcve:
DECLARE @x XML = '<xml>
<customer name="Max"><email address="me@you.com" /></customer>
<customer name="Erik"><email address="erik@your-mom.com" /></customer>
<customer name="Brent"><email address="brentcom" /></customer>
</xml>';
该查询:
SELECT WithValidEmail = @x.query('/xml/customer/email[contains(@address, "@")]')
, WithInvalidEmail = @x.query('/xml/customer/email[contains(@address, "@")] = False');
返回值:
╔═══════════════════════════════════════╦══════════════════╗
║ WithValidEmail ║ WithInvalidEmail ║
╠═══════════════════════════════════════╬══════════════════╣
║ <email address="me@you.com" /> ║ ║
║ <email address="erik@your-mom.com" /> ║ false ║
╚═══════════════════════════════════════╩══════════════════╝
该查询:
SELECT WithInValidEmail = @x.query('/xml/customer/email')
WHERE @x.exist('/xml/customer/email[contains(@address, "@")]') = 0;
返回值:
╔══════════════════╗
║ WithInValidEmail ║
╚══════════════════╝
(no results)
WHERE
上面查询中的子句消除了整个XML集,因为在电子邮件地址包含“ @”符号的地方至少存在一个序列。