105

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.

LarsH
  • 27,481
  • 8
  • 94
  • 152
CodeAndCats
  • 7,508
  • 9
  • 42
  • 60
  • 1
    The .NET XDocument parser accepts this as expected, but the attribute value is returned with a space, not a line feed as it would be in a text as you second example. (Your question is not specific to .NET, but my sample data is. I don't know if this is part of the general standard or a .NET feature.) – Mark Hurd Mar 02 '11 at 06:40
  • 1
    See also http://stackoverflow.com/q/2004386/55452 – David J. Liszewski Jan 03 '13 at 15:04
  • made an example to a similar question that preserves newlines: http://stackoverflow.com/a/29782321/611007 – n611x007 Apr 22 '15 at 08:10
  • related: https://stackoverflow.com/questions/260436/ - related: https://stackoverflow.com/questions/2004386/ - related: https://stackoverflow.com/questions/1289524/ – n611x007 Apr 22 '15 at 10:59

4 Answers4

110

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.

derobert
  • 49,731
  • 15
  • 94
  • 124
  • 6
    One example when new lines are a good idea inside an attribute is for xsi:schemaLocation attribute in the Spring configuration, which can contain several URLs separated by spaces and thus be much longer than the screen width. – stivlo Jul 13 '11 at 08:43
  • 3
    it is valid however the parser will normalize them to space, [as Jan Cetkovsky says](http://stackoverflow.com/a/8188290/611007). – n611x007 Apr 22 '15 at 08:12
  • Well ... I use multiple lines for long if/when test statements in XSLT documents. – Nullius Mar 17 '16 at 08:00
55

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

LarsH
  • 27,481
  • 8
  • 94
  • 152
Jan Cetkovsky
  • 776
  • 6
  • 4
4

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

Łukasz Wiatrak
  • 2,747
  • 3
  • 22
  • 38
2

Yes the first example is a valid one.

Reji
  • 3,426
  • 2
  • 21
  • 25