“/”的 XML 实体?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/539143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:22:00  来源:igfitidea点击:

XML Entity for "/"?

xmlentityescapingsequence

提问by Alpants

So I'm writing some XML generating code, and found that the following attribute value was screwing up the XML formatting:

所以我正在编写一些 XML 生成代码,发现以下属性值搞砸了 XML 格式:

"Jim/Bob"

So I looked into the XML Entities used as escape sequences and every list I saw did not include one for the forward slash. Am I missing something obvious here? Seems like the sort of thing you'd want to escape...

所以我查看了用作转义序列的 XML 实体,我看到的每个列表都没有包含一个用于正斜杠的列表。我在这里遗漏了一些明显的东西吗?似乎是你想要逃避的那种事情......

回答by Ray Booysen

The forward slash is valid as is and does not need further encoding.

正斜杠按原样有效,不需要进一步编码。

The only reserved characters are:

唯一的保留字符是:

>
<
&
%

For even more XML entities - http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references

对于更多 XML 实体 - http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references

回答by sinisterstuf

I know it turned out this wasn't the problem, but I thought it would be helpful to mention that, in addition to bobince's answer, the Fraction SlashHTML entity &frasl;looks just like a forward slash. Just in case anybody reaching this page actually doeswant an HTML entity for something representing a forward slash.

我知道事实证明这不是问题,但我认为提及除了 bobince 的答案之外,Fraction SlashHTML 实体&frasl;看起来就像一个正斜杠会有所帮助。以防万一到达此页面的任何人实际上确实需要一个 HTML 实体来表示正斜杠。

回答by Eamonn Kenny

I don't think the comments in this thread are entirely correct since if you use a schema (XSD) you could define elements with names Jim, Bob and Jim/Bob without any issues. But then when you want to define the element and entity:

我不认为该线程中的评论是完全正确的,因为如果您使用模式 (XSD),您可以毫无问题地定义名称为 Jim、Bob 和 Jim/Bob 的元素。但是当你想定义元素和实体时:

<names>
  <Jim>Carrey</Jim>
  <Bob>Jones</Bob>
  <Jim/Bob>Six figured Hillbilly</Jim/Bob>
</names>

The problems are obvious.

问题是显而易见的。

回答by bobince

There's no predefined entity reference for it, but you can use a character reference: “&#47;”.

它没有预定义的实体引用,但您可以使用字符引用:“/”。

However, you don't need to escape / for inclusion in XML. You might have to include it for inclusion in something else, for example a URI path part. But then you'd have to escape it for that format first; the application that picks up the URI wouldn't have any way to know if you'd encoded it in the XML or not.

但是,您不需要转义 / 以包含在 XML 中。您可能必须将其包含在其他内容中,例如 URI 路径部分。但是,您必须首先针对该格式对其进行转义;获取 URI 的应用程序无法知道您是否已将其编码到 XML 中。

回答by Adam Diament

FYI although a slash is valid XML, watch out how it's being used downstream. We used it for connecting to an azure service bus queue. Azure itself generated keys with slashes in the connection strings, but then broke silently when we tried to use them. Escaping them with “&#47;”made it work

仅供参考,尽管斜杠是有效的 XML,但请注意它在下游的使用方式。我们用它来连接到 Azure 服务总线队列。Azure 本身在连接字符串中生成带有斜杠的密钥,但是当我们尝试使用它们时,它会无声地中断。逃脱他们“&#47;”使它工作

"Jim&#47;Bob"

回答by AnthonyWJones

I can't see why a value of "Jim/Bob" would need escaping or cause XML any problems at all.

我不明白为什么“Jim/Bob”的值需要转义或导致 XML 出现任何问题。

回答by leppie

You probably have a constrained attribute, as defined in the XML Schema.

您可能有一个受约束的属性,如 XML 模式中所定义。

I do not know what you mean by XML formatting.

我不知道您所说的 XML 格式是什么意思。