【问题标题】:Count the Occurrences of an Attribute?计算属性的出现次数?
【发布时间】:2013-05-22 18:22:22
【问题描述】:

我在一个看起来像这样的大表中有一个 XML 列,在 TSQL 中我将如何计算属性 (LCPID) 出现的次数?

谢谢,


代码:

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <PingAutoRequest xmlns="http://Reply.LeadMarketPlace.Services.Ping.ServiceContracts/2007/10/ServiceContracts">
      <AutomotiveLead>
        <LCPId>766</LCPId>
        <Zipcode>33544</Zipcode>
        <Make>Ford</Make>
        <Model>Escape</Model>
        <LeadType>New</LeadType>
        <Year>2013</Year>
        <Trim>FWD S</Trim>
        <ExteriorColor />
        <InteriorColor />
        <Transmission />
        <TradeIn>false</TradeIn>
        <LastFourPhoneDigits />
        <LastName />
      </AutomotiveLead>
    </PingAutoRequest>
  </soap:Body>
</soap:Envelope>

【问题讨论】:

    标签: sql-server xml tsql select


    【解决方案1】:

    好的,因为您的 XML 不包含任何 attributes(命名空间除外),所以我假设您的意思是要计算 &lt;LCPId&gt; elements 的数量。如果是这样,那么你可以这样做......

    ;WITH XMLNAMESPACES ('http://Reply.LeadMarketPlace.Services.Ping.ServiceContracts/2007/10/ServiceContracts' as ns) 
    SELECT XmlColumn.value('(count(//ns:LCPId))','int')
    FROM YourTableName
    

    注意,我们需要处理XML namespace using the WITH statement,分号不是错误的。然后我们用XPath 表达式count(//ns:LCPId) 计算元素的数量。

    您可以在 SQL Fiddle here 看到它的实际效果。

    【讨论】:

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