45

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

Ray Booysen
  • 28,894
  • 13
  • 84
  • 111
Alpants
  • 766
  • 2
  • 8
  • 19
  • 3
    Can you define "screwing up"? What code are you using to create the XML. – AnthonyWJones Feb 11 '09 at 22:33
  • 1
    Ok so it turns out it wasn't the / after all it was the "&" in another attribute on the same line. That explains why there's no escape sequence for it! – Alpants Feb 11 '09 at 22:34
  • In retrospect this question was pretty stupid, if anyone has the rep to remove it or close it.. they should! – Alpants Feb 11 '09 at 22:36
  • 16
    +1 @Alpants Keep it open, google brought me here first result, and the answers are helpful – Drenai Jul 18 '11 at 15:20
  • I hit a case where an unescaped forward slash caused a seeming issue in XSLT: This tried to run an XPath expression which, of course, failed. I realized that I simply needed to wrap it in single quotes for it to work: I comment this only because my (dumb) issue led me to this page before I figured it out. – danjuggler Dec 12 '16 at 21:31
  • I too assumed / would mess up my XML, so the question was useful – Greg Woods Oct 26 '18 at 10:08

7 Answers7

62

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

Ray Booysen
  • 28,894
  • 13
  • 84
  • 111
  • 3
    ‘%’ does not need encoding in XML. ‘>’ can need encoding in one particular niche situation. ‘"’ or ‘'’ will need encoding inside attribute values (whichever was used as the delimiter for that attribute). – bobince Feb 11 '09 at 22:37
  • Nope. No magic, no nothing. Nothing helped me in my case. Only `formatted="false"` solved it for me. – Justin Case Apr 08 '16 at 10:32
  • Some text editors render characters which appear to be fine, but when opening it in a different editor, you can see the the character is actually an illegal character. I had a situation where I copied text including an em-dash (the long hyphen) from MS Word into an XML file in Notepad. Looks fine there but the XML ends up being invalid. Looking at the file in Visual Studio Code, the em-dash was rendered as an invalid character (was diamond with question mark in it). So re-copied the text into Visual Studio Code and re-saved and the file retained the valid em-dash character. Go figure. – Tahari Jul 26 '17 at 00:19
9

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.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
sinisterstuf
  • 195
  • 4
  • 7
  • 2
    The SAX parser was giving me errors about an entity referenced but not declared, using ⁄ so I used the Unicode equivalent ⁄ and it works great. Thanks. – Lambart Sep 12 '14 at 18:46
  • 1
    Indeed @Lambart, I wasn't that specific; you'll need to use the `⁄` entity in XML, which is naturally also valid HTML, but in HTML you have the additional convenience of using the more easily remembered `⁄` HTML entity. – sinisterstuf Sep 25 '14 at 09:02
  • @bobince has suggested / but there is &sol; as well – kurdtpage Jun 30 '15 at 21:40
8

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.

Eamonn Kenny
  • 81
  • 1
  • 1
  • 1
    Thanks. This is the problem I was having. I needed to replace a bunch of invalid xml element characters with valid substitutes. – Adam Bruss Feb 23 '12 at 05:29
7

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"
Deantwo
  • 1,113
  • 11
  • 18
Adam Diament
  • 4,290
  • 3
  • 34
  • 55
6

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.

bobince
  • 528,062
  • 107
  • 651
  • 834
3

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

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
-1

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

I do not know what you mean by XML formatting.

leppie
  • 115,091
  • 17
  • 196
  • 297