0

我在 SSMS 2008 中运行以下命令,以尝试从节点中获取数据,以便于搜索和限制值。这是供应商发送给我们的 XML,我们经常需要搜索以查找发送的消息,目前,它没有针对在表级别拆分数据进行优化。(目前这是不可能的)。MessageStringXML 是 XML 数据类型的字段。我从以下查询中没有得到任何结果。

;WITH XMLNAMESPACES(DEFAULT 'http://www.opentravel.org/OTA/2003/05')
Select Case When MessageStringXML.value('(/OTA_HotelAvailNotifRQ/@EchoToken)[1]','varchar(255)') IS Not Null Then MessageStringXML.value('(/OTA_HotelAvailNotifRQ/@EchoToken)[1]','varchar(255)')
            When MessageStringXML.value('(/OTA_HotelAvailNotifRS/@EchoToken)[1]','varchar(255)') IS Not Null Then MessageStringXML.value('(/OTA_HotelAvailNotifRS/@EchoToken)[1]','varchar(255)')
            When MessageStringXML.value('(/OTA_HotelRateAmountNotifRQ/@EchoToken)[1]','varchar(255)') IS Not Null Then MessageStringXML.value('(/OTA_HotelRateAmountNotifRQ/@EchoToken)[1]','varchar(255)')
            When MessageStringXML.value('(/OTA_HotelRateAmountNotifRS/@EchoToken)[1]','varchar(255)') IS Not Null Then MessageStringXML.value('(/OTA_HotelRateAmountNotifRS/@EchoToken)[1]','varchar(255)')
       End as EchoToken
      ,Toddler.value('@InvCode','varchar(255)') as RoomTypeCode
      ,Toddler.value('@RatePlanCode','varchar(255)') as RateCode
      ,Infant.value('@Status','varchar(255)') as 'Status'
From #Gamma
    Cross Apply MessageStringXML.nodes('/OTA_HotelAvailNotifRQ/AvailStatusMessages/AvailStatusMessage') AS N(Child)
    Cross Apply Child.nodes('/StatusApplicationControl') AS O(Toddler)
    Cross Apply Child.nodes('/RestrictionStatus') AS P(Infant)

下面是我尝试解析的 XML 消息的示例:

<OTA_HotelAvailNotifRQ xmlns="http://www.opentravel.org/OTA/2003/05" EchoToken="123456789" TimeStamp="2013-08-13T10:56:25.32-05:00" Target="Production" Version="1.001" PrimaryLangID="en-us">
  <POS>
    <Source>
      <RequestorID Type="18" ID="AAAAAA" />
    </Source>
  </POS>
  <AvailStatusMessages ChainCode="BB" BrandCode="CC" HotelCode="12345">
    <AvailStatusMessage>
      <StatusApplicationControl Start="2014-05-11" End="2014-07-09" InvCodeApplication="InvCode" InvCode="DDD" RatePlanCodeType="RatePlanCode" RatePlanCode="EEE" RateTier="8" IsRoom="1" Override="1" />
      <RestrictionStatus Restriction="Master" Status="Open" />
    </AvailStatusMessage>
    <AvailStatusMessage>
      <StatusApplicationControl Start="2014-05-11" End="2014-07-09" InvCodeApplication="InvCode" InvCode="FFF" RatePlanCodeType="RatePlanCode" RatePlanCode="GGG" RateTier="9" IsRoom="1" Override="1" />
      <RestrictionStatus Restriction="Master" Status="Close" />
    </AvailStatusMessage>
  </AvailStatusMessages>
</OTA_HotelAvailNotifRQ>

编辑:

效果非常好!!!谢谢!我还没有找到很多好的文档来源,所以我很感激。如果我尝试进一步深入到另一个 XML,

<OTA_HotelAvailNotifRQ xmlns="http://www.opentravel.org/OTA/2003/05" EchoToken="34496481-1" PrimaryLangID="en-us" Target="Production" TimeStamp="2013-09-06T05:50:37.01+00:00" Version="1.002">
  <POS>
    <Source>
      <RequestorID ID="R_GoldenTulip" Type="18" />
    </Source>
  </POS>
  <AvailStatusMessages BrandCode="GT" ChainCode="GT" HotelCode="040510">
    <AvailStatusMessage>
      <StatusApplicationControl End="2013-10-06" Fri="true" InvCode="ROH" InvCodeApplication="InvCode" Mon="true" RatePlanCode="EXPED1" RatePlanCodeType="RatePlanCode" Sat="true" Start="2013-10-04" Sun="true" Thur="true" Tue="true" Weds="true" />
      <RestrictionStatus Restriction="Master" Status="Open" />
    </AvailStatusMessage>
    <AvailStatusMessage>
      <StatusApplicationControl End="2013-10-06" Fri="true" InvCode="ROH" InvCodeApplication="InvCode" Mon="true" RatePlanCode="EXPED1" RatePlanCodeType="RatePlanCode" Sat="true" Start="2013-10-04" Sun="true" Thur="true" Tue="true" Weds="true" />
      <LengthsOfStay ArrivalDateBased="true" FixedPatternLength="1">
        <LengthOfStay MinMaxMessageType="FullPatternLOS" Time="1" TimeUnit="Day" />
      </LengthsOfStay>
    </AvailStatusMessage>
    <AvailStatusMessage>
      <StatusApplicationControl End="2013-10-06" Fri="true" InvCode="ROH" InvCodeApplication="InvCode" Mon="true" RatePlanCode="EXPED1" RatePlanCodeType="RatePlanCode" Sat="true" Start="2013-10-04" Sun="true" Thur="true" Tue="true" Weds="true" />
      <LengthsOfStay ArrivalDateBased="false" FixedPatternLength="1">
        <LengthOfStay MinMaxMessageType="FullPatternLOS" Time="1" TimeUnit="Day" />
      </LengthsOfStay>
    </AvailStatusMessage>
  </AvailStatusMessages>
</OTA_HotelAvailNotifRQ>

我现在尝试点击“LengthsOfStay”节点,我的 SQL 在下面。我认为这可能与并非所有具有“LengthsOfStay”节点的消息有关

;WITH XMLNAMESPACES(DEFAULT 'http://www.opentravel.org/OTA/2003/05')

Select 
[MessageBodyID],
Case When MessageStringXML.value('(/OTA_HotelAvailNotifRQ/@EchoToken)[1]','varchar(255)') IS Not Null Then MessageStringXML.value('(/OTA_HotelAvailNotifRQ/@EchoToken)[1]','varchar(255)')
            When MessageStringXML.value('(/OTA_HotelAvailNotifRS/@EchoToken)[1]','varchar(255)') IS Not Null Then MessageStringXML.value('(/OTA_HotelAvailNotifRS/@EchoToken)[1]','varchar(255)')
            When MessageStringXML.value('(/OTA_HotelRateAmountNotifRQ/@EchoToken)[1]','varchar(255)') IS Not Null Then MessageStringXML.value('(/OTA_HotelRateAmountNotifRQ/@EchoToken)[1]','varchar(255)')
            When MessageStringXML.value('(/OTA_HotelRateAmountNotifRS/@EchoToken)[1]','varchar(255)') IS Not Null Then MessageStringXML.value('(/OTA_HotelRateAmountNotifRS/@EchoToken)[1]','varchar(255)')
       End as EchoToken
      ,Toddler.value('@InvCode','varchar(255)') as RoomTypeCode
      ,Toddler.value('@Start','varchar(255)') as FromDate
      ,Toddler.value('@End','varchar(255)') as ToDate
      ,Toddler.value('@RatePlanCode','varchar(255)') as RateCode
      ,Infant.value('@Status','varchar(255)') as 'Status'
      ,Fetus.value('@ArrivalDateBased','varchar(255)') as 'FPLOS'

From #Alpha
    Cross Apply MessageStringXML.nodes('OTA_HotelAvailNotifRQ/AvailStatusMessages') AS N(Tween)
    Cross Apply Tween.nodes('AvailStatusMessage') AS Q(Child)
    Cross Apply Child.nodes('StatusApplicationControl') AS O(Toddler)
    Cross Apply Child.nodes('RestrictionStatus') AS P(Infant)
    Cross Apply Child.nodes('LengthsOfStay') AS R(Fetus)
    --Cross Apply Fetus.nodes('Lengthofstay') AS S(Embryo)
4

1 回答 1

1

改变这个

Cross Apply Child.nodes('/StatusApplicationControl') AS O(Toddler)
Cross Apply Child.nodes('/RestrictionStatus') AS P(Infant)

对此

Cross Apply Child.nodes('StatusApplicationControl') AS O(Toddler)
Cross Apply Child.nodes('RestrictionStatus') AS P(Infant)

或者确实

Cross Apply Child.nodes('./StatusApplicationControl') AS O(Toddler)
Cross Apply Child.nodes('./RestrictionStatus') AS P(Infant)

你应该得到行。裸露的首字母/使它返回到文档根目录以查找您命名的节点。通过使用./或不使用前缀,您可以说“从当前上下文中搜索”。

于 2013-09-12T09:25:24.797 回答