Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

3. Convert Docbook to HTML

3.1.  Project Setup

In Eclipse create a new project "de.vogella.docbook.first", select File -> New -> Project and select from the proposed list General -> Projects.

Create the following folder structure:

  • output: Contains the result of the conversion

  • docbook-xml-4.5: Contains the DTD definition for DocBook

  • docbook-xsl: Contains the XSL stylesheets to convert to the other output formats.

  • lib: Will contains your libraries (for pdf creation)

  • documents: Contains your DocBook files

Place the DocBook DTD and the XSLT stylesheets into the corresponding directories.

Create the folder "/lib/xalan" and copy the xalan jars into this folder. The result should look like the following.

3.2. Write your first DocBook document

In your folder "documents" create a following file "book.xml". The "../docbook-xml-4.5/docbookx.dtd" corresponds to the directory you have created earlier in your project setup.

				
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "../docbook-xml-4.5/docbookx.dtd">
<article>
	<articleinfo>
		<title>DocBook Intro</title>
		<author>
			<firstname>Lars</firstname>
			<surname>Vogel</surname>
		</author>
	</articleinfo>
	<sect1 label="1.0">
		<title>An introduction to DocBook</title>
		<para> 
			This is text.
		</para>
	</sect1>
</article>

			

3.3. Use ant to convert DocBook to html

We will now create an Apache Ant . Create the following file "build.xml" in your project directory.

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

	<description>
            This Ant buildhtml.xml file is used to transform DocBook XML to html output
    </description>

	<!--
      - Configure basic properties that will be used in the file.
      -->
	<property name="docbook.xsl.dir" value="docbook-xsl" />
	<property name="doc.dir" value="output" />
	<property name="documents" value="documents" />
	<property name="html.stylesheet" value="${docbook.xsl.dir}/html/docbook.xsl" />

	<!-- Making xalan available -->
	<path id="xalan.class.path">
		<pathelement location="lib/xalan/serializer.jar" />
		<pathelement location="lib/xalan/xalan.jar" />
		<pathelement location="lib/xalan/xercesImpl.jar" />
		<pathelement location="lib/xalan/xml-apis.jar" />
	</path>


	<!--
      - 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:  build-html
      - description:  Iterates through a directory and transforms
      -     .xml files into .html files using the DocBook XSL.
      -->
	<target name="build-html" depends="depends" description="Generates HTML files from DocBook XML">
		<xslt style="${html.stylesheet}" extension=".html" basedir="${documents}" destdir="${doc.dir}">
			<include name="**/*book.xml" />
			<include name="**/*article.xml" />
			<param name="html.stylesheet" expression="style.css" />
			<classpath refid="xalan.class.path" />
		</xslt>
		<!-- Copy the stylesheet to the same directory as the HTML files -->
		<copy todir="${doc.dir}">
			<fileset dir="lib">
				<include name="style.css" />
			</fileset>
		</copy>


	</target>

</project>

		

Run the build.xml file (right mouse click, run as -> Ant Build). Then check the output directory. You should find a directory "Example", with the file "book.html".

Congratulations you created your first Docbook and converted it into an HTLM document.