Config transform problem - how to skip undefined or empty variables?

We have configuration transform which supposed to replace fixed value in App.config with Octopus variable, something like this:
<add key="UrlKey" value="#{UrlVariable}" xdt:Locator="Match(key)" xdt:Transform="Replace" />
It generally works as expected when UrlVariable has some meaningful value, but in some cases it is not defined or defined as empty string. In those cases instead of replacement we need to remove UrlKey from config file completely. The whole line.

HOW to do that?

Thank you!
Konstantin

Hi,

The below code in the transform logic should take care of this

xdt:Transform="#{if UrlVariable}Replace#{/if}#{unless UrlVariable}Remove#{/unless}"

Dalmiro

Thank you, Dalmiro! It works, but unfortunately it requires UrlKey to be present (with some value) in the original config file before transform. If it is not present, then Replace would fail. Ideally I need transform which would work the same regardless of the UrlKey being present or not present in the original config.

The best I could come out with is two lines solution - first line does “InsertIfMissing” transform and second line is yours Replace or Remove. Looks ugly like hell.

Any idea how to make it in one line with less repetition of the same words again and again?

Konstantin

Just to show you how ugly it is:

We have to repeat variable name six times (!!) only to set WebServiceOpsId to the value of variable unless it is undefined.

Hi - I can’t think of a better way to make this work with less lines. I’m afraid that’s the way the Microsoft transformation syntax works.