2012-05-22 136 views
2

掙扎我掙扎使用XML節點時,讓我的頭圍繞XML命名空間查詢 - 我在這裏讀其他類似的問題和文章here但不能將此應用到我的具體查詢。與XML命名空間節點查詢

我有這個樣本XML的放大版本:

SELECT CAST('<Navigator clsid="{ABF8EB05-610D-11D3-A53E-684F745C}"> 
    <Version>7</Version> 
    <NavigatorNodes clsid="{0484ACB1-78F5-48D7-A26B-3A0ACD3A5F91}"> 
    <NavigatorNode clsid="{59AB61BD-7833-4954-94C2-B6D737E3DD6F}"> 
     <Indent>0</Indent> 
     <Caption>Contact</Caption> 
     <Selected>0</Selected> 
     <ViewType>0</ViewType> 
     <ViewSubType>0</ViewSubType> 
     <ShowTotals>0</ShowTotals> 
     <RowHeight>0</RowHeight> 
     <Complement>0</Complement> 
     <FreezeFirst>0</FreezeFirst> 
     <a1:QueryFilter xmlns:a2="http://www.ascenttechnology.co.uk/schemas/entities" xmlns:xsi="http://www.w3.org/21/XMLSchema-instance" xmlns:a1="http://www.ascenttechnology.co.uk/schemas/services" Type="QueryFilter"> 
     <entity_name>Contact</entity_name> 
     <relationship_name /> 
     <relationship_from_entity_name /> 
     <a1:ColumnInfoCollection Type="ColumnInfoCollection" AscentCollection="true"> 
      <__vers>1</__vers> 
      <IgnoreMissingItem>0</IgnoreMissingItem> 
      <ReadOnly>0</ReadOnly> 
      <AllowNulls>0</AllowNulls> 
      <a1:ColumnInfo Type="ColumnInfo"> 
      <attribute_name>state</attribute_name> 
      <width>0</width> 
      </a1:ColumnInfo> 
      <a1:ColumnInfo Type="ColumnInfo"> 
      <attribute_name>current_address_suffix</attribute_name> 
      <width>0</width> 
      </a1:ColumnInfo> 
     </a1:ColumnInfoCollection> 
     <a1:SurveyColumnInfoCollection Type="AttributeInfoCollection" AscentCollection="true"> 
      <__vers>1</__vers> 
      <IgnoreMissingItem>0</IgnoreMissingItem> 
      <ReadOnly>0</ReadOnly> 
      <AllowNulls>0</AllowNulls> 
     </a1:SurveyColumnInfoCollection> 
     <a1:WorkflowColumnInfoCollection Type="AttributeInfoCollection" AscentCollection="true"> 
      <__vers>1</__vers> 
      <IgnoreMissingItem>0</IgnoreMissingItem> 
      <ReadOnly>0</ReadOnly> 
      <AllowNulls>0</AllowNulls> 
     </a1:WorkflowColumnInfoCollection> 
     <a1:ConditionCollection Type="ConditionCollection" AscentCollection="true"> 
      <__vers>1</__vers> 
      <IgnoreMissingItem>0</IgnoreMissingItem> 
      <ReadOnly>0</ReadOnly> 
      <AllowNulls>0</AllowNulls> 
      <a1:Condition Type="Condition"> 
      <attribute_name>class</attribute_name> 
      <description1 /> 
      <description2 /> 
      <attribute_id>0</attribute_id> 
      <not>0</not> 
      <operator>28</operator> 
      <value1>1</value1> 
      <value2 xsi:nil="true" /> 
      <indent_level>0</indent_level> 
      <value1_is_reference>0</value1_is_reference> 
      <value2_is_reference>0</value2_is_reference> 
      </a1:Condition> 
      <a1:Condition Type="Condition"> 
      <attribute_name>do_not_mail</attribute_name> 
      <description1 /> 
      <description2 /> 
      <attribute_id>0</attribute_id> 
      <not>0</not> 
      <operator>0</operator> 
      <value1>True</value1> 
      <value2 xsi:nil="true" /> 
      <indent_level>0</indent_level> 
      <value1_is_reference>0</value1_is_reference> 
      <value2_is_reference>0</value2_is_reference> 
      </a1:Condition> 
      <a1:Condition Type="Condition"> 
      <attribute_name>gone_away</attribute_name> 
      <description1 /> 
      <description2 /> 
      <attribute_id>0</attribute_id> 
      <not>0</not> 
      <operator>0</operator> 
      <value1>True</value1> 
      <value2 xsi:nil="true" /> 
      <indent_level>0</indent_level> 
      <value1_is_reference>0</value1_is_reference> 
      <value2_is_reference>0</value2_is_reference> 
      </a1:Condition> 
      <a1:Condition Type="Condition"> 
      <attribute_name>dp_opt_out</attribute_name> 
      <description1 /> 
      <description2 /> 
      <attribute_id>0</attribute_id> 
      <not>0</not> 
      <operator>0</operator> 
      <value1>True</value1> 
      <value2 xsi:nil="true" /> 
      <indent_level>0</indent_level> 
      <value1_is_reference>0</value1_is_reference> 
      <value2_is_reference>0</value2_is_reference> 
      </a1:Condition> 
      <a1:Condition Type="Condition"> 
      <attribute_name>deceased</attribute_name> 
      <description1 /> 
      <description2 /> 
      <attribute_id>0</attribute_id> 
      <not>0</not> 
      <operator>0</operator> 
      <value1>True</value1> 
      <value2 xsi:nil="true" /> 
      <indent_level>0</indent_level> 
      <value1_is_reference>0</value1_is_reference> 
      <value2_is_reference>0</value2_is_reference> 
      </a1:Condition> 
      <a1:Condition Type="Condition"> 
      <attribute_name>deleted</attribute_name> 
      <description1 /> 
      <description2 /> 
      <attribute_id>0</attribute_id> 
      <not>0</not> 
      <operator>0</operator> 
      <value1>True</value1> 
      <value2 xsi:nil="true" /> 
      <indent_level>0</indent_level> 
      <value1_is_reference>0</value1_is_reference> 
      <value2_is_reference>0</value2_is_reference> 
      </a1:Condition> 
      <a1:Condition Type="Condition"> 
      <attribute_name>Date_Of_Birth</attribute_name> 
      <description1 /> 
      <description2 /> 
      <attribute_id>0</attribute_id> 
      <not>0</not> 
      <operator>19</operator> 
      <value1>2010-06-23T::.+:</value1> 
      <value2 xsi:nil="true" /> 
      <indent_level>0</indent_level> 
      <value1_is_reference>0</value1_is_reference> 
      <value2_is_reference>0</value2_is_reference> 
      </a1:Condition> 
      <a1:Condition Type="Condition"> 
      <attribute_name>home_address1</attribute_name> 
      <description1 /> 
      <description2 /> 
      <attribute_id>0</attribute_id> 
      <not>0</not> 
      <operator>26</operator> 
      <value1 xsi:nil="true" /> 
      <value2 xsi:nil="true" /> 
      <indent_level>0</indent_level> 
      <value1_is_reference>0</value1_is_reference> 
      <value2_is_reference>0</value2_is_reference> 
      </a1:Condition> 
      <a1:Condition Type="Condition"> 
      <attribute_name>home_postcode</attribute_name> 
      <description1 /> 
      <description2 /> 
      <attribute_id>0</attribute_id> 
      <not>0</not> 
      <operator>26</operator> 
      <value1 xsi:nil="true" /> 
      <value2 xsi:nil="true" /> 
      <indent_level>1</indent_level> 
      <value1_is_reference>0</value1_is_reference> 
      <value2_is_reference>0</value2_is_reference> 
      </a1:Condition> 
      <a1:Condition Type="Condition"> 
      <attribute_name>home_address1</attribute_name> 
      <description1 /> 
      <description2 /> 
      <attribute_id>0</attribute_id> 
      <not>0</not> 
      <operator>0</operator> 
      <value1 /> 
      <value2 xsi:nil="true" /> 
      <indent_level>0</indent_level> 
      <value1_is_reference>0</value1_is_reference> 
      <value2_is_reference>0</value2_is_reference> 
      </a1:Condition> 
      <a1:Condition Type="Condition"> 
      <attribute_name>home_postcode</attribute_name> 
      <description1 /> 
      <description2 /> 
      <attribute_id>0</attribute_id> 
      <not>0</not> 
      <operator>0</operator> 
      <value1 /> 
      <value2 xsi:nil="true" /> 
      <indent_level>1</indent_level> 
      <value1_is_reference>0</value1_is_reference> 
      <value2_is_reference>0</value2_is_reference> 
      </a1:Condition> 
     </a1:ConditionCollection> 
     <a1:OrderByCollection Type="OrderByCollection" AscentCollection="true"> 
      <__vers>1</__vers> 
      <IgnoreMissingItem>0</IgnoreMissingItem> 
      <ReadOnly>0</ReadOnly> 
      <AllowNulls>0</AllowNulls> 
     </a1:OrderByCollection> 
     <SelectedArticles xsi:nil="true" /> 
     <groupby_field_name /> 
     <reporting_currency>52</reporting_currency> 
     <Negate_Filter>0</Negate_Filter> 
     <Survey_Id>----</Survey_Id> 
     <Survey_Name xsi:nil="true" /> 
     <Workflow_Id>----</Workflow_Id> 
     <Workflow_Name xsi:nil="true" /> 
     <XPath>0</XPath> 
     <locale>0</locale> 
     </a1:QueryFilter> 
    </NavigatorNode> 
    </NavigatorNodes> 
</Navigator>' AS XML) as xmldata 

而且一直在試圖查詢來獲得包含在A1中的信息:條件節點。我試着查詢,如:

;WITH XMLNAMESPACES(
'http://www.ascenttechnology.co.uk/schemas/services' AS a1 
,'http://www.ascenttechnology.co.uk/schemas/entities' AS a2 
,DEFAULT 'http://www.w3.org/21/XMLSchema-instance' 
) 
SELECT 
#xmldata.* 
,ref.value('attribute_name[1]','NVARCHAR(50)') as attribute_name 
FROM #xmldata 
CROSS APPLY xmldata.nodes('/Navigator/NavigatorNodes/NavigatorNode/a1:QueryFilter/a1:ConditionCollection/a1:Condition') T(ref) 

但他們總是回來的空白時,我得到了命名空間節點近 - 我能拉從節點的數據一路攀升至'/Navigator/NavigatorNodes/NavigatorNode'但隨後有沒有運氣。

謝謝!

回答

1

您聲明DEFAULT 'http://www.w3.org/21/XMLSchema-instance'但你的XML字符串甚至不會有一個默認的命名空間。

嘗試

WITH XMLNAMESPACES(
    'http://www.ascenttechnology.co.uk/schemas/services' AS a1 
, 'http://www.ascenttechnology.co.uk/schemas/entities' AS a2 
) 
+0

認爲這可能做的伎倆 - 因爲我是新來的XML命名空間,我怎麼會知道,如果有一個默認的命名空間包含在內? – Dibstar

+0

@Davin:當有一個'的xmlns =「...」'聲明在頂層元素。普通的命名空間得到分配的前綴,如'的xmlns:前綴=「命名空間URI」',默認的命名空間沒有。 – Tomalak

+0

這非常奏效,並且學到了新的東西。謝謝! – Dibstar