2

我想xdt:TransformDebug配置中执行一次,但前提是条目的值app.debug.config是某个值,让我们说true保持简单。例如:

应用程序配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings> 
    <add key="Value.First" value="foo" />
    <add key="Value.Second" value="foo" />
    <add key="Value.Third" value="foo" />
  </appSettings>
</configuration>

应用程序调试配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <!--Convenient flag to indicate if transform should happen-->
    <add key="Perform.Transform.If.True" value="true" xdt:Transform="Insert" />

    <!--Should only happen if the above is true-->
    <add key="Value.First" value="bar" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
    <add key="Value.Second" value="bar" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
    <add key="Value.Third" value="bar" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
  </appSettings>
</configuration>

我希望Value.*app.config 中的所有条目仅在密钥Perform.Transform.If.True设置为true. 如果是false什么都不应该发生。原因是有时在测试期间我们想快速打开和关闭由配置文件控制的东西。

我已经看到了Locator诸如 Match、Conditional、XPath 等的选项,但似乎没有一个允许来自另一个条目的条件。可以用 slowcheetah/xdt 转换来完成吗?

4

1 回答 1

0

好吧,找到了一个迂回的方式使用XPath

 <add
    key="Value.First" 
    value="bar" 
    xdt:Transform="Replace"
    xdt:Locator="XPath(//appSettings/add[@key='Perform.Transform.If.True' and translate(@value, 'ERTU', 'ertu')='true']/../add[@key='Value.First'])" 
/>

如果标志不是true,它将无法解析路径。translate使其不区分大小写。

无法解析 (is false) 会导致编译警告,这很烦人,但由于这是一个调试工具,我们可以忍受。

于 2019-10-25T11:33:59.500 回答