Hiding the answers

If you are using a qandaset for student exercises, then you may want to produce two versions of your document: one with answers for the teacher, and one without answers for the students. The general process of producing a different document for different audiences is called profiling in DocBook. It is described in Chapter 26, Profiling (conditional text).

Normally in profiling you add profiling attributes to DocBook elements you want to make conditional in your document. Then you process the document with the profiling stylesheet, while setting parameters that identify which profiling values to accept and which to reject. For example, you might put a userlevel="teacher" attribute on each answer element in your document. To generate the output version with the answers, you process your document with the parameter profile.userlevel set to teacher. Since that value matches the attribute value, the answers are included in the output. When you process the document with the parameter profile.userlevel set to student, the value does not match and the answers are left out.

This process is a bit cumbersome if you have many questions, because you have to add the userlevel="teacher" attribute to every answer element. You also have to use the profiling stylesheet. You can avoid both problems by using a stylesheet customization instead.

This stylesheet customization will turn off all the answers when the parameter profile.userlevel is set to student, without having to put a profiling attribute on every answer, and without having to use the profiling stylesheet.

<xsl:template match="answer">
    <xsl:if test="$profile.userlevel = 'teacher'">

Since this template is in your customization layer, it has a higher import precedence than the stock template that matches on answer. When this template is applied, it first tests to see if the parameter profile.userlevel is set to teacher. If so, then it calls xsl:apply-imports, which applies the original match="answer" template to generate the answer in the output. If the parameter is any other value, then this template does nothing, so the answer is not output.