繁体   English   中英

使用不同样式表的XSLT任务

[英]XSLT task using different stylesheet

首先,请原谅我的英语不好。

这是问题所在:我有一个目录stylesheet-dir ,其中有未定义数量的xsl文件,每个文件当然都具有不同的名称。 我需要的是xslt任务来执行与目录中文件一样多的转换。 到目前为止,这应该可以完成工作:

    <target name="do-report">
        <xslt basedir="${stylesheet-dir}" destdir="${output_dir}" style="stylesheet.xsl" force="true">
            <classpath location="${processor_path}"/>
        </xslt>
    </target>

问题^^以上^^,是每个转换的stylesheet.xsl都相同。 我需要样式表使每次转换保持一致。 最有趣的部分是输入文件和样式表应该相同。 我一直在通过互联网寻找解决方法,但没有成功。 很感谢任何形式的帮助。

Ant不是一种脚本语言,并且仅具有对文件集进行迭代的有限支持,例如apply task 在这些情况下,我需要一个嵌入式的Groovy脚本,该脚本能够调用其他ANT任务。

├── build.xml
├── src
│   ├── xml
│   │   ├── file1.xml
│   │   ├── file2.xml
│   │   └── file3.xml
│   └── xsl
│       ├── file1.xsl
│       └── file2.xsl
└── target
    ├── xsl1
    │   ├── file1.xml
    │   ├── file2.xml
    │   └── file3.xml
    └── xsl2
        ├── file1.xml
        ├── file2.xml
        └── file3.xml

build.xml

<project name="demo" default="build">

  <available classname="org.codehaus.groovy.ant.Groovy" property="groovy.installed"/> 

  <target name="build" depends="install-groovy">
    <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>

    <fileset id="xslfiles" dir="src/xsl"  includes="*.xsl"/>
    <fileset id="xmlfiles" dir="src/xml"  includes="*.xml"/>

    <groovy>
      def count = 1
      project.references.xslfiles.each { xsl ->
        project.references.xmlfiles.each { xml ->
          def xslfile = new File(xsl.toString())
          def xmlfile = new File(xml.toString())
          def outfile = new File("target/xsl${count}", xmlfile.name)

          ant.xslt(force:true, style:xslfile, in:xmlfile, out:outfile)
        }
        count++
     }
    </groovy>
  </target>

  <target name="install-groovy" unless="groovy.installed">
    <mkdir dir="${user.home}/.ant/lib"/>
    <get dest="${user.home}/.ant/lib/groovy-all.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.4.5/groovy-all-2.4.5.jar"/>
    <fail message="groovy has been installed. Run the build again"/>
  </target>

</project>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM