使用XQuery / XPath查找相关记录

时间:2012-01-04 06:44:24

标签: sql xpath xquery

我对XQUERY / XPATH非常新手:)所以我很可能会以错误的方式解决这个问题。我有一个客户对象序列化并以下列格式存储在数据库列中。

<Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Addresses>
<AddressBlock>
  <AddressLine1>1234 SomeStreet Ave.</AddressLine1>
  <City>SomeCity</City>
  <State>SomeState</State>
  <Zipcode>SomeZip</Zipcode>
</AddressBlock>
<AddressBlock>
  <AddressLine1>5678 SomeOtherStreet Ave.</AddressLine1>
  <City>SomeOtherCity</City>
  <State>SomeOtherState</State>
  <Zipcode>SomeOtherZip</Zipcode>
</AddressBlock>
</Addresses>
</Customer>

如果同一地址块中的addressline1和city包含某些关键字,我正在寻找一种方法来选择此记录。我有以下声明,几乎可以完成我正在寻找的内容。

select * 
from users 
where [UserData].exist('/Customer/Addresses/AddressBlock/AddressLine1/text()[contains(upper-case(.),""SOMESTREET"")]')=1
and [UserData].exist('/Customer/Addresses/AddressBlock/City/text()[contains(upper-case(.),""SOMECITY"")]')=1"

我唯一的问题是,如果第一个地址区块包含地址线1且第二个地址区块包含城市,则此语句也将返回记录。

1 个答案:

答案 0 :(得分:0)

您必须在同一个XQuery中测试这两个条件。

select * 
from users 
where [UserData].exist('/Customer/Addresses/AddressBlock
                         [contains(upper-case(AddressLine1[1]),"SOMESTREET") and 
                          contains(upper-case(City[1]),"SOMECITY")]')=1