The general case

In the general case, customizing the semantic XML files generated by w2x requires writing both a XED script and an XSLT stylesheet.

For example, let’s suppose we want to group all the paragraphs having a “Note” paragraph style and to generate for such groups DocBook and DITA note elements.

The following blocks.convert parameter would allow to very easily create the desired groups:

-p edit.blocks.convert "p-Note p g:id='note_group_member'¬
 g:container='div class=\”role-note\” ’"

However this would leave us with two unsolved problems:

  1. A paragraph having a “Note” paragraph style often starts with bold text “Note:”. We want to eliminate this redundant label.
  2. The stock XSLT stylesheets will not convert XHTML element <div class=”role-note”> to a DocBook or DITA note element.

A custom XED script

The first problem is solved by the following w2x_install_dir/doc/manual/customize/notes.xed script:

namespace "http://www.w3.org/1999/xhtml";
namespace html = "http://www.w3.org/1999/xhtml";
namespace g = "urn:x-mlmind:namespace:group";

for-each /html/body//p[get-class("p-Note")] {
    delete-text("note:\s*", "i");
    if content-type() <= 1 and not(@id) {
        delete();
    } else {
        remove-class("p-Note");
        set-attribute("g:id", "note_group_member");
        set-attribute("g:container", "div class='role-note'");
    }
}

group();

The “Note:” label, if any, is deleted using XED command delete-text. If doing this creates a useless empty (content-type() <= 1) paragraph, then delete this paragraph using XED command delete.

The above script is executed after stock script w2x_install_dir/xed/blocks.xed by the means of the following w2x command-line option:

-pu edit.after.blocks customize\notes.xed

A custom XSLT stylesheet

The second problem is solved by the following w2x_install_dir/doc/manual/customize/custom_topic.xslt XSLT 1.0 stylesheet:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:h="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="h">

<xsl:import href="w2x:xslt/topic.xslt"/>

<xsl:template match="h:div[@class = 'role-note']">
  <note>
    <xsl:call-template name="processCommonAttributes"/>
    <xsl:apply-templates/>
  </note>
</xsl:template>
...
</xsl:stylesheet>

This stylesheet, which imports stock w2x_install_dir/xslt/topic.xslt, is used for the topic, map and bookmap output formats (see –o option). Similar, very simple, stylesheets have been developed for the docbook and docbook5 output formats.

Something like “w2x:xslt/topic.xslt” is an absolute URL supported by w2x. “w2x:” is an URL prefix (defined in the automatic XML catalog used by w2x) which specifies the location of the parent directory of both the xed/ and xslt/ subdirectories.

The above stylesheet replaces the stock one by the means of the following w2x command-line option:

-o topic -t customize\custom_topic.xslt

Do not forget to specify the –t option after the –o option, because it’s the –o option which implicitly invokes stock w2x_install_dir/xslt/topic.xslt (this has been explained in chapter Going further with w2x) and we want to use –t to override the use of the stock XSLT stylesheet.

Tip: You’ll find a template for custom XED scripts and several templates for custom XSLT stylesheets in w2x_install_dir/doc/manual/templates/.

For example, in order to create w2x_install_dir/doc/manual/customize/custom_topic.xslt, we started by copying template XSLT stylesheet w2x_install_dir/doc/manual/templates/template_topic.xslt.