Preserving white space in XML

When editing code in your documents, you will want to edit white space as well as textual content. Without explicit white space preservation, all your code will end up on one line. Here we will explore the techniques involved in preserving white space.

XML: xml:space="preserve"

One way is to explicity state in your document that for the given element, white space is in fact significant, and thus should be preserved. The XML standard provides an attribute that can state exactly that: xml:space. Here's an example how you could put it to use:

<para>Don't try this at home:</para>
<blockcode xml:space="preserve">infiniteLoop: while (true)
{
  continue infiniteLoop;
}</blockcode>

XML Schema: xs:whiteSpace="preserve"

… as defined in the XML Schema specification.

This functionality however has not yet been implemented in Xopus, so for now I will not elaborate on this method. As an alternative, you could declare the xml:space attribute to preserve white space by default:

<xs:element name="blockcode">
  <xs:complexType mixed="true"
    <xs:attribute ref="xml:space" default="preserve"/>
  </xs:complexType>
</xs:element>

HTML and CSS: <pre> or selector { white-space: pre }

To present your XML in accordingly faishion, we should also let the browser know the white space is significant:

blockcode
{
  white-space: pre;
}

In HTML, the pre element has this style applied by default, so you could render it using that element:

<xsl:template match="blockcode">
  <pre>
    <xsl:apply-templates/>
  </pre>
</xsl:template>

White space handling in Xopus

  • White space is collapsed onload. It skips all elements that have xml:space="preserve" in it's ancestor path.

  • It doesn't insert white space that will be collapsed (prevalidation). This means that when you type a space at the end of sentence, the space will appear only after the next character has been typed.


History

version event
Xopus 3.2.2 Change. Added support for xml:space="preserve".
Xopus 3 Introduction.