문제

I have a requirement where a script scans a name/value pair xml and populates the values for future translation.

Here is a sample Test.xml

<?xml version="1.0" encoding="UTF-8"?>
<consumer>
<name>Sprint-1</name>
<value>1.0.0, 1.0.1</value>
<name>Sprint-2</name>
<value>1.1.0, 1.1.1</value>
<name>Sprint-3</name>
<value>1.2.0, 1.2.1</value>
</consumer>

Here is my ant script:

<xmlproperty file="Test.xml" collapseAttributes="true"/>
<for list="${consumer.name}" param="letter" delimiter=",">
        <sequential>
        <echo>@{letter}</echo>
    </sequential>
    </for>
<for list="${consumer.value}" param="string" delimiter=",">
    <sequential>
         <echo>@{string}</echo>
     </sequential>
    </for>

Output:

   [echo] Sprint-1
   [echo] Sprint-2
   [echo] Sprint-3
   [echo] 1.0.0
   [echo]  1.0.1
   [echo] 1.1.0
   [echo]  1.1.1
   [echo] 1.2.0
   [echo]  1.2.1    

It is clear that the output does not contain any "," separated values because ANT tokenizes the input values by delimiting them with the default "," separator.

Was interested in knowing if we can override the "," separator in ant so that by default it uses something like a "|" while scanning values from the xml file?

then I could use:

for list="${consumer.value}" param="string" delimiter="|"

ANTXtras seems to provide this feature but I could not get it working :(

Appreciate your help.

Thanks, Sandeep

도움이 되었습니까?

해결책

@aa W

Yes, we can use delimiter="|" in a for loop - but only when the list itself is tokenized with a "|" separator.

Problem here is when you use

<xmlproperty file="Test.xml" collapseAttributes="true"/> 

ANT by default scans the file and saves the list with a "," separator and I would like to override this default behavior.

Mater of fact while typing this, I just had a Eureka moment (Thanks to Mark!) and tried using

<xmlproperty file="Test.xml" collapseAttributes="true" delimiter="|"/>

now my script works as expected.

Output:

Test:
   [echo] Sprint-1
   [echo] Sprint-2
   [echo] Sprint-3
   [echo] 1.0.0, 1.0.1
   [echo] 1.1.0, 1.1.1
   [echo] 1.2.0, 1.2.1

다른 팁

Per : http://ant-contrib.sourceforge.net/tasks/tasks/for.html , for ant-contrib task uses comma as default delimeter. You can override to required value :

The delimiter characters that separates the values in the "list" attribute. 
Each character in the supplied string can act as a delimiter. 
This follows the semantics of the StringTokenizer class.

Required :

No, defaults to ",".

So you could change to look like:

<for list="${consumer.value}" param="string" delimiter="|">
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top