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, answerEach 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
Find the appropriate parameter entity for block elements
First determine which category your new element belongs to. This is the most difficult part. Your elementexercisefits well alongside equations, examples, figures, and so on. These are all grouped in the parameter entity%formal.class;(filedbpoolx.mod). Corresponding customizations are made in%local.formal.class;. Therefore, use the structure of the elements in this class as a guide.Customizing
%local.formal.class;
Create a new file and add your elementexerciseto the parameter entity%local.formal.class;:
<!ENTITY % local.formal.class "|exercise"> 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;Defining
exercise
Finally, insert your new element after the preceding lines. Use a structure similar to that ofexampleto 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 ofexercise, just the element, or just its attributes. The title was taken from the parameter entity%formalobject.title.content;.
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
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. Forexercise, for example, there aredb.formal.blocks,db.para.blocksordb.technical.blocks. This example assumes the patterndb.formal.blocks.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 exercisDefining
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
}Using the customization file
Use the customization file instead of DocBook’s original file (see Validating with a DocBook 5 customization file).
Copyright © 2009 Millin Verlag
You may print the online version for your private use.
Otherwise, this chapter from "DocBook-XML: Medienneutrales und plattformunabhängiges Publizieren" is subject to the same provisions as the printed edition: the work, including all its parts, is protected by copyright. All rights are reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Millin Verlag, Siebengebirgsring 36, 53797 Lohmar, info(at)millin.de