Sunday, February 19, 2012

CDATA In .value() Method Result

Hi All,
I'm trying to select data out from an xml type. One of my elements is cdata
wrapped and when I select it I'm getting the resulting cdata wrapper rather
than the data by itself. I've included my sql and the results I see below.
Any help is appreciated.
Thanks
DECLARE @.xml xml
SELECT @.xml =
'<Udpates><Udpate><ID>123</ID><Name>![CDATA[somename]]</Name></Udpate><Udpate><ID>456</ID><Name>![CDATA[someothername]]</Name></Udpate></Udpates>'
Declare @.XMLTable table (ID int, Name varchar (150))
INSERT INTO @.XMLTable (ID, Name)
SELECT T.c.query('ID').value('.','int')
, T.c.query('Name').value('.','varchar(150)')
FROM @.xml.nodes('/Udpates/Udpate') T(c)
SELECT * FROM @.XMLTable
Results:
123 ![CDATA[somename]]
456 ![CDATA[someothername]]
First off that's not a CDATA wrapper. Try using <![CDATA[ ... ]]> instead.
"JA" <buosc@.hotmail.com> wrote in message
news:uLkq3YlJIHA.3940@.TK2MSFTNGP05.phx.gbl...
> Hi All,
> I'm trying to select data out from an xml type. One of my elements is
> cdata wrapped and when I select it I'm getting the resulting cdata wrapper
> rather than the data by itself. I've included my sql and the results I
> see below. Any help is appreciated.
> Thanks
>
> DECLARE @.xml xml
> SELECT @.xml =
> '<Udpates><Udpate><ID>123</ID><Name>![CDATA[somename]]</Name></Udpate><Udpate><ID>456</ID><Name>![CDATA[someothername]]</Name></Udpate></Udpates>'
> Declare @.XMLTable table (ID int, Name varchar (150))
> INSERT INTO @.XMLTable (ID, Name)
> SELECT T.c.query('ID').value('.','int')
> , T.c.query('Name').value('.','varchar(150)')
> FROM @.xml.nodes('/Udpates/Udpate') T(c)
> SELECT * FROM @.XMLTable
>
> Results:
> 123 ![CDATA[somename]]
> 456 ![CDATA[someothername]]
>

No comments:

Post a Comment