2016-01-15 74 views
1

爲什麼我得到第二個SELECT查詢的空白結果,第一個查詢返回結果。對於具有默認命名空間的元素,查詢XML數據失敗

Declare @myxml xml; 
set @myxml = '<items count="2"> 
    <item id="561" quantity="1" /> 
    <item id="167" quantity="1" /> 
</items>' 

select @myxml.query('//items'); 
Go 
Declare @myxml xml; 
set @myxml = '<ContactLog xmlns="http://adventure-works/contact"> 
    <Contact Date="2008-09-02T10:12:00" Type="Email"> 
    <Employee>adventure-works\linda3</Employee> 
    <Notes>Customer confirmed delivery of order 71935.</Notes> 
    </Contact> 
    <Contact Date="2008-06-02T15:07:00" Type="Phone"> 
    <Employee>adventure-works\david8</Employee> 
    <Employee>adventure-works\linda3</Employee> 
    <Notes>Customer requested status update on order 71935. Informed customer it is being prepared for delivery.</Notes> 
    </Contact> 
</ContactLog>'; 

select @myxml.query('//ContactLog'); 
+0

*默認命名空間如何處理XML命名空間*是關鍵字.. – har07

+0

har07對不起,但沒有得到它,請解釋。 – TechTurtle

回答

1

你應該在正確的命名空間查詢。你的第二個SELECT應改爲:

select @myxml.query('declare namespace pd="http://adventure-works/contact"; //pd:ContactLog') ; 

寫這個的另一種方法如下:

WITH XMLNAMESPACES ('http://adventure-works/contact' AS pd) 
SELECT @myxml.query('//pd:ContactLog'); 

你可以閱讀更多有關SQL Server here

+0

謝謝TT,它有效。 – TechTurtle

相關問題