• +49-(0)721-402485-12
Your experts for XML, XQuery and XML Databases

Whitespace in element constructors

The treatment of whitespace in element constructors is controlled by the value of xmlspace from the XQuery prologue. In case it is not explicitly defined that whitespace must be preserved, boundary whitespace is removed. This is whitespace which only separates element boundaries ("start tag" and "end tag") and/or evaluation contexts from one another. Other whitespace is always preserved. Also whitespace which is indicated in the form of character references (for example  ) is preserved. So, the element constructor shown before is equivalent to:

<Volunteer ID="vol_01"><!-- inserted 1.1.2004--><Name><FirstName>Daniela</FirstName><LastName>Baumann</LastName></Name><Address><Street>Potsdamer Straße</Street><HouseNo>62</HouseNo><City>Berlin</City><Country>D</Country><PostalCode>14145</PostalCode></Address><DateOfBirth> {xs:date("1982-07-23")} </DateOfBirth><Phone>+49 30-234626</Phone><Number>07</Number><Competence>Elderly care</Competence><VocationalClass>Nurse</VocationalClass></Volunteer>

The following table shows some examples for results of element constructors with and without preservation of boundary whitespace:

Element constructorResult of the evaluation
with whitespace preservationwithout whitespace preservation
<X y="{1}   {2}"/><X y="1   2"/> <X y="12"/>
<X>      </X><X>     </X><X></X>
<X y="{1}  2"/><X y="1   2"/><X y="1   2"/>
<X>  1  {"Text"}  </X> <X>  1  "Text"  </X><X>  1  "Text"</X>
<X>&#20;     </X><X>     </X><X>     </X>
<X>{"    "}</X><X>   </X><X>   </X>
<X><![CDATA[    ]]></X><X>   </X><X>   </X>

Table: Examles for the treatment of whitespace in element constructors

In the first and second example you can find whitespaces between the evaluation contexts or tags. They are regarded as boundary whitespace. In contrast to that, the third example does not show boundary whitespace because no evaluation context but a simple character follows.
The fourth example analogously demonstrates that also whitespaces at the beginning of an element constructor are always preserved if no expression but a value follows. In the fifth example you can see that the character reference &#x20; for a space character also has the effect that no boundary whitespace is assumed. In the sixth and seventh example the whitespace can be found within an expression or a CDATA constructor and is therefore preserved.


Source: "XQuery – Grundlagen und fortgeschrittene Methoden", dpunkt-Verlag, Heidelberg (2004)

<< backnext >>