简体   繁体   English

T-SQL选择xml值

[英]T-sql select xml value

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  
<soap:Header>   
    <Teste xmlns="http://google.com">    
        <Username></Username>    
        <Password></Password>    
        <Firma>Blabla</Firma>   
    </Teste>  
</soap:Header>  
<soap:Body>   
    -- Omitted
</soap:Body>
</soap:Envelope>

Having this xml in a column, how to retrieve the Firma value ? 在列中包含此xml,如何获取Firma值? What's giving me more trouble is the "soap" namespaces that seem to be ruining my query's 给我带来更多麻烦的是似乎是在破坏我查询的“肥皂”名称空间

This is what I was experimenting with, but not working as expected since the path is wrong 这是我正在尝试的方法,但由于路径错误而无法按预期工作

select ref.col.value('.','varchar(255)')
from my_View tbl
cross apply tbl.XMLColumn.nodes('Envelope/Header/Teste/Firma') as ref(col)

You must use namespaces in your Xquery 您必须在Xquery中使用名称空间

   create table so_Test (XMLColumn xml)
    go
    insert so_Test
    select '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  
    <soap:Header>   
        <Teste xmlns="http://google.com">    
            <Username></Username>    
            <Password></Password>    
            <Firma>Blabla</Firma>   
        </Teste>  
    </soap:Header>  
    <soap:Body>       
    </soap:Body>
    </soap:Envelope>'

    select XMLColumn.value('
    declare namespace soap="http://schemas.xmlsoap.org/soap/envelope/";
    declare namespace google="http://google.com";
    /soap:Envelope[1]/soap:Header[1]/google:Teste[1]/google:Firma[1]', 'varchar(255)') as Firma from  so_test

Result: 结果:

Firma 丰资本


Blabla BLABLA

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

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