问题描述
在我的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="<elem>(.*)elem>"
replace="<elem>${replace_only_second_match}elem>"
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.
推荐答案
您应该使用并的了解详情。结果
XPath的一些要领。
The replacesection also provides withXml / withFile / withBuffer
.
See xmltask manual and tutorial for details.
Some XPath essentials here.
这篇关于蚂蚁,replaceregexp,比赛和在比赛中的列表替换特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!