Mapping a Newline within a String

Today a colleague asked me how best to insert newlines into a concatenated set of address lines within a map. It struck me that despite spending eleven years as a BizTalk developer, creating hundreds if not thousands of maps,  and even authoring a Pluralsight course on the Mapper,  I’ve never actually attempted something as simple as inserting a newline into a string output!

Like most programmers with C# .NET experience, the first instinct was to try inserting “\n” or “\r\n” into the string. But of course that wouldn’t work – the output of a non-scripted mapper link is XSLT, not .NET code.

So that leads to the assumption that an XML entity character is required; surely that will work? Since the string “&#13&#10” is the XML entity representation of a carriage return followed by a line feed (CR LF), we should be able to just insert that as a constant parameter within the String Concatenate functoid:

01

But then it becomes evident that the mapper escapes all characters that might otherwise be unintentionally interpreted as markup:

02

Hmmm… not turning out to be as easy as we thought. Surely we don’t need to resort to inline XSLT just to insert a newline into text?

This answer by an MVP on MSDN suggests using a bit of C# script to insert the non-printable characters. That certainly works, since strings within a script block are interpreted in the context of the script language (i.e. a “\n” is correctly translated into a newline). In fact, that would probably be the best option if you had multiple newlines to embed within a single concatenated string.

However, I thought of another way, employing the little-used Conversion functoids:

03

The “ASCII to Character” functoid translates a numerical value into its character equivalent. So  by using two of these, one set to 10 and another set to lucky number 13 (shown below), you can output the necessary non-printable characters (you can look up ASCII codes on sites like this one if you’re not sure which values to use):

04

Then you simply direct the output of these babies as input parameters to your String Concatenate functoid (I set the label property on the two conversion functoids  to “ASCII 13” and “ASCII 10” first, so I could tell which one was which within the parameter grid):

05

Now when you first test the map, you might think, “Wait! It’s not working!!”  This is because the default browser within Visual Studio uses Internet Explorer’s “pretty print” stylesheet  which strips out all whitespace it deems extraneous (including newlines):

06

But if you view the source behind this, you will see the newline correctly appear:

07

Is this better than using an inline script? Depends on your circumstances I guess. Some developers prefer to have the visual story of the map evident within the mapper grid rather than hiding functionality within a scripting functoid. I suppose a good argument for the script is that in some cases you’d need multiple instances of the character functoid pairs as inputs, since a single parameter can only be used once within a functoid configuration.

At least one thing is for sure… after all these years, I’ve finally found a use for a conversion functoid!

About Dan Toomey
Enterprise integration geek, Microsoft Azure MVP, Pluralsight author, public speaker, MCSE, MCT, MCTS & former professional musician.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

John Glisson - Geek of the Cloth

Thoughts on integration, technology and what-not...

Prashant BizTalk And Azure Integration Blogs

My Integration Experiences - BizTalk And Azure Integration

The CRUCIBLE

THINK: It's not illegal....yet.....

Abdul Rafay's BizTalk Blog

My experiences with BizTalk related to architecture, development and performance in my enterprise.

BizTalk musings

Issues, patterns and useful tips for BizTalk development

EAI Guy.net

Enterprise Applicaiton Integration and SOA 2.0

Connected Pawns

Mainly BizTalk & Little Chess

Adventures inside the Message Box

BizTalk, Azure, and other tools in the Microsoft stack - Johann Cooper

Biz(Talk)2

Talk, talk and more talk about BizTalk

Richard Seroter's Architecture Musings

Blog Featuring Code, Thoughts, and Experiences with Software and Services

Sandro Pereira BizTalk Blog

My notes about BizTalk Server 2004, 2006, 2006 R2, 2009, 2010, 2013 and now also Windows Azure BizTalk Services.

BizTalk Events

Calendar of BizTalk events all over the world!

Mind Over Messaging

Musings on BizTalk, Azure, and Enterprise Integration

WordPress.com News

The latest news on WordPress.com and the WordPress community.

%d bloggers like this: