How to preserve leading and trailing spaces in a Boomi map when source is XML?
- Alex James
- Apr 10
- 2 min read
When doing a document translation from XML to XML or XML to EDI, the default behaviour of the Boomi maps is to trim the leading or trailing spaces while mapping to output field.
Please see the below the source XML where the author element has text “ Gambardella, Matthew “ with leading and trailing space.

When the above XML is translated into a different XML format using a map, the leading and trailing space in author elements gets trimmed.
Please see the map output with leading and trailing spaces removed.


While it will not be a problem in most cases, there might be situations where the leading/trailing spaces needs to be retained and cannot be dropped.
One of the immediate solutions that one would think of is to use a data process to search for “ “ and then replace it with an arbitrary string “WSPACE” and then reintroduce the space by data process search/replace “WSPACE with “ “. However, this solution will NOT work and exception as shown below will be thrown.


The problem with this approach is that the data process search/replace will replace all whitespaces in the XML document even the one in the XML declaration making the XML document invalid.
Please see below where “WSPACE” is even found in XML declaration.
The more robust and working solution would be to search/replace space only in text-only content with string “WSPACE”. This can be easily achieved using XSLT transformation

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:boomi="http://boomi.com/custom-function">
<xsl:output method="xml" indent="yes"/>
<!-- Identity template to copy everything by default -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- Template to replace whitespace in text-only elements -->
<xsl:template match="*[not(*) and normalize-space()]">
<xsl:copy>
<xsl:value-of select="replace(., '\s+', 'WSPACE')"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The above xslt will match elements that have no child elements (not(*)), have text content (normalize-space()) and then it replaces all whitespace (\s+) with "WSPACE"
Please see below the XSLT output

This XML will then be processed by the map component without issues. Immediately after the map, use a data process to replace string “WSPACE” with “ “

Please see the final desired output with leading and trailing spaces preserved.

Should you have anything to add, please write in comments,
Comments