Ant xmlproperty任务.如果有多个同名标签,会发生什么情况? [英] Ant xmlproperty task. What happens when there is more than one tag with the same name?
问题描述
我正在尝试遵循给出的大型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" />
据我了解,如果只有一个<tag>
元素,我可以使用${PREFIX.main.tagList.tag.file}
获取<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"/>
但是由于有两个<tag>
,在这种情况下${PREFIX.main.tagList.tag.file}
的值是多少?如果是某种列表,如何遍历两个<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.
推荐答案
当多个元素具有相同的名称时,<xmlproperty>
创建一个带有逗号分隔值的属性:
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
要处理逗号分隔的值,请考虑使用 <for>
任务第三方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屋!