简体   繁体   English

TSQL XML值返回所有记录的节点的第一个值

[英]TSQL XML value return first value of node for all records

I would like using tsql get value of ChargeType from xml file. 我想使用tsql从xml文件获取ChargeType的值。 I wrote script but, it always returns value 'Principal' for both xml records. 我写了脚本,但是,它总是为两个xml记录返回值“ Principal”。 I can't understand what is wrong and how to fix problem? 我不明白哪里出了问题以及如何解决问题? Script should return values: 脚本应返回值:

ChargeType
Principal
Taxed

Current result 当前结果

ChargeType
Principal
Principal

Source code 源代码

DECLARE @xml XML = '<ListFinancialEventsResponse xmlns="http://www.test.com">
    <ListFinancialEventsResult>
        <FinancialEvents>
            <ShipmentEventList>
                <ShipmentEvent>
                    <ShipmentItemList>
                        <ShipmentItem>
                            <ItemChargeList>
                                <ChargeComponent>
                                    <ChargeType>Principal</ChargeType>
                                    <ChargeAmount>
                                        <CurrencyAmount>20.4</CurrencyAmount>
                                        <CurrencyCode>RUR</CurrencyCode>
                                    </ChargeAmount>
                                </ChargeComponent>
                                <ChargeComponent>
                                    <ChargeType>Taxed</ChargeType>
                                    <ChargeAmount>
                                        <CurrencyAmount>1.23</CurrencyAmount>
                                        <CurrencyCode>GEL</CurrencyCode>
                                    </ChargeAmount>
                                </ChargeComponent>
                            </ItemChargeList>
                        </ShipmentItem>
                    </ShipmentItemList>
                </ShipmentEvent>
            </ShipmentEventList>
        </FinancialEvents>
    </ListFinancialEventsResult>
</ListFinancialEventsResponse>';


;WITH XMLNAMESPACES('http://www.test.com' as ns)
select 
        lfer.c.value('(//ns:ChargeType)[1]', 'nvarchar(50)') AS ChargeType
from @xml.nodes('//ns:ListFinancialEventsResponse//ns:ListFinancialEventsResult//ns:ShipmentItemList//ns:ShipmentItem//ns:ItemChargeList//ns:ChargeComponent') lfer(c)

Well, either you need to specify the whole list of nodes above <ChargeType> in your XPath, using single dashes (right now, you're leaving out a few) 那么, 要么你需要指定的节点之上的整个列表 <ChargeType>在你的XPath,使用短线(现在,你离开了几个)

@xml.nodes('/ns:ListFinancialEventsResponse/ns:ListFinancialEventsResult/ns:FinancialEvents/ns:ShipmentEventList .......

or then you need to use this XPath to get just the <ChargeComponent> nodes and grab the <ChargeType> from those. 否则,您需要使用此XPath来获取<ChargeComponent>节点并<ChargeType>获取<ChargeType>

Try this T-SQL: 试试这个T-SQL:

;WITH XMLNAMESPACES('http://www.test.com' as ns)
SELECT 
    -- do *NOT* use double dashes here!
    lfer.c.value('(ns:ChargeType)[1]', 'nvarchar(50)') AS ChargeType
FROM
    -- just grab *all* <ChargeComponent> nodes anywhere in the XML
    @xml.nodes('//ns:ChargeComponent') lfer(c)

Your existing code here: 您现有的代码在这里:

lfer.c.value('(//ns:ChargeType)[1]'

means: give me all the <ChargeType> nodes (because of the leading // ) and then take the first of all of those nodes - that's why you're getting the node with the Principal twice 意思是:给我所有 <ChargeType>节点(由于前导// ),然后取所有这些节点中的第一个 -这就是为什么要两次获得带有Principal的节点的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM