Are line breaks allowed in XML attribute values?

I understand that this is not elegant or desirable, but is it allowed (in well-formed XML) the attribute value in the XML element to span multiple lines?

eg.

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

Yes, I understand that it’s better to write this. I personally would write this as:

 <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 would like to know if the first example is allowed in well-formed XML.

+81
xml xml-parsing
Jan 16 '09 at 5:57
source share
4 answers

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

Everything except < , & , and your delimiter ( ' or " ) seems to be in order. Thus, there should be a new line.

+93
Jan 16 '09 at 6:12
source share

However, according to the W3C recommendation, your XML parser should normalize all whitespace in space (0x20) - so the output of your examples will be different (you should have a new line for the output for "& # 13 & # 10;", but only space in the first case).

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

+45
Nov 18 '11 at 19:51
source share

.NET only: If you are not sure if the target string is a valid xml attribute (and specify this attribute value through code), you can always use the SecurityElement.Escape function to avoid invalid characters.

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

< , > , & , ' , "

And that means (as my predecessors wrote) that the new line should be in order.

+3
Nov 15 '11 at 9:38
source share

Yes, the first example is valid.

+2
Jan 16 '09 at 6:09
source share



All Articles