5

I am transforming values of appsettings in web.config file depending on the environment. I ran into an issue when there are keys with same name but with different cases, example as below

Local Value

<add xdt:Transform="RemoveAll" xdt:Locator="Match(key)" key="LOGINURL" value="xyf" />

Dev Value

<add xdt:Transform="RemoveAll" xdt:Locator="Match(key)" key="LoginUrl" value="abcd" />

I would like to replace the value of keys case insensitively.

TIA

Chakri
  • 770
  • 2
  • 7
  • 24

1 Answers1

5

You can use XPath with the Condition locator instead of Match. And using the hack described here on building case insensitive matching in XPath you may be able to write this:

<add xdt:Transform="RemoveAll" xdt:Locator= "Condition(translate(@key,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='loginurl')"/>

The key and value attribute are useless since the elements are removed.

To edit elements use SetAttributes to keep the keys untouched.

<add xdt:Transform="SetAttributes" xdt:Locator="Condition(translate(@key,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='loginurl')" value="test.com" />

I tested all of it here.

Orace
  • 7,822
  • 30
  • 45
  • I'm still looking on XPath2 – Orace Jun 25 '19 at 08:49
  • This isn't working with Microsoft transformation DLL :( – Chakri Jun 26 '19 at 09:54
  • @Chakri are you sure ? I tested it in VS2019, it works. Transformation are only visible while publishing see: https://stackoverflow.com/questions/8841075. Please use the transformation preview (Right click on the Web.Debug.config file -> preview transformation). – Orace Jun 26 '19 at 13:01