Nant 中的 xmlpoke - 如何更新找到的字符串的所有实例

发布于 2024-08-31 09:25:29 字数 375 浏览 4 评论 0原文

您好,我在 Nant 构建脚本中使用 Xpath 来更改开发环境和其他环境之间的一些配置变量。

我从这个例子中获取了语法:

这个例子看起来像这样:

<xmlpoke
    file="config01/app.config"
    xpath="/configuration/appSettings/add[@key='AppName']/@value"
    value="TradeMonster">
</xmlpoke>

我想要的是与此类似的东西来搜索我的连接字符串并找到“localhost\SqlExpress”的所有实例,然后将它们更改为“localhost”

是这样的可能的?

Hi I am using Xpath in my Nant build script to change some config variables between development and my other environments.

I have taken the syntax from this example:

The example looks like this:

<xmlpoke
    file="config01/app.config"
    xpath="/configuration/appSettings/add[@key='AppName']/@value"
    value="TradeMonster">
</xmlpoke>

What I would like is something similar to this to search my connection strings and find all instances of "localhost\SqlExpress" and just change them to just "localhost"

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安人多梦 2024-09-07 09:25:29

在这里玩弄一个快速而肮脏的脚本......

如果您确定每个文件中只有一个连接字符串元素,您可以使用 xmlpeekxmlpoke 的组合来完成此操作代码>.使用某些 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>

Toying with a quick'n dirty script here....

If you're sure there is only one connectionstring element in each file you can accomplish this with a combination of xmlpeek and xmlpoke. Modifying the string is easier done with some C#, therefore using a script task to do a regex search and replace:

 <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>
如痴如狂 2024-09-07 09:25:29

XPath 只能选择节点,不能更改节点

完成所需更改的一种方法是对 XML 文档执行 XSLT 转换。

为了实现这一点,您必须提供 XML 文档并准确指定要更改其文本节点。

XPath only selects nodes, it cannot change nodes.

One way to accomplish the needed changes is to perform an XSLT transformation on the XML document.

In order to make this happen, you have to provide the XML document and to specify exactly which of its text nodes are to be changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文