蚂蚁,replaceregexp,比赛和在比赛中的列表替换特定元素 [英] Ant, replaceregexp, match and replace specific element in the list of matches

查看:223
本文介绍了蚂蚁,replaceregexp,比赛和在比赛中的列表替换特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Ant构建文件,我有一个需要更换的XML的特定元素的任务。

In my ant build file, I have a task that needs to replace a specific element of a XML.

下面是我想修改目标XML:

Here is the target XML that I am trying to modify:

<foo>
 <sub>
  <elem>name1</elem>
 </sub>
 <sub>
  <elem>name2</elem>
 </sub>
 <sub>
  <elem>name3</elem>
 </sub>
</foo>

Ant构建任务:

Ant build task:

<replaceregexp file="myfoo.xml"
      match="&lt;elem&gt;(.*)elem&gt;"
      replace="&lt;elem&gt;${replace_only_second_match}elem&gt;"
      byline="true"
/>

与上述任务的问题是,所有的标签都将被替换。但是,我想只有第二元件进行修改,而不是第一或第3匹配。 (这样的事情是正常的规律前pressions很容易的。)

The problem with the above task is that all the tags will get replaced. However, I want only the second element to be modified, not the first or 3rd match. (such a thing is quite easy with normal regular expressions.)

不知道如何使用Ant的常规前pression做到这一点。这是我需要就如何最好地解决此问题的帮助/建议。

Dont know how to do it with Ant's regular expression. This is where I need help/suggestions on how best to solve this problem.

推荐答案

您应该使用 xmltask 对于xmlrelated任务,您的问题用它这样的:

You should use xmltask for xmlrelated tasks, for your problem use it like that :

修改文件就地

<xmltask source="whatever.xml" dest="whatever.xml">
  <replace path="//sub[2]/elem/text()" withText="newname2"/>
</xmltask>

创建新文件

<xmltask source="whatever.xml" dest="newfile.xml">
  <replace path="//sub[2]/elem/text()" withText="newname2"/>
</xmltask>

该replacesection还提供了 withXml / withFile / withBuffer 。结果
见xmltask 手动并的教程了解详情。结果
XPath的一些要领这里

The replacesection also provides withXml / withFile / withBuffer.
See xmltask manual and tutorial for details.
Some XPath essentials here.

这篇关于蚂蚁,replaceregexp,比赛和在比赛中的列表替换特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆