我在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>
有&#34; LengthsOfStay&#34;我现在试图点击的节点,我的SQL在下面。我认为这可能与所有包含&#34; LengthsOfStay&#34;的消息有关。节点
;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)
答案 0 :(得分: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)
你应该得到行。裸的初始/
使其返回到文档根目录以查找您命名的节点。通过使用./
或不使用前缀,您可以说“从当前上下文中搜索”。