Plug-in coding conventions Coding conventions To ensure custom plug-ins work well with the core toolkit code and remain compatible with future releases, the DITA Open Toolkit project recommends that plug-ins use modern development practices and common coding patterns. plug-ins best practices upgrading best practices XSLT best practices preprocessing XSLT
Best practices

Adhering to certain development practices will properly isolate your code from that of DITA Open Toolkit. This will make it easier to you to upgrade to new versions of DITA-OT when they are released.

  • Use a properly-constructed DITA-OT plug-in.

  • Use a version control system to store your code.

  • Store the source code of your plug-ins outside of the DITA-OT installation directory, and add the repository location to the list of plug-in directories defined in the plugindirs entry of the configuration.properties file.

  • Never modify any of the core DITA-OT code.

    You may want to set the permissions on default plug-in directories such as org.dita.pdf2 to “read-only” to ensure that you do not accidentally modify the files within as you develop your customized plug-in.
  • Avoid copying entire DITA-OT files into your customization plug-in. When you only copy the attribute sets and templates that you need to override, there is less risk of impact from new features or fixes in the base code, making your code more stable and easier to upgrade between releases.

  • If you only need to change a few attribute sets and templates, you may prefer to store your overrides in custom.xsl files, or a simple folder hierarchy within your custom plug-in.

  • In cases that require substantial customizations, you may prefer to organize the files in a folder structure that mimics the hierarchy of the default plug-in you are customizing. This facilitates comparisons with the default settings in the base plug-in and makes it easier to migrate your changes to new toolkit versions. See for information on the files in the base PDF plug-in.

  • Upgrade your customization plug-in to new versions of DITA-OT regularly. Do not wait through several major releases before upgrading.

Use a custom namespace

For XSLT customizations, use a custom namespace for any modified template modes, template names, attribute sets, functions, and variables. This helps to clarify which portions of the code are specific to your customizations, and serves to isolate your changes in the event that items with the same name are added to the base plug-ins in the future.

For example, instead of creating a template named searchbar, use something like corp:searchbar instead. This ensures that if future versions of DITA-OT add a searchbar template, your custom version will be unaffected.

Instead of: <xsl:template name="searchbar"/>

use: <xsl:template name="corp:searchbar"/>

Use custom <xmlelement>pipeline</xmlelement> elements Ant pipeline Ant xslt Ant style

In Ant scripts, use the XSLT module from DITA-OT instead of Ant’s built-in xslt or style tasks.

The XSLT module allows access to DITA-OT features like using the job configuration to select files in the temporary folder based on file metadata and custom XSLT extension functions.

Instead of:

<xslt style="${dita.plugin.example.dir}/custom.xsl" basedir="${dita.temp.dir}" destdir="${dita.output.dir}" includesfile="${dita.temp.dir}/${fullditatopicfile}"/>

use:

<pipeline> <xslt style="${dita.plugin.example.dir}/custom.xsl" destdir="${dita.output.dir}"> <ditafileset format="dita" /> </xslt> </pipeline>

Use the plug-in directory property

In Ant scripts, always refer to files in other plug-ins using the dita.plugin.plugin-id.dir property.

Instead of:

<property name="base" location="../example/custom.xsl"/>

use:

<property name="base" location="${dita.plugin.example.dir}/custom.xsl"/>

This fixes cases where plug-ins are installed to custom plug-in directories or the plug-in folder name doesn’t match the plug-in ID.

For details, see .
Use the <codeph>plugin</codeph> URI scheme Ant xsl:import Ant xsl:include

In XSLT, use the plugin URI scheme in xsl:import and xsl:include to reference files in other plug-ins.

Instead of:

<xsl:import href="../../org.dita.base/xsl/common/output-message.xsl"/>

use:

<xsl:import href="plugin:org.dita.base:xsl/common/output-message.xsl"/>

As with the plug-in directory property in Ant, this allows plug-ins to resolve to the correct directory even when a plug-in moves to a new location. The plug-in is referenced using the syntax plugin:plugin-id:path/within/plugin/file.xsl.

For details, see .
Use <xmlelement>ditafileset</xmlelement> to select files Ant ditafileset ditafileset images selecting images copy-files

In Ant scripts, use ditafileset to select resources in the temporary directory.

For example, to select all images referenced by input DITA files, instead of:

<copy todir="${copy-image.todir}"> <fileset dir="${user.input.dir}"> <includes name="*.jpg"/> <includes name="*.jpeg"/> <includes name="*.png"/> <includes name="*.gif"/> <includes name="*.svg"/> </fileset> </copy>

use:

<copy todir="${copy-image.todir}"> <ditafileset format="image" /> </copy>

The ditafileset resource collection can be used to select different types of files.

Usage examples of <xmlelement>ditafileset</xmlelement> Example Description <ditafileset format="dita"/> Selects all DITA topics in the temporary directory. <ditafileset format="ditamap"/> Selects all DITA maps in the temporary directory. <ditafileset format="image"/> Selects images of all known types in the temporary directory.