Are line breaks in XML attribute values allowed?

XmlXml Parsing

Xml Problem Overview


I realise that it's not elegant or desired, but is it allowed (in well-formed XML) for an attribute value in an XML element to span multiple lines?

e.g.

<some-xml-element value="this value goes over....
multiple lines!" />

Yeah I realise there's better ways of writing that. I would personally write it like:

<some-xml-element>
<value>this value goes over...
multiple lines!</value>
</some-xml-element>

or:

<some-xml-element value="this value goes over....&#13;&#10;" />

But we have our own XML parser and I'd like to know whether the first example is allowed in well-formed XML.

Xml Solutions


Solution 1 - Xml

http://www.w3.org/TR/REC-xml/#NT-AttValue

Seems to say everything except <, &, and your delimiter (' or ") are OK. So newline should be, too.

Solution 2 - Xml

It is allowed, however according to W3C recommendation your XML parser should normalize the all whitespace characters to space (0x20) - so the output of your examples will differ (you should have new line on the output for " ", but only space in the first case).

http://www.w3.org/TR/1998/REC-xml-19980210#AVNormalize

Solution 3 - Xml

.NET only: If you're not sure if target string is valid xml attribute (and provide this attribute's value via code), you can always use SecurityElement.Escape function to escape invalid characters.

According to the description of this function, the only invalid characters are:

<, >, &, ', "

And this means (as my predecessors wrote), that new line should be OK.

Solution 4 - Xml

Yes the first example is a valid one.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCodeAndCatsView Question on Stackoverflow
Solution 1 - XmlderobertView Answer on Stackoverflow
Solution 2 - XmlJan CetkovskyView Answer on Stackoverflow
Solution 3 - XmlŁukasz WiatrakView Answer on Stackoverflow
Solution 4 - XmlRejiView Answer on Stackoverflow