Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to parse xml with special character in sql server

I am getting following error when i add < in my xml,

Msg 9455, Level 16, State 1, Line 6 XML parsing: line 4, character 14, illegal qualified name character

How can i parse xml with these type of special characters?

DECLARE @MyXML XML
SET @MyXML = '<SampleXML>
<Colors>
<Color1>W < hite</Color1>
<Color2>Blue</Color2>
<Color3>Black</Color3>
<Color4 Special="Light">Green</Color4>
<Color5>Red</Color5>
</Colors>
<Fruits>
<Fruits1>Apple</Fruits1>
<Fruits2>Pineapple</Fruits2>
<Fruits3>Grapes</Fruits3>
<Fruits4>Melon</Fruits4>
</Fruits>
</SampleXML>'

SELECT
a.b.value('Colors[1]/Color1[1]','varchar(10)') AS Color1,
a.b.value('Colors[1]/Color2[1]','varchar(10)') AS Color2,
a.b.value('Colors[1]/Color3[1]','varchar(10)') AS Color3,
a.b.value('Colors[1]/Color4[1]/@Special','varchar(10)')+' '+
+a.b.value('Colors[1]/Color4[1]','varchar(10)') AS Color4,
a.b.value('Colors[1]/Color5[1]','varchar(10)') AS Color5,
a.b.value('Fruits[1]/Fruits1[1]','varchar(10)') AS Fruits1,
a.b.value('Fruits[1]/Fruits2[1]','varchar(10)') AS Fruits2,
a.b.value('Fruits[1]/Fruits3[1]','varchar(10)') AS Fruits3,
a.b.value('Fruits[1]/Fruits4[1]','varchar(10)') AS Fruits4
FROM @MyXML.nodes('SampleXML') a(b)
like image 512
Haider Ali Wajihi Avatar asked Mar 29 '12 07:03

Haider Ali Wajihi


People also ask

Are special characters allowed in XML?

When you use wizards to customize any string in your XML file, you can use the following special symbols: <, >, &, ', ". You can also use these symbols when you are editing a query in Expert Mode or when you are manually entering SQL code into XML files between CDATA tags.

Can SQL parse XML?

SQL Server provides an XML option to use with the FOR clause, allowing for an easy method of converting table data into XML nodes.

Is XML a data type in SQL?

The xml data type is a built-in data type in SQL Server and is somewhat similar to other built-in types such as int and varchar. As with other built-in types, you can use the xml data type as a column type when you create a table as a variable type, a parameter type, a function-return type, or in CAST and CONVERT.


2 Answers

Invalid special characters & its substitute in xml

  1. & - &amp;
  2. < - &lt;
  3. > - &gt;
  4. " - &quot;
  5. ' - &#39;
like image 97
Haider Ali Wajihi Avatar answered Oct 08 '22 19:10

Haider Ali Wajihi


< needs to be specified as &lt; in the XML

<SampleXML>
<Colors>
<Color1>W &lt; hite</Color1>
<Color2>Blue</Color2>
<Color3>Black</Color3>
<Color4 Special="Light">Green</Color4>
<Color5>Red</Color5>
</Colors>
<Fruits>
<Fruits1>Apple</Fruits1>
<Fruits2>Pineapple</Fruits2>
<Fruits3>Grapes</Fruits3>
<Fruits4>Melon</Fruits4>
</Fruits>
</SampleXML>

Update:

The characters you need to escape in node values are < => &lt; and & => &amp;.
In attribute values you also need to escape " => &quot; if you use " around your attribute values.

This is a valid XML:

<root>
  <item> &lt; > &amp; ' "</item>
  <item att=" &lt; > &amp; ' &quot;" />
</root>

Try it in a query:

declare @xml xml =
'
<root>
  <item> &lt; > &amp; '' "</item>
  <item att=" &lt; > &amp; '' &quot;" />
</root>
'

select @xml.value('(root/item)[1]', 'varchar(20)') as NodeValue,
       @xml.value('(root/item/@att)[1]', 'varchar(20)') as AttValue

Result:

NodeValue            AttValue
-------------------- --------------------
 < > & ' "            < > & ' "
like image 38
Mikael Eriksson Avatar answered Oct 08 '22 20:10

Mikael Eriksson