简体   繁体   English

如何应用XML命名空间

[英]How to apply XML Namespaces

I am trying to parse some xml from a SOAP API. 我正在尝试从SOAP API解析一些xml。 I am quite new to XML, but i can get it to work when i only have the body of the statement. 我对XML还是很陌生,但是当我只有该语句的主体时,我可以使它工作。 I have tried applying some simple namespaces, but I am not sure whether this is the right path to pursue. 我曾尝试应用一些简单的名称空间,但不确定这是否是正确的方法。

DECLARE @XmlFile XML = 
'<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <Account_GetDataArrayResponse xmlns="http://e-conomic.com">
            <Account_GetDataArrayResult>
                <AccountData>
                    <Handle>
                        <Number>1000</Number>
                    </Handle>
                    <Number>1000</Number>
                    <Name>RESULTATOPGØRELSE</Name>
                    <Type>Heading</Type>
                    <DebitCredit>Debit</DebitCredit>
                    <IsAccessible>true</IsAccessible>
                    <BlockDirectEntries>false</BlockDirectEntries>
                    <Balance>0.00</Balance>
                </AccountData>
                <AccountData>
                    <Handle>
                        <Number>1001</Number>
                    </Handle>
                    <Number>1001</Number>
                    <Name>Omsætning</Name>
                    <Type>Heading</Type>
                    <DebitCredit>Debit</DebitCredit>
                    <IsAccessible>true</IsAccessible>
                    <BlockDirectEntries>false</BlockDirectEntries>
                    <Balance>0.00</Balance>
                </AccountData>
            </Account_GetDataArrayResult>
        </Account_GetDataArrayResponse>
    </soap:Body>
</soap:Envelope>'

;WITH XMLNAMESPACES( 'http://www.w3.org/2001/XMLSchema' AS e) 


SELECT
    AccountHandleNumber = AccountData.value('(Handle/Number)[1]', 'int')
    ,AccountName = AccountData.value('(Name)[1]', 'varchar(20)')
    ,AccountNumber = AccountData.value('(Number)[1]', 'int')
    ,AccountType = AccountData.value('(Type)[1]','varchar(20)')
    ,AccountDebitCredit = AccountData.value('(DebitCredit)[1]', 'varchar(20)')
    ,AccountIsAccessible = AccountData.value('(IsAccessible)[1]', 'varchar(20)')
    ,AccountBlockDirectEntries = AccountData.value('(BlockDirectEntries)[1]', 'varchar(20)')
    ,AccountBalance = AccountData.value('(Balance)[1]', 'float')
FROM  @XMLFile.nodes('/Account_GetDataArrayResponse/Account_GetDataArrayResult/AccountData') AS XTbl(AccountData)

Yes, this is very much the right way! 是的,这是非常正确的方法!

However, you're not reading the entire XML you have - you have to really start at the root node - so try to adapt your code like this: 但是,您并没有阅读到您拥有的整个XML-您必须真正从根节点开始-因此请尝试像这样修改代码:

;WITH XMLNAMESPACES('http://schemas.xmlsoap.org/soap/envelope/' AS e, 
                    DEFAULT 'http://e-conomic.com') 
SELECT
    AccountHandleNumber = AccountData.value('(Handle/Number)[1]', 'int')
    ..... 
FROM  
    @XMLFile.nodes('/e:Envelope/e:Body/Account_GetDataArrayResponse/Account_GetDataArrayResult/AccountData') AS XTbl(AccountData)

First of all - you need to also take care of the <soap:Envelope> and <soap:Body> tags at the very beginning of the document - you've created a XML namespace alias of e for that - so you need to include /e:Envelope/e:Body at the beginning of your XPath expression in the .nodes() call. 首先-您还需要在文档的开始部分照顾<soap:Envelope><soap:Body>标记-您已经为此创建了XML命名空间别名e因此您需要包括/e:Envelope/e:Body.nodes()调用中XPath表达式的.nodes()

Also: the <Account_GetDataArrayResponse> node declares another XML namespace which gets applied throughout the rest of the XML document - I've added that as the DEFAULT XML namespace to the query, so it will be applied to any node in the XPath expression that doesn't have an explicit XML namespace prefix associated with it 另外: <Account_GetDataArrayResponse>节点声明了另一个XML名称空间,该名称空间将在整个XML文档的其余部分中应用-我已将其作为DEFAULT XML名称空间添加到查询中,因此它将被应用于XPath表达式中不包含该名称空间的任何节点。没有与之关联的显式XML名称空间前缀

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

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