xml .узел как получить значение - PullRequest
0 голосов
/ 10 апреля 2020

Я знаю (как правило), как экспортировать данные из XML, используя SQL Сервер, и я сделал это для остальной части извлечения, я просто не могу понять, как получить значения для выравнивания, когда перекрестное применение материала внутри тегов XML.

Это работает и возвращает XML имя тега

  DECLARE @XML XML 
    SET @XML= '
                    <Export>
                        <CustomInformation name="Customer ID">12345</CustomInformation>
                        <CustomInformation name="Prepaid">0.00</CustomInformation>
                        <CustomInformation name="New Amount">0.00</CustomInformation>
                    </Export>
            ' 

    select 

        Description = CustomInformation.value('@name','nvarchar(max)')


    from
    @XML.nodes('/Export/CustomInformation') as b(CustomInformation)

Возвращает ноль

 DECLARE @XML XML 
   SET @XML= '
                    <Export>
                        <CustomInformation name="Customer ID">12345</CustomInformation>
                        <CustomInformation name="Prepaid">0.00</CustomInformation>
                        <CustomInformation name="New Amount">0.00</CustomInformation>
                    </Export>
            ' 

    select 

        Description = CustomInformation.value('@name','nvarchar(max)')
        ,tire_wheel = col2.value('@Prepaid', 'money')

    from
    @XML.nodes('/Export/CustomInformation') as b(CustomInformation)
    cross apply b.CustomInformation.nodes('CustomInformation') as c(col2)

Как сделать Я получаю данные в соответствии с именем тега?

1 Ответ

1 голос
/ 10 апреля 2020
DECLARE @XML XML = N'
<Export>
    <CustomInformation name="Customer ID">12345</CustomInformation>
    <CustomInformation name="Prepaid">120.00</CustomInformation>
    <CustomInformation name="New Amount">10.00</CustomInformation>
</Export>
<Export>
    <CustomInformation name="Customer ID">789</CustomInformation>
    <CustomInformation name="Prepaid">160.00</CustomInformation>
    <CustomInformation name="New Amount">30.00</CustomInformation>
</Export>                    
';

--1
select 
    Description = CustomInformation.value('@name','nvarchar(max)'),
    Value = CustomInformation.value('.', 'nvarchar(max)')
from @XML.nodes('./Export/CustomInformation') as c(CustomInformation);

--2
select 
    NodeID = dense_rank() over(order by b.Export), --use this to cross tab/pivot
    Description = CustomInformation.value('@name','nvarchar(max)'),
    Value = CustomInformation.value('.', 'nvarchar(max)')
from  @XML.nodes('./Export') as b(Export)
cross apply b.Export.nodes('./CustomInformation') as c(CustomInformation);

--3
select *
from
(
select 
    NodeID = dense_rank() over(order by b.Export), --use this to cross tab/pivot
    Description = CustomInformation.value('@name','nvarchar(max)'),
    Value = CustomInformation.value('.', 'nvarchar(max)')
from  @XML.nodes('./Export') as b(Export)
cross apply b.Export.nodes('./CustomInformation') as c(CustomInformation)
) as src
pivot
(
    max(value) for Description in ([Customer Id], [Prepaid], [New Amount])
) as pv;

--??
select 
    Description = CustomInformation.value('@name','nvarchar(max)'),
    Value = CustomInformation.value('.', 'nvarchar(max)'),
    [Prepaid value ?] = p.Prepaid.value('.[1]', 'nvarchar(max)')
from @XML.nodes('./Export/CustomInformation') as c(CustomInformation)
--up one level in the hierarchy...not performant
outer apply c.CustomInformation.nodes('../CustomInformation[@name="Prepaid"]') as p(Prepaid);

--??
select 
    Description = CustomInformation.value('@name','nvarchar(max)'),
    Value = CustomInformation.value('.', 'nvarchar(max)'),
    [Prepaid value ?] = p.Prepaid.value('.[1]', 'nvarchar(max)')
from  @XML.nodes('./Export') as b(Export)
cross apply b.Export.nodes('./CustomInformation') as c(CustomInformation)
outer apply b.Export.nodes('./CustomInformation[@name="Prepaid"]') as p(Prepaid); 
...