如何在XML属性值中转义双引号?

时间:2022-09-15 15:31:16

From the following trials

从以下试验

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

Only the last one works for a XML parser that I'm using here. Is there any other alternative?

我在这里使用的XML解析器只能使用最后一个。还有其他选择吗?

4 个解决方案

#1


148  

You can use &quot;

您可以使用“

#2


42  

From the XML spec:

从XML规范:

To allow attribute values to contain both single and double quotes, the apostrophe or single-quote character (') may be represented as "&apos;", and the double-quote character (") as "&quot;".

为了允许属性值同时包含单引号和双引号,撇号或单引号字符(')可以表示为“&apos”;,双引号字符(")为";"

#3


7  

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

编码器工具箱站点上的字符串转换页面可以方便地编码少量的HTML或XML代码,以便将其作为XML元素的值包含在其中。

#4


5  

A double quote character (") can be escaped as &quot;, but here's the rest of the story...

一个双重引用的角色(")可以被转义成";但下面是故事的其余部分……

Double quote character must be escaped in this context:

  • In XML attributes delimited by double quotes:

    在用双引号分隔的XML属性中:

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

Double quote character need not be escaped in most contexts:

  • In XML textual content:

    在XML文本内容:

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

    在用单引号(')分隔的XML属性中:

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

    Similarly, (') require no escaping if (") are used for the attribute value delimiters:

    类似地,如果属性值分隔符使用('),则不需要转义:

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

See also

#1


148  

You can use &quot;

您可以使用“

#2


42  

From the XML spec:

从XML规范:

To allow attribute values to contain both single and double quotes, the apostrophe or single-quote character (') may be represented as "&apos;", and the double-quote character (") as "&quot;".

为了允许属性值同时包含单引号和双引号,撇号或单引号字符(')可以表示为“&apos”;,双引号字符(")为";"

#3


7  

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

编码器工具箱站点上的字符串转换页面可以方便地编码少量的HTML或XML代码,以便将其作为XML元素的值包含在其中。

#4


5  

A double quote character (") can be escaped as &quot;, but here's the rest of the story...

一个双重引用的角色(")可以被转义成";但下面是故事的其余部分……

Double quote character must be escaped in this context:

  • In XML attributes delimited by double quotes:

    在用双引号分隔的XML属性中:

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

Double quote character need not be escaped in most contexts:

  • In XML textual content:

    在XML文本内容:

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

    在用单引号(')分隔的XML属性中:

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

    Similarly, (') require no escaping if (") are used for the attribute value delimiters:

    类似地,如果属性值分隔符使用('),则不需要转义:

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

See also