2013-12-13 94 views
0

我在包含XML數據字段的數據庫中有很多行。 XML大約是這樣的:如何在創建SQL請求時忽略XML名稱空間?

<CabasEstimateReply xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://cabmb.cab.se/schemas/CABMBGeneralSchemas/CABASEstimateReply/2006-11-16/"> 
    <Estimate xmlns=""> 
    <WorkshopCompanyId>C002006893</WorkshopCompanyId> 
    <EstimateId>1-SE-AEB965-634921885183891313</EstimateId> 
    </Estimate> 
    <EstimateReply xmlns=""> 
    **<EstimateReplyCode>ReplyStatus1</EstimateReplyCode>** 
    <EstimateReplyVersion>1</EstimateReplyVersion> 
    <EstimateReplyDate>2013-05-31T11:40:18.6227322+03:00</EstimateReplyDate> 
    <EstimateReplyComment /> 
    <EstimateReplyMessage>Kunden betalar :     8692 Fakturaadress :     Trygg Hansa</EstimateReplyMessage> 
    <EstimateReplyMessageCompressMethod /> 
    <EstimateReplyReference>010704</EstimateReplyReference> 
    <EstimateReplyForthcomingInspectionDate /> 
    </EstimateReply> 
    <Vehicle xmlns=""> 
    <VehicleRegNo>XND108</VehicleRegNo> 
    <VehicleMake>BMW</VehicleMake> 
    <VehicleModel>525I TOURING</VehicleModel> 
    <VehicleModelYear /> 
    <VehicleModelMonth /> 
    <VehicleVINCode /> 
    <VehicleChassiNo>NL51010CM95684</VehicleChassiNo> 
    <VehicleFirstRegistered>2006-02-23T00:00:00</VehicleFirstRegistered> 
    <Imported>null</Imported> 
    </Vehicle> 

我需要有一個可能獲得通過SQL請求的值EstimateReplyCode(標記爲粗體)。我這樣做是這樣的:

;WITH XMLNAMESPACES(DEFAULT 'https://cabmb.cab.se/schemas/CABMBGeneralSchemas/CABASEstimateReply/2006-11-16/') 
select [Data], 
Data.value('(/CabasEstimateReply/EstimateReply/EstimateReplyCode)[1]', 'nvarchar(64)') AS ReplyCode 
from EstimateReplyRawData 

但是隻得到ReplyCode的空值。當我試圖將XML轉換爲字符串時,然後替換命名空間,然後轉換爲XML返回一切正常,這就是爲什麼我認爲問題是命名空間。我在這裏做錯了什麼?

回答

2

如果你真的想忽略命名空間,你可以使用命名空間通配符。

select [Data], 
Data.value('(/*:CabasEstimateReply/*:EstimateReply/*:EstimateReplyCode)[1]', 'nvarchar(64)') AS ReplyCode 
from EstimateReplyRawData