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)
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.
SQL Server provides an XML option to use with the FOR clause, allowing for an easy method of converting table data into XML nodes.
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.
Invalid special characters & its substitute in xml
&
<
>
"
'
<
needs to be specified as <
in the XML
<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>
Update:
The characters you need to escape in node values are <
=> <
and &
=> &
.
In attribute values you also need to escape "
=> "
if you use "
around your attribute values.
This is a valid XML:
<root>
<item> < > & ' "</item>
<item att=" < > & ' "" />
</root>
Try it in a query:
declare @xml xml =
'
<root>
<item> < > & '' "</item>
<item att=" < > & '' "" />
</root>
'
select @xml.value('(root/item)[1]', 'varchar(20)') as NodeValue,
@xml.value('(root/item/@att)[1]', 'varchar(20)') as AttValue
Result:
NodeValue AttValue
-------------------- --------------------
< > & ' " < > & ' "
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With