XML technologies / DocBook / Customizing DocBook / Adding elements / Inserting a block element

(Excerpt from "DocBook-XML: Media-neutral and platform-independent publishing" by Thomas Schraitle)

The aim of this section is to add a block element for marking up exercises. The name chosen is exercise with the following content model:

exercise ::= title, question, answer

Each exercise therefore contains a title (title), a question (question), and a corresponding answer (answer). Fortunately, all three elements already exist in DocBook, so you only need to refer to their definitions.

Customizing DocBook 4

For DocBook 4, proceed as follows:

Procedure: Adding exercise as a block element for DocBook 4

  1. Find the appropriate parameter entity for block elements
    First determine which category your new element belongs to. This is the most difficult part. Your element exercise fits well alongside equations, examples, figures, and so on. These are all grouped in the parameter entity %formal.class; (file dbpoolx.mod). Corresponding customizations are made in %local.formal.class;. Therefore, use the structure of the elements in this class as a guide.

  2. Customizing %local.formal.class;
    Create a new file and add your element exercise to the parameter entity %local.formal.class;:

<!ENTITY % local.formal.class   "|exercise"> 

  1. Loading the DocBook DTD
    Insert the following lines in your customization file to load the DocBook DTD:

<!ENTITY % DocBookDTD PUBLIC 
   "-//OASIS//DTD DocBook XML V4.5//EN" 
   "http://www.docbook.org/xml/4.5/docbookx.dtd"> 
%DocBookDTD;

  1. Defining exercise
    Finally, insert your new element after the preceding lines. Use a structure similar to that of example to simplify later customizations to your file:

<!ENTITY % exercise.module "INCLUDE"> 
<![%exercise.module;[ 
  <!ENTITY % local.exercise.attrib ""> 
  <!ENTITY % exercise.role.attrib "%role.attrib;"> 
  <!ENTITY % exercise.element "INCLUDE"> 
  <![%exercise.element;[ 
   <!ELEMENT exercise ((%formalobject.title.content;), 
                       question, answer)> 
  <!--end of exercise.element-->]]> 
  <!ENTITY % exercise.attlist "INCLUDE"> 
   <![%exercise.attlist;[ 
   <!ATTLIST exercise 
        %common.attrib; 
        %label.attrib; 
        %exercise.role.attrib; 
        %local.exercise.attrib;> 
  <!--end of exercise.attlist-->]]> 
<!--end of exercise.module-->]]> 

The parameter entities %exercise.module;, %exercise.element; and %exercise.attlist; let you subsequently disable and customize the entire definition of exercise, just the element, or just its attributes. The title was taken from the parameter entity %formalobject.title.content;.

  1. Using the customization file
    Use the customization file instead of DocBook’s original file (see Validating with a DocBook 4 customization file).

Customizing DocBook 5

For DocBook 5, proceed as follows:

Procedure: Adding exercise as a block element for DocBook 5

  1. Finding the appropriate definition pattern
    Finding a suitable definition pattern is the most difficult part of customization (see the table Naming conventions for DocBook 5). Check which of the many patterns of the following type best suits your element: db.*.blocks. Depending on your intention, several options may be possible. For exercise, for example, there are db.formal.blocks, db.para.blocks or db.technical.blocks. This example assumes the pattern db.formal.blocks.

  2. Creating the customization file
    Create a customization file with the following contents:

<grammar ns="http://docbook.org/ns/docbook" xmlns:db="http://docbook.org/ns/docbook" xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0" xmlns="http://relaxng.org/ns/structure/1.0"> 
    <include href="docbook.rng"/> 
    <define name="db.formal.blocks" combine="choice"> 
        <ref name="db.exercise"/> 
    </define> 
    <!-- Definieren des exercise Elements: --> 
</grammar>

default namespace db = "http://docbook.org/ns/docbook" 
include "docbook.rnc" 
db.formal.blocks |= db.exercise 
# Definieren von exercis

  1. Defining exercise
    Finally, insert your new element after the preceding comment.

<define name="db.exercise.role.attribute"> 
    <attribute name="role"/> 
</define> 
<define name="db.exercise.attlist"> 
    <interleave> 
        <optional> 
            <ref name="db.exercise.role.attribute"/> 
        </optional> 
        <ref name="db.common.attributes"/> 
        <ref name="db.common.linking.attributes"/> 
    </interleave> 
</define> 
<define name="db.exercise.info"> 
    <ref name="db._info.title.onlyreq"/> 
</define> 
<define name="db.exercise"> 
    <!-- Schematron-Regeln ausgeblendet --> 
    <element name="exercise"> 
        <ref name="db.exercise.attlist"> 
            <a:documentation>An exercise</a:documentation> 
        </ref> 
        <ref name="db.exercise.info"/> 
        <oneOrMore> 
            <ref name="db.para.blocks"/> 
        </oneOrMore> 
        <ref name="db.answer"/> 
    </element> 
</define>

db.exercise.role.attribute = attribute role { text } 
db.exercise.attlist = 
  db.exercise.role.attribute? 
  & db.common.attributes 
  & db.common.linking.attributes 
db.exercise.info = db._info.title.onlyreq 
db.exercise = 
  # Schematron-Regeln ausgeblendet 
element exercise { 
   ## An exercise 
    db.exercise.attlist, 
    db.exercise.info, 
    db.question, 
    db.answer 
}

  1. Using the customization file
    Use the customization file instead of DocBook’s original file (see Validating with a DocBook 5 customization file).

  

back to top