在 Web.config 转换中使用 xdt:定位器按条件"starts-with"或"contains"的问题



我正在尝试创建一个web。配置转换文件,如果名称中包含单词"config",则将appSettings值列表更改为"false"。

<add name="Config.Showlog" value ="true" />

转换文件有

<appSettings>
    <add xdt:Transform="SetAttributes(value)" 
         value="false" 
         xdt:Locator="Condition(starts-with(@name,'Config')"/>
</appSettings>

Visual Studio 2010显示错误:

条件只需要1个参数

我还尝试了Xpath作为xdt:定位器的属性,并得到了相同的错误。似乎问题来自VS 2010如何解析Condition()Xpath()内的表达式。

如何解决这个问题?

我想出了以下解决方案:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add xdt:Transform="SetAttributes(value)"
         value="false"
         xdt:Locator="Condition(contains(@key, 'Config'))"/>
  </appSettings>
</configuration>

这将把key属性中包含'Config'的<appSettings><add>元素的所有value属性设置为'false'。

<add key="SomeOtherAppSettings"
     value="OriginalValue" />
<add key="An entry containing Config in the key attribute"
     value="false" />

此问题是Visual Studio 2010中安装的Microsoft.Web.Publishing.Tasks.Dll中的一个错误。

微软已经更正了Visual Studio 2012 RTM的问题(见反馈)。

对于那些仍然在Visual Studio 2010上的人,用$(MSBuildToolsPath)MSBuildMicrosoftVisualStudiov11.0Web中的更新文件替换$(MSBuildToolsPath)MSBuildMicrosoftVisualStudiov10.0Web中的Microsoft.Web.Publishing.Tasks.Dll将解决问题并允许成功构建。

这是Visual Studio 2010的一个bug。微软已经在Visual Studio 2012中修复了这个问题

http://connect.microsoft.com/VisualStudio/feedback/details/618550/web-config-xpath-and-condition-locators-do-not-allow-commas-in-xpath-expression

最新更新