2

I have the below code which errors when I run it because it has the "&" sign and can not convert it.

the result should display "testing &". however if I change the xml bit to "testing &" it works. I need a way to replace it so that it does not error.

Declare @Request XML = null
If @Request IS NULL 
BEGIN
    SET @Request = '
                    <Request>  
                       <ProductRequest>
                    <ProductName>testing &</ProductName>
                      </ProductRequest>
                     </Request>'
END 

select @Request.value ('(//ProductName)[1]','nvarchar(100)') 
0

3 Answers 3

2

You are probably looking for this:

Declare @Request XML = null
If @Request IS NULL 
BEGIN
    SET @Request = (SELECT 'testing &' AS ProductName FOR XML PATH('ProductRequest'),ROOT('Request'));
END 

select @Request.value ('(//ProductName)[1]','nvarchar(100)') 

Some background:

XML is more than just some text with extra characters. XML should never be generated just by typing (as in your case) or by string concatenation (often seen). Use the proper method to generate your XML and all encoding issues are solved for you implicitly.

Look at the XML generated and you will find, that the & is found as &amp;. While reading this with value() the re-encoding is done for you - again implicitly.

You should not start to do own REPLACE approaches. Next day someone enters a <or > or another not supported character and you have the same troubles again.

Sign up to request clarification or add additional context in comments.

Comments

1

The & is a reserved/special character in XML. It should be &amp ; and remove space between &amp and ;

as the next:

Declare @Request XML = null
If @Request IS NULL 
BEGIN
    SET @Request = '
                    <Request>  
                       <ProductRequest>
                    <ProductName>testing &amp;</ProductName>
                      </ProductRequest>
                     </Request>'
END 

select @Request.value ('(//ProductName)[1]','nvarchar(100)') 

2 Comments

I need something to replace "&" with "&amp;" automatically, as the user will only enter & in the productname area
I'd strongly advise not even to think about entity references. This is something done for you implicitly, if you just use SELECT ... FOR XML ... When you deal with XML one should avoid to think in string...
0

You need to specify the entity reference &amp; in the XML string for the ampersand:

DECLARE @Request XML = NULL;
IF @Request IS NULL 
BEGIN
    SET @Request = '
                    <Request>  
                       <ProductRequest>
                    <ProductName>testing &amp;</ProductName>
                      </ProductRequest>
                     </Request>';
END; 

SELECT @Request.value ('(//ProductName)[1]','nvarchar(100)');

See https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references

2 Comments

I'd strongly advise not even to think about entity references. This is something done for you implicitly, if you just use SELECT ... FOR XML ... When you deal with XML one should avoid to think in string...
@Shnugo, agreed entity references are transparent when XML objects and methods are used, but that is not the case here since the string was created manually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.