Xpath Nant中的xmlpoke-如何更新找到的字符串的所有实例
嗨,我在Nant构建脚本中使用Xpath在开发和其他环境之间更改一些配置变量 我采用了本例中的语法: 示例如下所示:Xpath Nant中的xmlpoke-如何更新找到的字符串的所有实例,xpath,nant,xmlpoke,Xpath,Nant,Xmlpoke,嗨,我在Nant构建脚本中使用Xpath在开发和其他环境之间更改一些配置变量 我采用了本例中的语法: 示例如下所示: <xmlpoke file="config01/app.config" xpath="/configuration/appSettings/add[@key='AppName']/@value" value="TradeMonster"> </xmlpoke> 我想要的是类似于此的东西来搜索我的连接字符串,找到“localho
<xmlpoke
file="config01/app.config"
xpath="/configuration/appSettings/add[@key='AppName']/@value"
value="TradeMonster">
</xmlpoke>
我想要的是类似于此的东西来搜索我的连接字符串,找到“localhost\SqlExpress”的所有实例,并将它们更改为“localhost”
这可能吗?XPath只选择节点,不能更改节点 完成所需更改的一种方法是对XML文档执行XSLT转换
为了实现这一点,您必须提供XML文档,并准确指定要更改其文本节点。在这里玩弄quick'n dirty脚本 如果您确定每个文件中只有一个connectionstring元素,那么您可以通过组合使用
xmlpek
和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只选择节点,但xmlpoke(NAnt函数)更改XPath选择的节点。Xmlpoke就是问题所在。@azheglov:这个问题被标记为“xpath”。甚至“我正在使用XPath更改…”这句话也不正确。OP希望XPath表达式“搜索我的连接字符串”,但他没有提供应用XPath表达式的XML文档——因此这是一个不完整的问题。这就是我在回答中所表达的全部。这真的很有帮助。