SQL查找XML节点值

SQL查找XML节点值,sql,xml,xpath,namespaces,sccm,Sql,Xml,Xpath,Namespaces,Sccm,在这里搜索并尝试了所有示例,但没有运气。我想从此XML中查找节点“CustomID”的值: <?xml version="1.0" encoding="UTF-8"?> <AppMgmtDigest xmlns="http://schemas.microsoft.com/SystemCenterConfigurationManager/2009/AppMgmtDigest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance

在这里搜索并尝试了所有示例,但没有运气。我想从此XML中查找节点“CustomID”的值:

<?xml version="1.0" encoding="UTF-8"?>
<AppMgmtDigest xmlns="http://schemas.microsoft.com/SystemCenterConfigurationManager/2009/AppMgmtDigest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Application AuthoringScopeId="ScopeId_B831987C-E9E7-47D5-95A5-1EEAC7B035D5" LogicalName="Application_d030d07c-a5ef-419b-aa46-44ff0697050b" Version="13">
        <DisplayInfo DefaultLanguage="en-US">
            <Info Language="en-US">
                <Title>Adobe Acrobat Professional XI</Title>
                <Publisher>Adobe</Publisher>
                <Version>11.0.00</Version>
                <Tags>
                    <Tag>Adobe Acrobat Professional XI</Tag>
                </Tags>
            </Info>
        </DisplayInfo>
        <DeploymentTypes>
            <DeploymentType AuthoringScopeId="ScopeId_B831987C-E9E7-47D5-95A5-1EEAC7B035D5" LogicalName="DeploymentType_9193d006-04f2-41e5-8495-62bedf3f79e4" Version="10" />
        </DeploymentTypes>
        <Title ResourceId="Res_814073674">Adobe Acrobat Professional XI</Title>
        <Description ResourceId="Res_469365393" />
        <Publisher ResourceId="Res_231516204">Adobe</Publisher>
        <SoftwareVersion ResourceId="Res_266572543">11.0.00</SoftwareVersion>
        <CustomId ResourceId="Res_1915338015">**THIS IS ANSWER**</CustomId>
        <AutoInstall>true</AutoInstall>
        <Owners>
            <User Qualifier="LogonName" Id="abc" />
        </Owners>
        <Contacts>
            <User Qualifier="LogonName" Id="abc" />
        </Contacts>
        <HighPriority>1</HighPriority>
        <AutoDistribute>true</AutoDistribute>
    </Application>
</AppMgmtDigest>

您的XML在XPath表达式中需要考虑的命名空间。

您可以使用以下XPath表达式提取所需的文本,而无需考虑命名空间:

//*[local-name()='AppMgmtDigest']/*[local-name()='Application']/*[local-name()='CustomId'] 
您可以尝试:

select a.b.value('.','varchar(max)') 
    from @xml.nodes('//*[local-name()="AppMgmtDigest"]/*[local-name()="Application"]/*[local-name()="CustomId"]') a(b);
您还可以注册名称空间。您可能需要在
选择
查询之前添加以下内容:

;with xmlnamespaces ('http://schemas.microsoft.com/SystemCenterConfigurationManager/2009/AppMgmtDige‌​st' as prefix)
其中
prefix
是您想要的任何前缀。然后使用前缀限定XPath元素:

//prefix:AppMgmtDigest/prefix:Application/prefix:CustomId

您需要注册名称空间
”http://schemas.microsoft.com/SystemCenterConfigurationManager/2009/AppMgmtDigest“
或者忽略名称空间调用XPath表达式。谢谢你,我的朋友……我希望我能在两天前问这个问题并节省时间。再次感谢,成功了!
//prefix:AppMgmtDigest/prefix:Application/prefix:CustomId