Ant xmlproperty 任务.当有多个同名标签时会发生什么? [英] Ant xmlproperty task. What happens when there is more than one tag with the same name?

查看:25
本文介绍了Ant xmlproperty 任务.当有多个同名标签时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循我得到的一个大型 ant 构建文件,但在这种情况下我无法理解 xmlproperty 的功能.考虑这个 xml 文件,example.xml.

I am trying to follow a large ant buildfile that I have been given, and I am having trouble understanding the functionality of xmlproperty in this case. Consider this xml file, example.xml.

<main>
  <tagList>
    <tag>
      <file>file1</file>
      <machine>machine1</machine>
    </tag>
    <tag>
      <file>file2</file>
      <machine>machine2</machine>
    </tag>
  </tagList>
</main>

在构建文件中,有一个任务可以简化为以下示例:

In the buildfile, there is a task which can be simplified to the following for this example:

<xmlproperty file="example.xml" prefix="PREFIX" />

据我所知,如果只有一个 元素,我可以使用 ${PREFIX> 获取 的内容.main.tagList.tag.file}因为大致相当于这样写:

As I understand it, if there was only one <tag> element, I could get the contents of <file> with ${PREFIX.main.tagList.tag.file} because it is roughly equivalent to writing this:

<property name="PREFIX.main.tagList.tag.file" value="file1"/>

但是由于有两个,那么在这种情况下${PREFIX.main.tagList.tag.file}的值是多少?如果它是某种列表,我如何遍历 两个值?

But as there are two <tag>s, what is the value of ${PREFIX.main.tagList.tag.file} in this case? If it is some sort of list, how do I iterate through both <file> values?

我使用的是 ant 1.6.2.

I am using ant 1.6.2.

推荐答案

当多个元素具有相同的名称时, 创建一个以逗号分隔值的属性:

When multiple elements have the same name, <xmlproperty> creates a property with comma-separated values:

<project name="ant-xmlproperty-with-multiple-matching-elements" default="run" basedir=".">
    <target name="run">
        <xmlproperty file="example.xml" prefix="PREFIX" />

        <echo>${PREFIX.main.tagList.tag.file}</echo>
    </target>
</project>

结果:

run:
     [echo] file1,file2

要处理逗号分隔值,请考虑使用 任务来自第三方 Ant-Contrib 库:

To process the comma-separated values, consider using the <for> task from the third-party Ant-Contrib library:

<project 
    name="ant-xmlproperty-with-multiple-matching-elements" 
    default="run" 
    basedir="." 
    xmlns:ac="antlib:net.sf.antcontrib"
    >
    <taskdef resource="net/sf/antcontrib/antlib.xml" />
    <target name="run">
        <xmlproperty file="example.xml" prefix="PREFIX" />

        <ac:for list="${PREFIX.main.tagList.tag.file}" param="file">
            <sequential>
                <echo>@{file}</echo>
            </sequential>
        </ac:for>
    </target>
</project>

结果:

run:
     [echo] file1
     [echo] file2

这篇关于Ant xmlproperty 任务.当有多个同名标签时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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