Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are line breaks in XML attribute values allowed?

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.

like image 752
CodeAndCats Avatar asked Jan 16 '09 05:01

CodeAndCats


People also ask

Are line breaks allowed in XML?

XML does not require a specific form of line break, so you can use whatever is convenient (carriage return, linefeed, or a combination) when creating an XML file. XML parsers will do the right thing largely because they're parsing on tags, not records - so whatever line break you're using is just whitespace to XML.

How do you break a line in XML?

use <br/> ; or.

Can XML attribute values have spaces?

The default value of the xml:space attribute is the literal value "default" . For the value "default" , or if xml:space is not indicated at all, the behavior of significant white-space parsing is the default handling, as defined in the topic White-space processing in XAML.

Is illegal in XML attribute values?

Some special characters are not permitted in XML attribute values. Note that the ampersand (&) and less-than (<) characters are not permitted in XML attribute values.


2 Answers

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.

like image 125
derobert Avatar answered Sep 28 '22 06:09

derobert


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 "&#13;&#10;", but only space in the first case).

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

like image 33
Jan Cetkovsky Avatar answered Sep 28 '22 06:09

Jan Cetkovsky