"> Only...">

How to avoid double quotes in XML attribute values?

From the following studies

<tag attr="\""> <tag attr="<![CDATA["]]>"> <tag attr='"'> 

Only the latter works for the XML parser that I use here. Is there any other alternative?

+102
xml
Oct 18 '10 at 17:08
source share
4 answers

You can use &quot;

+163
Oct 18 2018-10-18
source share

From XML Specification :

To resolve attribute values ​​in both single and double quotation marks, an apostrophe or single quote character (') can be represented as "&", and a double quote character (") as" "".

+43
Oct 18 '10 at 17:12
source share

The String conversion page on the Coder Toolbox site is convenient for encoding more than a small amount of HTML or XML code to include as a value in an XML element.

+7
Jul 17 '12 at 13:55
source share

The double quote character ( " ) may be escaped as &quot; but here the rest of the story ...

The double quote character must be escaped in this context:

  • In XML attributes, separated by double quotes:

     <EscapeNeeded name="Pete &quot;Maverick&quot; Mitchell"/> 

The double quote character should not be escaped in most contexts:

  • In XML text content:

     <NoEscapeNeeded>He said, "Don't quote me."</NoEscapeNeeded> 
  • In XML attributes, separated by single quotes ( ' ):

     <NoEscapeNeeded name='Pete "Maverick" Mitchell'/> 

    Similarly, ( ' ) does not require escaping if ( " ) are used for separators of attribute values:

     <NoEscapeNeeded name="Pete 'Maverick' Mitchell"/> 

see also

  • Simplified XML Screening
+5
Nov 28 '17 at 15:13
source share



All Articles