属性的XML方法在SQL Server 2012中获取NULL值

问题描述 投票:1回答:1

我想从节点Name中提取属性<Adapter>的单个值。但是我得到NULL值。有人可以帮我吗?

样本数据:

<SqlSyncProviderScopeConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" IsTemplate="false">
  <Adapter Name="[Kit].[KIT_Kit]" GlobalName="[Kit].[KIT_Kit]" TrackingTable="[DataSync].[KIT_Kit_dss_tracking]" SelChngProc="[DataSync].[KIT_Kit_dss_selectchanges_9f5d350b-a9f7-48d2-8398-0a0d6de7595c]" SelRowProc="[DataSync].[KIT_Kit_dss_selectrow_9f5d350b-a9f7-48d2-8398-0a0d6de7595c]" InsProc="[DataSync].[KIT_Kit_dss_insert_9f5d350b-a9f7-48d2-8398-0a0d6de7595c]" UpdProc="[DataSync].[KIT_Kit_dss_update_9f5d350b-a9f7-48d2-8398-0a0d6de7595c]" DelProc="[DataSync].[KIT_Kit_dss_delete_9f5d350b-a9f7-48d2-8398-0a0d6de7595c]" InsMetaProc="[DataSync].[KIT_Kit_dss_insertmetadata]" UpdMetaProc="[DataSync].[KIT_Kit_dss_updatemetadata]" DelMetaProc="[DataSync].[KIT_Kit_dss_deletemetadata]" BulkTableType="[DataSync].[KIT_Kit_dss_BulkType_9f5d350b-a9f7-48d2-8398-0a0d6de7595c]" BulkInsProc="[DataSync].[KIT_Kit_dss_bulkinsert_9f5d350b-a9f7-48d2-8398-0a0d6de7595c]" BulkUpdProc="[DataSync].[KIT_Kit_dss_bulkupdate_9f5d350b-a9f7-48d2-8398-0a0d6de7595c]" BulkDelProc="[DataSync].[KIT_Kit_dss_bulkdelete_9f5d350b-a9f7-48d2-8398-0a0d6de7595c]" InsTrig="[Kit].[KIT_Kit_dss_insert_trigger]" UpdTrig="[Kit].[KIT_Kit_dss_update_trigger]" DelTrig="[Kit].[KIT_Kit_dss_delete_trigger]">
    <Col name="KitGuid" type="uniqueidentifier" size="16" param="@P_1" pk="true" />
    <Col name="KitHrid" type="nvarchar" size="50" param="@P_2" collation="SQL_Latin1_General_CP1_CI_AS" />
    <Col name="StudyGuid" type="uniqueidentifier" size="16" param="@P_3" />
    <Col name="StudyVersionGuid" type="uniqueidentifier" size="16" param="@P_4" />
    <Col name="StudyKitDefGuid" type="uniqueidentifier" size="16" param="@P_5" />
    <Col name="SiteGuid" type="uniqueidentifier" size="16" param="@P_6" />
    <Col name="SourceSystemName" type="nvarchar" size="50" param="@P_7" collation="SQL_Latin1_General_CP1_CI_AS" />
    <Col name="ReplacedByGuid" type="uniqueidentifier" size="16" null="true" param="@P_8" />
    <Col name="FirstScannedOn" type="datetimeoffset" size="10" null="true" param="@P_9" />
    <Col name="SiteType" type="nvarchar" size="50" null="true" param="@P_10" collation="SQL_Latin1_General_CP1_CI_AS" />
    <Col name="CtmOrderGuid" type="uniqueidentifier" size="16" null="true" param="@P_11" />
    <Col name="EarlyExpirationOn" type="date" size="3" null="true" param="@P_12" />
    <Col name="DiscardedOn" type="datetimeoffset" size="10" null="true" param="@P_13" />
    <Col name="CreatedOn" type="datetimeoffset" size="10" param="@P_14" />
    <Col name="CreatedById" type="nvarchar" size="300" null="true" param="@P_15" collation="SQL_Latin1_General_CP1_CI_AS" />
    <Col name="ModifiedOn" type="datetimeoffset" size="10" null="true" param="@P_16" />
    <Col name="ModifiedById" type="nvarchar" size="300" null="true" param="@P_17" collation="SQL_Latin1_General_CP1_CI_AS" />
    <Col name="JobId" type="uniqueidentifier" size="16" null="true" param="@P_18" />
  </Adapter>  
</SqlSyncProviderScopeConfiguration>

这是我的查询:

;with xmlnamespaces('http://www.w3.org/2001/XMLSchema' as ns,
                    'http://www.w3.org/2001/XMLSchema-instance' as nf)
select config_data.value('(/ns:SqlSyncProviderScopeConfiguration/ns:Adapter/@name)[1]','varchar(100)')
from tableA

所需的输出:

[Kit].[KIT_Kit]

我不知道为什么输出为NULL?

sql-server xml-parsing
1个回答
2
投票

根节点上的这两个XML名称空间不是真正相关的,它们都不是default名称空间(仅使用xmlns标记定义-不提供前缀)-它们只会应用如果XML节点使用了它们各自的前缀-示例中的所有节点都不是。

因此您应该能够使用以下方式获得所需的输出:

select 
    config_data.value('(/SqlSyncProviderScopeConfiguration/Adapter/@name)[1]','varchar(100)')
from tableA

如果不使用它们,则无需定义XML名称空间声明。...

© www.soinside.com 2019 - 2024. All rights reserved.