从SQL查询中选择XML数据 - 未返回任何值

时间:2021-11-30 21:22:33

Xml Column returns no rows.
Below created a table type and xml value inserted into that table. After executing the below mentioned two queries no value is returned.
Xml data is a valid data.

Xml列不返回任何行。下面创建了一个表类型和插入该表的xml值。执行下面提到的两个查询后,不返回任何值。 Xml数据是有效数据。

DECLARE @Test TABLE (Id INT IDENTITY (1,1), XMLDATA XML)

INSERT INTO @test
SELECT '
<TXLife xmlns="http://ACORD.org/Standards/Life/2" xmlns:tx="http://ACORD.org/Standards/Life/2" Version="2.20.00">
    <TXLifeRequest PrimaryObjectID="Holding_1">
        <CorrelationGUID>4b30545a-158b-441a-a37a-0b259f757059</CorrelationGUID>
    </TXLifeRequest>
</TXLife>'

SELECT 
      Id
    , XMLDATA.query('//CorrelationGUID') AS 'TransType'
    , XMLDATA
FROM @test

SELECT C.value('./CorrelationGUID[1]', 'varchar(50)') AS 'TransType'
FROM @test
CROSS APPLY XMLDATA.nodes('/TXLife/TXLifeRequest') n (C)

3 个解决方案

#1


1  

Well, if I test your code with CREATE TABLE, here and here, the unfiltered select works just fine. However, the second filtered select returns no results.

好吧,如果我用CREATE TABLE测试你的代码,这里和这里,未经过滤的选择工作正常。但是,第二个筛选的选择不返回任何结果。

If I alter the query to to specify the right namespace, like in @Devart's answer,

如果我改变查询以指定正确的命名空间,就像@ Devart的答案一样,

WITH XMLNAMESPACES (DEFAULT 'http://ACORD.org/Standards/Life/2')
SELECT
        C.value('./CorrelationGUID[1]', 'varchar(50)') AS 'TransType'
    FROM
        Test
    CROSS APPLY
        XMLDATA.nodes('/TXLife/TXLifeRequest') n (C)

You can see, it works as expected.

你可以看到,它按预期工作。

#2


1  

Try this one -

试试这个 -

DECLARE @XML XML
SELECT  @XML = '
<TXLife xmlns="http://ACORD.org/Standards/Life/2" xmlns:tx="http://ACORD.org/Standards/Life/2" Version="2.20.00">
    <TXLifeRequest PrimaryObjectID="Holding_1">
        <CorrelationGUID>4b30545a-158b-441a-a37a-0b259f757059</CorrelationGUID>
    </TXLifeRequest>
</TXLife>'

;WITH XMLNAMESPACES (DEFAULT 'http://ACORD.org/Standards/Life/2')
SELECT t.c.value('CorrelationGUID[1]', 'UNIQUEIDENTIFIER')
FROM @XML.nodes('//TXLifeRequest') t(c)

Output -

------------------------------------
4B30545A-158B-441A-A37A-0B259F757059

#3


1  

You need to declare the namespace. Try this:

您需要声明命名空间。尝试这个:

SELECT
  XMLDATA.value('declare namespace L="http://ACORD.org/Standards/Life/2";(//L:CorrelationGUID)[1]',
                'UNIQUEIDENTIFIER') AS 'TransType'
FROM   @test

UNION ALL

SELECT
  C.value('declare namespace L="http://ACORD.org/Standards/Life/2";(./L:CorrelationGUID)[1]',
          'UNIQUEIDENTIFIER') AS 'TransType'
FROM   @test
CROSS APPLY
  XMLDATA.nodes('declare namespace L="http://ACORD.org/Standards/Life/2";/L:TXLife/L:TXLifeRequest') N(C)

See it working here.

看到它在这里工作。

#1


1  

Well, if I test your code with CREATE TABLE, here and here, the unfiltered select works just fine. However, the second filtered select returns no results.

好吧,如果我用CREATE TABLE测试你的代码,这里和这里,未经过滤的选择工作正常。但是,第二个筛选的选择不返回任何结果。

If I alter the query to to specify the right namespace, like in @Devart's answer,

如果我改变查询以指定正确的命名空间,就像@ Devart的答案一样,

WITH XMLNAMESPACES (DEFAULT 'http://ACORD.org/Standards/Life/2')
SELECT
        C.value('./CorrelationGUID[1]', 'varchar(50)') AS 'TransType'
    FROM
        Test
    CROSS APPLY
        XMLDATA.nodes('/TXLife/TXLifeRequest') n (C)

You can see, it works as expected.

你可以看到,它按预期工作。

#2


1  

Try this one -

试试这个 -

DECLARE @XML XML
SELECT  @XML = '
<TXLife xmlns="http://ACORD.org/Standards/Life/2" xmlns:tx="http://ACORD.org/Standards/Life/2" Version="2.20.00">
    <TXLifeRequest PrimaryObjectID="Holding_1">
        <CorrelationGUID>4b30545a-158b-441a-a37a-0b259f757059</CorrelationGUID>
    </TXLifeRequest>
</TXLife>'

;WITH XMLNAMESPACES (DEFAULT 'http://ACORD.org/Standards/Life/2')
SELECT t.c.value('CorrelationGUID[1]', 'UNIQUEIDENTIFIER')
FROM @XML.nodes('//TXLifeRequest') t(c)

Output -

------------------------------------
4B30545A-158B-441A-A37A-0B259F757059

#3


1  

You need to declare the namespace. Try this:

您需要声明命名空间。尝试这个:

SELECT
  XMLDATA.value('declare namespace L="http://ACORD.org/Standards/Life/2";(//L:CorrelationGUID)[1]',
                'UNIQUEIDENTIFIER') AS 'TransType'
FROM   @test

UNION ALL

SELECT
  C.value('declare namespace L="http://ACORD.org/Standards/Life/2";(./L:CorrelationGUID)[1]',
          'UNIQUEIDENTIFIER') AS 'TransType'
FROM   @test
CROSS APPLY
  XMLDATA.nodes('declare namespace L="http://ACORD.org/Standards/Life/2";/L:TXLife/L:TXLifeRequest') N(C)

See it working here.

看到它在这里工作。