Question

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

"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...

Was it helpful?

Solution

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

OTHER TIPS

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 Slash HTML entity &frasl; looks just like a forward slash. Just in case anybody reaching this page actually does want an HTML entity for something representing a forward slash.

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.

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:

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

The problems are obvious.

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

"Jim&#47;Bob"

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

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

I do not know what you mean by XML formatting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top