Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 single quotes( ' ) issue in XML

Author  Topic 

binto
Yak Posting Veteran

59 Posts

Posted - 2012-03-15 : 03:47:33
Hi,
I have a XML given by client side.
<EmailBatch>
<EmailDetail dealerid="51" OpportunityID="74892" ClientName="Martha's Hart" saleid="255049" email="JP@YAHOO.COM" />
<EmailDetail dealerid="51" OpportunityID="98802" ClientName="Timothy's Lowry" saleid="255273" email="karen@hinds.com" />
</EmailBatch>

DECLARE @TEMPXML XML
SET @TEMPXML = '<EmailBatch>
<EmailDetail dealerid="51" OpportunityID="74892" ClientName="Martha's Hart" saleid="255049" email="JP@YAHOO.COM" />
<EmailDetail dealerid="51" OpportunityID="98802" ClientName="Timothy's Lowry" saleid="255273" email="karen@hinds.com" />
</EmailBatch>
'

for selecting data in SQL I am using
SELECT
Batch.client.value('./@dealerid', 'nvarchar(max)') as DID,
Batch.client.value('./@OpportunityID', 'nvarchar(max)') as OpportunityID,
Batch.client.value('./@ClientName', 'nvarchar(max)') as Clientname,
Batch.client.value('./@saleid', 'nvarchar(max)') as SaleID,
Batch.client.value('./@email', 'nvarchar(max)') as Email
FROM @TEMPXML.nodes('/EmailBatch/EmailDetail') Batch(client)

but I am getting error as XML attribute value includes '(ClientName="Martha's Hart").How can I resolve ' issue in XML attribute?
Please help me.



Thanks & Regards
Binto Thomas

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-03-19 : 07:34:42
How are you being given the XML? If it is in a file, you can import the data into a table even if it has single quotes, as long as the XML is well-formed (which it is, for the XML fragment you have shown). Presence of single quote would not be a problem then. For example:
CREATE TABLE #tmp(xmlCol XML);

INSERT INTO #tmp
SELECT *
FROM OPENROWSET(
BULK 'c:\YourXmlFile.txt',
SINGLE_BLOB) AS x;

SELECT
Batch.client.value('./@dealerid', 'nvarchar(max)') as DID,
Batch.client.value('./@OpportunityID', 'nvarchar(max)') as OpportunityID,
Batch.client.value('./@ClientName', 'nvarchar(max)') as Clientname,
Batch.client.value('./@saleid', 'nvarchar(max)') as SaleID,
Batch.client.value('./@email', 'nvarchar(max)') as Email
FROM #tmp cross apply xmlCol.nodes('/EmailBatch/EmailDetail') Batch(client)
If you are getting the data from the client and pasting it to somewhere, then you can escape the single quotes by inserting another single quote:
DECLARE @TEMPXML XML
SET @TEMPXML = '<EmailBatch>
<EmailDetail dealerid="51" OpportunityID="74892" ClientName="Martha''s Hart" saleid="255049" email="JP@YAHOO.COM" />
<EmailDetail dealerid="51" OpportunityID="98802" ClientName="Timothy''s Lowry" saleid="255273" email="karen@hinds.com" />
</EmailBatch>
'

SELECT
Batch.client.value('./@dealerid', 'nvarchar(max)') as DID,
Batch.client.value('./@OpportunityID', 'nvarchar(max)') as OpportunityID,
Batch.client.value('./@ClientName', 'nvarchar(max)') as Clientname,
Batch.client.value('./@saleid', 'nvarchar(max)') as SaleID,
Batch.client.value('./@email', 'nvarchar(max)') as Email
FROM @TEMPXML.nodes('/EmailBatch/EmailDetail') Batch(client)
Go to Top of Page
   

- Advertisement -