2

Writing some stylesheets for DocBook.

Edit: per the comment below: Yes, this means I am writing customizations based on the DocBook-XSL stylesheets, not rewriting existing DocBook stylesheets.

Is it possible, via an XSL stylesheet, to set a default size/scale for imagedata elements, particularly in print output?

Essentially I want to set things up such that if I include scalefit, width and/or contentdepth attributes in an imagedata element, those attributes will be used; however if they aren't included, they will default to scalefit="1" width="100%" contentdepth="100%".

Seems straightforward enough, but I'm an XSLT newbie, and googling has gotten me nowhere. Is this possible? How?

Thanks!

DanM
  • 7,037
  • 11
  • 51
  • 86
  • When you say "Writing some stylesheets for DocBook", do you in fact mean that you are writing customizations based on the [DocBook-XSL stylesheets](https://sourceforge.net/projects/docbook/)? – mzjn Dec 21 '11 at 17:50

1 Answers1

3

Essentially I want to set things up such that if I include scalefit, width and/or contentdepth attributes in an imagedata element, those attributes will be used; however if they aren't included, they will default to scalefit="1" width="100%" contentdepth="100%".

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="imagedata[not(@scalefit|@width|@contentdepth)]">
  <imagedata scalefit="1" width="100%" contentdepth="100%">
   <xsl:copy-of select="@*"/>

   <xsl:apply-templates/>
  </imagedata>
 </xsl:template>
</xsl:stylesheet>

when applied on the following XML document:

<t>
 <imagedata scalefit="1" width="80%" contentdepth="90%"/>
 <imagedata/>
</t>

produces the wanted, correct result:

<t>
   <imagedata scalefit="1" width="80%" contentdepth="90%"/>
   <imagedata scalefit="1" width="100%" contentdepth="100%"/>
</t>

Explanation: Overriding the identity rule for any imagedata that has none of the scalefit, width and contentdepth attributes.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • Thanks for the quick answer... However I am getting the following error when I add those templates to my stylesheet: net.sf.saxon.trans.XPathException: org.apache.fop.fo.ValidationException: "fo:root" is missing child elements. Required content model: (layout-master-set, declarations?, bookmark-tree?, (page-sequence|fox:external-document)+) ...any idea what that means? – DanM Dec 21 '11 at 16:51
  • @DanM: I don't know anything about XSL-FO. In this answer I provided a solution which works with any XML document -- you have to adapt it to *your* XML document, which I believe would be an instance of whatever XSL-FO might be -- this means that you have to satisfy any syntactic and semantic requirements of XSL-FO in your XSL-FO program -- and these aren't related to your question at all, which is: how to add default attributes to an element that doesn't have these attributes -- and which has been fully answered. – Dimitre Novatchev Dec 21 '11 at 17:04