2016-11-02 2 views
1

I-Code haben wie unten:SQL Server: Wählen Sie aus XML mit Namespace

declare @strXML nvarchar(max) = 
'<?xml version="1.0" encoding="utf-16"?> 
    <GetDictResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <GetDictResult> 
    <Success xmlns="http://or.org/ValidationSchema.xsd">true</Success> 
    <Dicts xmlns="http://or.org/ValidationSchema.xsd"> 
     <Dict> 
     <Code>TADR</Code> 
     <Name>Type of address</Name> 
     </Dict> 
    </Dicts> 
    </GetDictResult> 
</GetDictResponse> 
' 

declare @xml xml = CONVERT(XML,CONVERT(NVARCHAR(MAX),@strXML)) 

--empty row result 
SELECT 
    ltrim(rtrim(T.c.value('Code[1]','VARCHAR(max)'))) Code, 
    ltrim(rtrim(T.c.value('Name[1]','VARCHAR(max)'))) Name 
FROM @xml.nodes('*/*/*/*') AS T(C) 
--any rows 
SELECT 
    ltrim(rtrim(T.c.value('Code[1]','VARCHAR(max)'))) Code, 
    ltrim(rtrim(T.c.value('Name[1]','VARCHAR(max)'))) Name 
FROM @xml.nodes('GetDictResponse/GetDictResult/Dicts/Dict') AS T(C) 

Wie kann ich Code und Name Werte?

Grüße, R.B.

Antwort

0

Versuchen Sie folgendes:

DECLARE @strXML XML = 
'<GetDictResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <GetDictResult> 
     <Success xmlns="http://or.org/ValidationSchema.xsd">true</Success> 
     <Dicts xmlns="http://or.org/ValidationSchema.xsd"> 
     <Dict> 
     <Code>TADR</Code> 
     <Name>Type of address</Name> 
     </Dict> 
    </Dicts> 
    </GetDictResult> 
</GetDictResponse>' 

;WITH XMLNAMESPACES('http://or.org/ValidationSchema.xsd' as ns) 
    SELECT 
     Code = c.value('ns:Code[1]', 'NVARCHAR(20)'), 
     Name = c.value('ns:Name[1]', 'NVARCHAR(20)') 
    FROM 
     @strXml.nodes('/GetDictResponse/GetDictResult/ns:Dicts/ns:Dict') AS T(C) 

Ein Problem ist die XML-Header Sie haben - können Sie davon loswerden? Ohne diesen Code werden die gewünschten Werte zurückgegeben:

enter image description here