SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Nested XML/CDATA ...


Nested XML/CDATA ...

Author
Message
Sim-473257
Sim-473257
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 126
I've been kindly given some XML, and asked to return relevant values from within.
Now I have only done simple xml before, and this one has me beat.

It seams to have a second xml document nested within one of the nodes wrapped by a CDATA tag.
I have a sneaking feeling that whoever write the application that produces this xml, should have provided us with the facility to read it ! .. but regardless, this is the situation as of today.

Could anyone point me in the right direction to being able to decipher this and strip/display the node values?

Many Thanks


<?xml version="1.0" encoding="utf-8"?>
<DOWNLOADED-SCRIPT SCRIPTID="blah" OBJECTTYPE="IS">
<SCRIPT-MAIN>
<![CDATA[
<?xml version="1.0" encoding="utf-8"?>
<Script ScriptId="blah" StartNode="600001" LastModifiedBy="xxxxxx" BeginDate=.....<truncated for post>
<Node NodeId="600001" TemplateId="Dialogue" X="595" Y="1215" Column="-1" Row="-1" NodeType="template">
<Resource URI="Package_Type" Name="Survey"/>
<NodeDescription Language="en" Text="Package Type?"/>
<NodeDescription Language="cs" Text=""/>
</Node>
<Node ChapterId="1" NodeId="600002" TemplateId="Dialogue" X="5982" Y="2407" Column="-1" Row="-1" NodeType="template">
<Resource URI="Fault_Type?" Name="Survey"/>
<NodeDescription Language="en" Text="Fault Type?"/>

... etc etc


Arthur Olcot
Arthur Olcot
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3208 Visits: 1779
Hi,
Personally, if I was to approach this from the SQL server side of things, I would do something like the following. It isn't pretty, but it works and I hope that someone could post a better solution here :-D


DECLARE @xml XML
SET @xml = '
<DOWNLOADED-SCRIPT SCRIPTID="blah" OBJECTTYPE="IS">
<SCRIPT-MAIN>
<![CDATA[
<?xml version="1.0" encoding="utf-8"?>
<Script ScriptId="blah" StartNode="600001" LastModifiedBy="xxxxxx" BeginDate="">
<Node NodeId="600001" TemplateId="Dialogue" X="595" Y="1215" Column="-1" Row="-1" NodeType="template">
<Resource URI="Package_Type" Name="Survey"/>
<NodeDescription Language="en" Text="Package Type?"/>
<NodeDescription Language="cs" Text=""/>
</Node>
<Node ChapterId="1" NodeId="600002" TemplateId="Dialogue" X="5982" Y="2407" Column="-1" Row="-1" NodeType="template">
<Resource URI="Fault_Type?" Name="Survey"/>
<NodeDescription Language="en" Text="Fault Type?"/>
</Node>
</Script>
]]>
</SCRIPT-MAIN>
</DOWNLOADED-SCRIPT>'

--Extract xml in cdata section
DECLARE @InnerXmltext VARCHAR(MAX)
SELECT @InnerXmltext = LTRIM(@xml.value('(//SCRIPT-MAIN/text())[1]', 'varchar(max)'))

--Get declaration at start of string
SET @InnerXmltext = SUBSTRING(@InnerXmltext, CHARINDEX('<', @InnerXmltext), LEN(@InnerXmltext))

--cast string to xml
DECLARE @InnerXml XML
SET @InnerXml = CAST(@InnerXmltext AS XML)

SELECT @InnerXml




The idea is that the query first extracts the contents of the cdata section into a varchar(max) variable. Then the code trims of the padding at the start of the @InnerXmltext so that the xml declaration is at the start of the text. This should make the contents of @InnerXmltext valid and able to cast to xml so that you can start to use the xml functions against it.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search