代码之家  ›  专栏  ›  技术社区  ›  Remotec

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

  •  5
  • Remotec  · 技术社区  · 14 年前

    嗨,我在nant构建脚本中使用xpath在开发和其他环境之间更改一些配置变量。

    我从这个例子中得到了语法:

    示例如下:

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

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

    这有可能吗?

    2 回复  |  直到 14 年前
        1
  •  4
  •   Peter Lillevold Rene    14 年前

    玩弄一个又快又脏的剧本…

    如果您确定每个文件中只有一个connectionString元素,可以通过以下组合来完成此操作: xmlpeek xmlpoke . 使用一些C可以更容易地修改字符串,因此使用脚本任务执行regex搜索和替换:

     <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>
    
        2
  •  0
  •   Dimitre Novatchev    14 年前

    xpath只选择节点,不能更改节点 .

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

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