how to parse xml with special character in sql ser

2019-04-04 11:16发布

问题:

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)

回答1:

Invalid special characters & its substitute in xml

  1. & - &amp;
  2. < - &lt;
  3. > - &gt;
  4. " - &quot;
  5. ' - &#39;


回答2:

< 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
-------------------- --------------------
 < > & ' "            < > & ' "


回答3:

You need to ensure the XML is valid, so you need to make sure any special characters are encoded.

e.g.

DECLARE @MyXML XML
SET @MyXML = '<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>'