[ 
https://issues.apache.org/jira/browse/DAFFODIL-2346?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17555128#comment-17555128
 ] 

Mike Beckerle commented on DAFFODIL-2346:
-----------------------------------------

Update: This ticket is pretty outdated. We have learned much about the 
limitations of XML as a data language since then.

This comment is to clarify some things about space preservation. it is kind of 
an essay on using XML as a data language.

The xml:space='preserve' is about *whitespace-only* nodes in the text. The XML 
spec says "to set apart the markup", and [this site 
|http://www.xmlplease.com/xml/xmlspace/] clarifies that the spec means not 
whitespace generally, but only between XML elements.

So to clarify: xml:space='preserve' allows conversion of this:
```
<x xml:space='preserve'>
this is <y> some data </y>
<z> and more </z>
</x>
```
into
```
<x xml:space='preserve'>this is <y> some data </y>
<z> and more </z>
</x>
```
Note that the whitespace-only line ending between the end `</y>` and the `<z>` 
tag is preserved, as is the whitespace-only line ending betwen the `</z>` and 
the `</x>`. But the whitespace before the word "this" at the start of the 
content of element x, is NOT (necessarily) preserved. That's because this is 
not a whitespace-only node. It is part of the text node that is the first child 
node of element x. 

The default whitespace policy would let this whole thing be recast on a single 
line of text. 

Given this strange whitespace-only-node focus, the only thing xml:space is good 
for is something like this:
```
<myHaiku xml:space='preserve'>
<line>Of all the gin joints</line>
<line>In all the towns of the world</line>
<line>She walked into mine</line>
</myHaiku>
```
In that case, the xml:space='preserve' tells an XML processor to preserve the 
whitespaces before, between, and after the line elements. Those are all 
whitespace-only nodes. Note however, if this was deeply indented by a XML 
pretty printer, it could turn this into:
```
<myHaiku xml:space='preserve'>
<line>
  Of all the
  gin joints
</line>
<line>
  In all the towns
  of the world
</line>
<line>
  She walked into
  mine
</line>
</myHaiku>
```
So the whitespace inside the line elements would not be preserved. 

The conclusion from all of this is that xml:space attribute is just not helpful 
for much of anything when using XML as a data language, because as a data 
language we do NOT care about the whitespace between element tags. We ONLY care 
about the whitespace within elements of simple text content, and xml:space has 
nothing to do with those. 

Hence, using xml:space='preserve' is not a relevant way to solve this problem. 

First, it has nothing to do with whether CRLF and CR are converted to LF. XML 
parsers do this regardless of xml:space='preserve'. Second it has nothing to do 
with whether XML processors will clobber whitespace characters when trying to 
pretty print or wrap long lines. 

The only way to get XML text to preserve whitespace characters is this: 
# replace all whitespace characters by their equivalent numeric character 
entities. This includes spaces between words of ordinary text. 
# encapsulate all characters, including whitespace, with CDATA bracketing. 
Note that CDATA bracketing cannot contain the sequence "]]>" nor can it contain 
characters that must be preserved using numeric character entities like CR 
where &amp;#x0D; must be used. 

The best we can do is expect short strings containing single spaces to be 
preserved usually. This is a heuristic, but most likely is what people want. 
However any whitespace character other than a single space BETWEEN (only. Not 
at start or end of string.) words of short strings should be replaced by a 
numeric character entity. 
So consider this example where the line ending after the word 'spaces' is a CR:
```
<x>  some text with    four spaces
in a row</x>
```
If we escape everything, this must become
```
<x>&#x20;&#x20;some text with &#x20;&#x20;&#x20;four spaces&#x0D;in a row</x>
```
or this:
```
<x><![CDATA[  some text with    four spaces]]>&#x0D;<![CDATA[in a row]]></x>
```
Both are pretty ugly, but given the infrequency of CR in data, that particular 
ugliness is less likely to actually appear. 

Long strings need to always get the escaping per above (either replace all 
whitespace with entities, or use CDATA where possible).  

A mode where the escaping is done for ALL strings should be available as well. 

> XML Output needs option to use <![CDATA[     ]]> around simple element values 
> containing whitespace.
> ----------------------------------------------------------------------------------------------------
>
>                 Key: DAFFODIL-2346
>                 URL: https://issues.apache.org/jira/browse/DAFFODIL-2346
>             Project: Daffodil
>          Issue Type: Bug
>          Components: Back End
>    Affects Versions: 2.6.0
>            Reporter: Mike Beckerle
>            Priority: Minor
>
> It is incredibly painful to take the XML output, pretty print it to make it 
> readable, and find out that this has mangled the significant whitespace 
> inside element values. 
> In general, since whitespace within simple values is considered fungible in 
> XML, we have to protect whitespace that is truly part of the DFDL infoset. 
> I think CDATA bracketing is preferable to replacing whitespace characters 
> with XML escaping like &amp;#x20; 



--
This message was sent by Atlassian Jira
(v8.20.7#820007)

Reply via email to