NANT中的XMLPOKE-如何更新发现字符串的所有实例
题
嗨,我正在Nant构建脚本中使用XPATH来更改开发和其他环境之间的一些配置变量。
我从这个示例中获取了语法:
这个示例看起来像这样:
<xmlpoke
file="config01/app.config"
xpath="/configuration/appSettings/add[@key='AppName']/@value"
value="TradeMonster">
</xmlpoke>
我想要的是与此类似的东西,以搜索我的连接字符串并找到“ localhost sqlexpress”的所有实例,然后将它们更改为“ localhost”
这可能吗?
解决方案
在这里使用Quick'n Dirty脚本玩耍。
如果您确定每个文件中只有一个ConnectionsTring元素,则可以结合使用 xmlpeek
和 xmlpoke
. 。修改字符串对某些C#进行更容易完成,因此使用脚本任务进行正则搜索并替换:
<script language="C#" prefix="custom" >
<code>
<![CDATA[
[Function("fix")]
public static string Fix(string input) {
return Regex.Replace(input, @"localhost\\\w+", "localhost");
}
]]>
</code>
</script>
<!-- Get the existing connection string -->
<xmlpeek
file="config01/app.config"
xpath="/configuration/connectionStrings/add[@contains(@connectionString,'localhost\')]/@connectionString"
property="connectionstring">
</xmlpeek>
<!-- Write back the modified connection string -->
<xmlpoke
file="config01/app.config"
xpath="/configuration/connectionStrings/add[@contains(@connectionString,'localhost\')]/@connectionString"
value="${custom::fix(connectionstring)}">
</xmlpoke>
其他提示
XPath仅选择节点,它不能更改节点.
完成所需更改的一种方法是在XML文档上执行XSLT转换。
为了实现这一目标,您必须提供XML文档,并确切地指定要更改其哪些文本节点。
不隶属于 StackOverflow