相关疑难解决方法(0)

XDT转换:InsertBefore - 忽略定位器条件

我有一个web.config文件,我需要插入该<configSections />元素或操作该节点的子节点(如果它已经存在).
如果它已经存在,我不想再插入它(显然,因为它只允许存在一次).

通常,这不会是一个问题,但是:

如果此元素位于配置文件中,则它必须是元素的第一个子元素.

资料来源:MSDN.

所以,如果我使用xdt:Transform="InsertIfMissing"<configSections />元素将始终任何现有的子元素后插入(及总有一些),违反不必是第一个子元素的它上面的限制<configuration />

我试图通过以下方式完成这项工作:

 <configSections
    xdt:Transform="InsertBefore(/configuration/*[1])"
    xdt:Locator="Condition(not(.))" />
Run Code Online (Sandbox Code Playgroud)

如果<configSections />元素尚不存在,哪个工作完美.但是,我指定的条件似乎被忽略了.

事实上,我尝试了一些条件,如:

Condition(not(/configuration[configSections]))
Condition(/configuration[configSections] = false())
Condition(not(/configuration/configSections))
Condition(/configuration/configSections = false())
Run Code Online (Sandbox Code Playgroud)

最后,出于绝望,我试过:

Condition(true() = false()) 
Run Code Online (Sandbox Code Playgroud)

它仍然插入了<configSections />元素.

重要的是要注意我正在尝试将其包含在NuGet包中,因此我将无法使用自定义转换(如AppHarbor使用的那个).

有没有其他聪明的方法可以将我的元素放在正确的位置,只有它尚不存在?

要测试它,请使用AppHarbors配置转换测试器.用以下内容替换Web.config:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="initialSection" />
  </configSections>
</configuration>
Run Code Online (Sandbox Code Playgroud)

和Web.Debug.config具有以下内容:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

  <configSections
    xdt:Transform="InsertBefore(/configuration/*[1])"
    xdt:Locator="Condition(true() = false())" />

  <configSections>
    <section name="mySection" xdt:Transform="Insert" />
  </configSections>

</configuration>
Run Code Online (Sandbox Code Playgroud)

结果将显示两个<configSections /> …

web-config web-config-transform xdt-transform

23
推荐指数
1
解决办法
9703
查看次数