Stylesheet's selection process

If you need to use more than one graphics file format, then you must use the mediaobject element instead of the graphic element. You could use profiling to select from among several graphic elements, but mediaobject is designed to do it without the need for the profiling step.

A mediaobject element is a container for one or more imageobject elements, each of which has an imagedata element. Usually, the various images in a mediaobject are different formats of the same illustration, with the idea that only one of them at a time will be used by the stylesheet. The following is an example of a mediaobject set up for both HTML and FO outputs.

Example 18.1. Multiple graphics in a mediaobject

<mediaobject  id="MousePicture">  1  
  <imageobject  role="html">  2
    <imagedata  format="PNG"  fileref="mouse.png"/>  3
  <imageobject  role="fo">
    <imagedata  format="PDF"  fileref="mouse.pdf"/>

Note these features of this example:


The mediaobject element contains two graphical elements. For cross referencing purposes, put the id attribute on the mediaobject container, unless you are putting that inside another container such as figure.


The first imageobject element has a role="html" attribute that designates it for HTML output. The second imageobject has a role="fo" attribute that designates it for FO output.


Each imagedata element indicates its file name and format. You could also put sizing attributes there.

Select by role

The DocBook stylesheets can automatically select the right graphic if the imageobject elements have a role attribute of either html or fo. When you process this example with the html stylesheet, you get the PNG graphic, and when you process it with the FO stylesheet, you get the PDF graphic. Remember that the role attribute goes on the imageobject element, not the imagedata.

If you are using the xhtml stylesheet, then you can use role="xhtml" if you need a different format for that output. Otherwise the xhtml stylesheet falls back to the object with role="html". Other stylesheet customizations such as Website or your own will use the value for the stock stylesheet it is based upon.

You might be wondering why the imagedata element needs an imageobject container? Because imageobject can also contain an objectinfo element. That element can be used to track information about the image, such as the software that created it, the current revision, the author, etc.

The automatic selection behavior is controlled by the use.role.for.mediaobject parameter. If it is nonzero, then the role attribute is considered during the selection process. You can turn that behavior off if you set the parameter to zero.

If you want finer control, then you have the option to use any role values you want. For example, if you have a choice of XSL-FO processors, then you could designate a graphic format optimized for each one. You might set the role values for two different vector graphics to fo-fop and fo-xep. Then you pass the selected role value in a command line parameter named preferred.mediaobject.role. For example:

<mediaobject  id="MousePicture">
  <imageobject  role="html">
    <imagedata  format="PNG"  fileref="mouse.png"/>
  <imageobject  role="fo-fop">
    <imagedata  format="SVG"  fileref="mouse.svg"/>
  <imageobject  role="fo-xep">
    <imagedata  format="PDF"  fileref="mouse.pdf"/>

If you are processing with the FOP processor, then set the parameter preferred.mediaobject.role="fo-fop" on the command line. The DocBook stylesheet will use the SVG graphic reference into the FO output file. If you are using XEP, then set the parameter preferred.mediaobject.role="fo-xep" to use the PDF graphic instead.

Select by format

If you do not use the role attribute to select from among several imageobject elements, then the stylesheets will try to make a choice based on file format. The stylesheets contain several lists of file formats that are acceptable for each output type. For the various XSL-FO processors, it checks to see if any of the parameters fop.extensions, passivetex.extensions, arbortext.extensions, or xep.extensions is set.

The process of selection by format goes like this:

  1. It looks at the first imageobject inside the mediaobject.

  2. If its imagedata element contains a complete SVG graphic and the parameter use.svg is nonzero, then it accepts that object and does not consider any others.

  3. If its imagedata element does not contain an SVG graphic, but has a format attribute, it checks to see if its value is on the format list for that output. If so, then it accepts that object and does not consider any others.

  4. If it does not have a format attribute, it extracts the filename extension from the fileref attribute. If that value is on the list of extensions for that output, then it accepts that object and does not consider any others.

  5. If the fileref does not have an extension, it checks the graphic.default.extension parameter to see what extension would be added to such a file reference. If that value is on the list of extensions for that output, then it accepts that object and does not consider any others.

  6. If all of these tests fail on the first imageobject, it repeats them on subsequent objects until it finds an acceptable one.

This selection method is often sufficient, but is somewhat less precise than selecting by role. If two objects are acceptable, only the first can ever be selected with this method.