如何<foreach>在<宏定义>? [英] How to <foreach> in a <macrodef>?

查看:26
本文介绍了如何<foreach>在<宏定义>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的 xml:

I have a xml just like below:

<data>

    <foo>value1</foo>

    <foo>value2</foo>

    <foo>value3</foo>

  </data>

我想创建实现以下功能的macrodef:

I want to create macrodef which implements below function:

<?xml version="1.0"?>
<project name="OATS" default="execute" basedir=".">

  <xmlproperty file="data.xml" collapseAttributes="true"/>
  <target name="execute">
    <foreach list="${data.foo}" target="runScript" param="script"/>
  </target>
  <target name="runScript">
    <echo>Doing things with ${script}</echo>
  </target>
</project>

有人知道怎么做吗?提前致谢.

Anybody knows how to ? Thanks in advance.

推荐答案

xmltask 是 Ant 社区中用于此目的的最佳选择,您不必定义自己的宏定义.

xmltask is the best choice in the Ant community for this purpose, and you don't have to define your own macrodef.

例如:

  <tools:xmltask source="data.xml" report="false" >
    <tools:call path="data/foo">
      <param name="value" path="text()"/>
      <actions>
          <echo>Doing things with @{value}</echo>
      </actions>
    </tools:call>
  </tools:xmltask>

我鼓励您阅读用户手册,因为 xmltask 有很多选项.它基本上支持 XPath 提取和迭代 xml 的任何部分.除了匿名代码块(如示例中所示)之外,它还支持对现有目标的调用.

I encourage you to read the user manual, for xmltask has lots of options. It basically supports XPath to extract and iterate any portion of your xml. It also supports calls to existing targets in addition to anonymous code blocks (as in the example).

很难被击败.

这篇关于如何&lt;foreach&gt;在&lt;宏定义&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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