如何从Sql Server中的xml列获取特定属性

时间:2014-07-23 10:19:37

标签: xml xquery-sql

我的数据库中有一个名为MesssageContent的xml列:

<ns1:ProductAvailabilityPublication xmlns:ns0="http://www.example.com/Ecommerce/Worldwide/AvailabilityService/Schemas/ProductResponse" xmlns:ns2="http://www.example.com/Ecommerce/Worldwide/AvailabilityService/Schemas/ProductRequest" xmlns:ns1="http://www.example.com/Ecommerce/Worldwide/AvailabilityService/Schemas/ProductAvailabilityPublish" RequestorID="WCS_AU" BatchID="36dae836-cc16-489c-8009-afab63f92dff">
  <ns0:ProductResponse ProductNumber="C8010A" LeadTime="1" LastUpdated="2014-07-18T11:17:47.677" Region="AP" CountryCode="AU" CurrInv="259" FutureInv="0" CurrEDD="TBA" FutureEDD="TBA" ETA="0" D-Flag="True">
    <ns0:ProductType>
      <ns2:NonStartingPoint />
    </ns0:ProductType>
    <ns0:Status>
      <ns0:Success />
    </ns0:Status>
  </ns0:ProductResponse>
  <ns0:ProductResponse ProductNumber="Q3655A" LeadTime="1" LastUpdated="2014-07-18T11:17:47.677" Region="AP" CountryCode="AU" CurrInv="2" FutureInv="0" CurrEDD="TBA" FutureEDD="TBA" ETA="0" D-Flag="True">
    <ns0:ProductType>
      <ns2:NonStartingPoint />
    </ns0:ProductType>
    <ns0:Status>
      <ns0:Success />
    </ns0:Status>
  </ns0:ProductResponse>
  <ns0:ProductResponse ProductNumber="PP654A" LeadTime="21" LastUpdated="2014-07-18T11:17:47.677" Region="AP" CountryCode="AU" CurrInv="0" FutureInv="0" CurrEDD="TBA" FutureEDD="TBA" ETA="0" D-Flag="False">
    <ns0:ProductType>
      <ns2:NonStartingPoint />
    </ns0:ProductType>
    <ns0:Status>
      <ns0:Success />
    </ns0:Status>
  </ns0:ProductResponse>

</ns1:ProductAvailabilityPublication>

现在我想找出所有&#34; ProductNumber&#34;在&#34; CurrInv&#34;&gt; 0和&#34; D-flag&#34; =&#34; True&#34;记录。我试过但只能找到包含不需要的数据的整个xml colomn。是否有可能找出所有&#34; ProductNumber&#34;满足我的表达?

1 个答案:

答案 0 :(得分:0)

以下XQuery应该可行。您需要绑定正确的命名空间(或使用命名空间通配符*而不是特定的命名空间):

//ns0:ProductResponse[@CurrInv > 0][@D-Flag = "True"]/@ProductNumber/string()