【问题标题】:Finding the relevant records using XQuery/XPath使用 XQuery/XPath 查找相关记录
【发布时间】:2012-02-02 03:06:39
【问题描述】:

我对 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"

我唯一的问题是,如果第一个地址块包含 addressline1 而第二个地址块包含城市,则此语句也会返回记录。

【问题讨论】:

    标签: sql xpath xquery


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-04
      • 2013-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多