0

I have an xml file (myxml.xml) like this

<fileset dir ="C:\dir1">
    <include name="abc.txt">
    <include name ="def.txt">
</fileset>
<fileset dir ="C:\dir2">
    <include name="abc2.txt">
    <include name ="def2.txt">
</fileset>

I want to parse this file using my Ant build.xml and want two txt files to be created say text1.txt and text2.txt, with following content

text1.txt

C:\dir1\abc.txt
C:\dir1\def.txt

and text2.txt should have

C:\dir2\abc2.txt
C:\dir2\def2.txt

Any idea how I can achieve this :)

shruti
  • 1
  • 1
  • Is it okay to add two stylesheets that will output to the separate txt files instead of using xmltask? – randominstanceOfLivingThing Sep 11 '12 at 14:41
  • 2
    Why do you want to use Ant? Wouldn't you be better off with a full programming language such as Python, Perl, or Powershell (since you're on Windows)? Ant is a matrix dependency language. It's built for building software (especially Java code). If this is part of a build.xml, I can understand, but if this is merely trying to create a couple of files from XML, you're better off with a true programming language. Think about your requirements, and retag and edit your question if you need to. – David W. Sep 11 '12 at 14:41
  • @DavidW. - Yes this is a part of build.xml and the xml file that i need to parse is also part of the build. – shruti Sep 11 '12 at 15:18
  • @SureshKoya - I tried using xmltask but no success, I am not sure how to use stylesheets in build.xml – shruti Sep 11 '12 at 15:20

2 Answers2

0

If you have the option to include the <fileset> tags in your build.xml file, you could create text files containing the contents of the filesets as follows.

<target name="create-text1">
  <fileset dir="C:\dir1" id="dir1.files">
    <include name="abc.txt" />
    <include name ="def.txt" />
  </fileset>
  <pathconvert pathsep="${line.separator}"
      property="text1" refid="dir1.files"/>
  <echo file="text1.txt" message="${text1}" />
</target>

<target name="create-text2">
  <fileset dir="C:\dir2" id="dir2.files">
    <include name="abc2.txt" />
    <include name ="def2.txt" />
  </fileset>
  <pathconvert pathsep="${line.separator}"
      property="text2" refid="dir2.files"/>
  <echo file="text2.txt" message="${text2}" />
</target>
Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
  • I can't add the directory path and the file names in build.xml. I have to read them from the myxml.xml file. Although the pathconvert mentioned here is working very well and this code also creates the files but it would be perfect if it read the myxml.xml file too :( – shruti Sep 12 '12 at 09:16
0

Try this:

Build file:

<project default="createfile2">
<target name="createfile1">
    <xslt in="myxml.xml" out="text1.txt" style="test1.xslt">
        <param name="d" expression="C:\dir1"/>
    </xslt>
</target>

    <target name="createfile2" depends="createfile1">
    <xslt in="myxml.xml" out="text2.txt" style="test1.xslt">
        <param name="d" expression="C:\dir2"/>
    </xslt>
</target>
</project>

XSLT transformation

<?xml version="1.0" standalone="yes"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:param name="d"/>
 <xsl:output omit-xml-declaration="yes" method="html"  indent="no"/>
 <xsl:strip-space elements="*"/>
<xsl:template match="/root/fileset[@dir=$d]">
    <xsl:for-each select="include">
        <xsl:value-of select="$d"/>\<xsl:value-of select="@name"/>
<xsl:text>
</xsl:text>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Output files

text1.txt

C:\dir1\abc.txt
C:\dir1\def.txt

text2.txt

C:\dir2\abc2.txt
C:\dir2\def2.txt
randominstanceOfLivingThing
  • 16,873
  • 13
  • 49
  • 72
  • Hi Suresh, thankyou for your solution. I am having 2 problems with this 1) I am also getting some unwanted tag values being printed before the wanted lines in both the text files. These unwanted tag values appear in myxml.xml before the template-match expression. 2) Is there an alternative to not referring the directory name (expression="C:\dir2") in build.xml? – shruti Sep 12 '12 at 11:34
  • Ok the problem 1) i mentioned above has been solved by adding the after tag mentioned in solution above. But problem 2 still exists... – shruti Sep 12 '12 at 12:20
  • You can have the hardcoding done in the xslt files themselves. I tried to avoid having two xslt files with the directory name hardcoded. – randominstanceOfLivingThing Sep 12 '12 at 15:03
  • Do you mean hardcoding all the dir paths in another xslt? I dont mind using two xslt but i dont want to hardcode path/ duplicate all dir paths in myxml.xml – shruti Sep 12 '12 at 16:04