使用sql查询从数据库中提取xml元素

时间:2014-04-10 22:35:33

标签: sql-server xml sql-server-2008 tsql sql-server-2012

您好我在数据库表列中有以下xml结构:

DECLARE @Response XML =

'<star:ShowInfo xmlns="http://www.starstandard.org/STAR/5" 
xmlns:ns2="http://www.openapplications.org/oagis/9" 
xmlns:star="http://www.starstandard.org/STAR/5" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" releaseID="5.1.5" 
xsi:noNamespaceSchemaLocation="">

<ShowDataArea>
    <ServiceInfo>
        <SVPlanInfo>
            <AKStatus>
                <Code>Error</Code>
                <STText xsi:type="ns2:TextType">E12143 - Please fetch me from this xml     </STText>
            </AKStatus>
        </SVPlanInfo>
    </ServiceInfo>
</ShowDataArea></star:ShowInfo>'

在上面的xml中,我需要获取 STText E12143 - 请从此xml 中获取。谁能指出我怎么做呢?

我尝试了以下但它似乎无法工作:

;WITH XMLNAMESPACES ('http://www.w3.org/2001/XMLSchema' as xsd,
            'http://www.w3.org/2001/XMLSchema-instance' as xsi)
SELECT @Response.value('(/xsd:Response)[1]','nvarchar(500)') as ExceptionMessage

1 个答案:

答案 0 :(得分:0)

多么痛苦。

删除:

xmlns="http://www.starstandard.org/STAR/5"

这不是一个sql问题,而是一个名称空间混乱的问题。

有时,通过测试像

这样的地方,它可以解决这个问题

http://xpath.online-toolz.com/tools/xpath-editor.php