You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by "Cox, Charlie" <cc...@cincom.com> on 2003/02/07 14:25:19 UTC

xslt task with foreach

I'm having trouble getting this task to work. What I need is to run an xslt
task for each file in the directory and I need the filename to be passed as
a parameter to the xslt.

This is what I have so far and it works for one file, but when I have many
files, the xslt task processes all the files passing in the name of the
first file to each one, then it tries to complete the foreach by running the
xslt task for the rest, which does nothing since it already did them all.

<target name="all">
	<foreach param="htmlFileName" target="convertXsl">
		<fileset dir="html" includes="*.htm"/>
	</foreach>
</target>

<!-- process a given file. called by the 'all' target -->
<target name="convertXsl">
	<!-- get the name of the document without the path -->
	<basename file="${htmlFileName}" property="baseName"/>
	<echo message="processing....${baseName}"/>
	<!-- convert the style first -->
	<xslt basedir="html" destdir="xsl" extension=".xsl"
style="myxsl.xsl" processor="trax" >
		<param name="myFormName" expression="${baseName}"/>
		<xmlcatalog>
			<dtd publicId="-//W3C//DTD XHTML 1.0
Transitional//EN" location="xhtml.dtd"/>
		</xmlcatalog>
	</xslt>
</target>

I tried adding includes="$(baseName)" for the xslt task, which caused it to
not process any files. Likewise for includes="$(htmlFileName)".

how can I get the foreach to go through the list of files and the xslt to
process only the given file?

thanks,
Charlie

Re: xslt task with foreach

Posted by Dwayne Schultz <dw...@schultz.net>.
On Friday, Feb 7, 2003, at 05:25 US/Pacific, Cox, Charlie wrote:

> I'm having trouble getting this task to work. What I need is to run an 
> xslt
> task for each file in the directory and I need the filename to be 
> passed as
> a parameter to the xslt.

Have you looked at systemId()?  It's an XPath function in the 
http://icl.com/saxon namespace but I am sure Xalan has a similar 
function.  We do something like this:

<xsl:value-of select="substring-after(saxon:systemId(), $SourceDir)"
     xmlns:saxon="http://icl.com/saxon"/>

where $SourceDir is passed into the task like:

<param name="SourceDir" expression="${basedir}"/>

Should get you the path relative to basedir like "dir/file.xml".

Dwayne