Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

7. Using XInclude with Eclipse XSL

7.1. Overview

XInclude can be used to structure the DocBook source files so that you have one file per chapter / section and one master file which includes these files. Via XInclude these separate files can be combined into on file.

You can for example include a file "foo.xml" into another one via the following statement

				
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="foo.xml" />
			
In case this file should be treated as text:
				
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="bar.xml" />
			

A XInclude ant task is provided by the Eclipse XSL project. I can proudly say that this ant task was contributed by me to the Eclipse XLS project. :-)

7.2. Eclipse XSL Tools

Eclipse XSL Tools provide support for XSLT transformations. It supports XSL editing and debugging support. We will only use the XInclude task but you have to install the whole package.

Install the XSL tools via the update manager from the standard Eclipse Galileo update site. See Using the Eclipse update manager for details.

7.3. Using the XInclude ant task

From your Eclipse installation take the "org.eclipse.wst.xsl.core.jar" and add this jar to your ant classpath. Put the new jar into your Ant classpath. See Apache Ant Tutorial - Classpath for details.

You should now be able to create and run the xinclude task. Belong an example ant build.xml file.

				
<?xml version="1.0"?>
<!--
  - Author:  Lars Vogel
  -->
<project name="docbook-src" default="usage">

	<description>
            This Ant build.xml file is used to transform DocBook XML to various
			output formats
    </description>

	<!--
      - Configure basic properties that will be used in the file.
      -->

	<property name="doc.dir" value="${basedir}/output" />
	<property name="src" value="${basedir}/src" />
	<property name="src.tmp" value="${basedir}/src.tmp" />
	<property name="lib" value="${basedir}/lib/" />
	<property name="docbook.xsl.dir" value="${basedir}/docbook-xsl-1.72.0" />
	
	<property name="html.stylesheet" value="${docbook.xsl.dir}/html/docbook.xsl" />
	<property name="xinclude.lib.dir" value="${basedir}/lib/" />


	<!--
      - target:  usage
      -->
	<target name="usage" description="Prints the Ant build.xml usage">
		<echo message="Use -projecthelp to get a list of the available targets." />
	</target>

	<!--
      - target:  clean
      -->
	<target name="clean" description="Cleans up generated files.">
		<delete dir="${doc.dir}" />
	</target>

	<!--
      - target:  depends
      -->
	<target name="depends">
		<mkdir dir="${doc.dir}" />
	</target>


	<!--
	- target: xinclude
	- description: Creates one combined temporary files for the different inputs files. 
	- The combined file will then be processed via different ant tasks
		-->
	<target name="xinclude">
		<xsl.xinclude in="${src}/DocBook/article.xml" out="${src.tmp}/DocBook/article.xml" />
	</target>
	
	
	<!--
      - target:  build-html
      - description:  Iterates through a directory and transforms
      -     .xml files into .html files using the DocBook XSL.
      -->
	<target name="build-html" depends="depends, xinclude" description="Generates HTML files from DocBook XML">
		<xslt style="${html.stylesheet}" extension=".html" basedir="${src.tmp}" destdir="${doc.dir}">
			<include name="**/*book.xml" />
			<include name="**/*article.xml" />
			<param name="html.stylesheet" expression="styles.css" />
		</xslt>
		<!-- Copy the stylesheet to the same directory as the HTML files -->
		<copy todir="${doc.dir}">
			<fileset dir="lib">
				<include name="styles.css" />
			</fileset>
		</copy>
	</target>

</project>