XML Special Characters

Posted: September 12, 2010 in SQL Server 2005 - XML

If you use XML with no DTD (Document Type Definitions), the following five characters are assumed to be predeclared, and you can use them without declaring them as follows:

&lt; The less-than character (<)
&amp; The ampersand character (&)
&gt; The greater-than character (>)
&quot; The double-quote character (")
&apos; The apostrophe or single-quote character (')

You can get these characters in the XML file as follows:

-- Declare variables
DECLARE @lxml_value XML;

-- Set XML values
SET @lxml_value = '<Detail>
                      <Name>Testing XML &amp; Special Characters</Name>
                      <Address>Test XML&apos;s address</Address>
                      <Quote>&quot;Quote&quot;</Quote>
                      <Compare1>10 &lt; 20</Compare1>
                      <Compare2>10 &gt; 5</Compare2>
                   </Detail>'

-- Select vales from XML
SELECT @lxml_value.value('(/Detail/Name)     [1]', 'VARCHAR(100)');
SELECT @lxml_value.value('(/Detail/Address)  [1]', 'VARCHAR(100)');
SELECT @lxml_value.value('(/Detail/Quote)    [1]', 'VARCHAR(100)');
SELECT @lxml_value.value('(/Detail/Compare1) [1]', 'VARCHAR(100)');
SELECT @lxml_value.value('(/Detail/Compare2) [1]', 'VARCHAR(100)');
Advertisements
Comments
  1. […] więcej: XML Special Characters « Chronicle's of a Geek… Tags: and-you, are-assumed, definitions, document-type, dtd, five-characters, following, […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s