How to save newlines in XML attribute?

XmlXsltNewline

Xml Problem Overview


I need to save content that containing newlines in some XML attributes, not text. The method should be picked so that I am able to decode it in XSLT 1.0/ESXLT/XSLT 2.0

What is the best encoding method?

Please suggest/give some ideas.

Xml Solutions


Solution 1 - Xml

In a compliant DOM API there is nothing you need to do. Simply save actual newline characters to the attribute, the API will encode them correctly on its own (see Canonical XML spec, section 5.2).

If you do your own encoding (i.e. replacing \n with 
 before saving the attribute value), the API will encode your input again, resulting in 
 in the XML file.

Bottom line is, the string value is saved verbatim. You get out what you put in, no need to interfere.

However… some implementations are not compliant. For example, they will encode & characters in attribute values, but forget about newline characters or tabs. This puts you in a losing position since you can't simply replace newlines with 
 beforehand.

These implementations will save newline characters unencoded, like this:

<xml attribute="line 1
line 2" />

Upon parsing such a document, literal newlines in attributes are normalized into a single space (again, in accordance to the spec) - and thus they are lost.

Saving (and retaining!) newlines in attributes is impossible in these implementations.

Solution 2 - Xml

You can use the entity &#10; to represent a newline in an XML attribute. &#13; can be used to represent a carriage return. A windows style CRLF could be represented as &#13;&#10;.

This is legal XML syntax. See XML spec for more details.

Solution 3 - Xml

A crude answer can be:

XmlDocument xDoc = new XmlDocument();
xDoc.Load(@"Agenda.xml");
//make stuff with the xml
//make attributes value = "\r\n" (you need both expressions to make a new line)
string a = xDoc.InnerXml.Replace("&#xD;", "\r").Replace("&#xA;", "\n").Replace("><",">\r    \n<");
StreamWriter sDoc = new StreamWriter(@"Agenda.xml");
sDoc.Write(a);
sDoc.Flush();
sDoc.Dispose();

This will as you see is just a string

Solution 4 - Xml

A slightly different approach that has been helpful in some situations-

Placeholders and Find & Replace.

Before parsing you can simply use your own custom linebreak marker/placeholder, then on the 2nd half of the situation just string replace it with whatever line break character is effective, whether that's \n or or or #&10; or \u2028 or any of the various line break characters out there. Find & replace them back in after setting the placeholder of your own in the data initially.

This is useful when parsers like jQuery $.parseXML() strip the unencoded line breaks. For example, you could use {LBREAK} as your line break char, insert it while raw text, and replace it later after parsed to an XML object. String.replaceAll() is a helpful prototype.

So rough code concept with jquery and a replaceAll prototype (have not tested this code but it will show the concept):

function onXMLHandleLineBreaks(_result){
    var lineBreakCharacterThatGetsLost = '&#10;';
    var lineBreakCharacterThatGetsLost = '&#xD;';
    var rawXMLText = _result; // hold as text only until line breaks are ready
        rawXMLText = String(rawXMLText).replaceAll(lineBreakCharacterThatGetsLost, '{mylinebreakmarker}'); // placemark the linebreaks with a regex find and replace proto
    var xmlObj = $.parseXML(rawXML); // to xml obj
    $(xmlObj).html( String(xmlObj.html()).replaceAll('{mylinebreakmarker}'), lineBreakCharacterThatWorks ); // add back in line breaks
    console.log('xml with linebreaks that work: ' + xmlObj);
}

And of course you could adjust the line break chars that work or don't work to your data situation, and you could put that in a loop for a set of line break characters that don't work and iterate through them to do a an entire set of linebreak characters.

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
QuestionTommyView Question on Stackoverflow
Solution 1 - XmlTomalakView Answer on Stackoverflow
Solution 2 - XmlAsaphView Answer on Stackoverflow
Solution 3 - Xmlrosca dragosView Answer on Stackoverflow
Solution 4 - XmlOG SeanView Answer on Stackoverflow