Daisy Wiki Document Type Specific Styling
Introduction
This document describes how to create custom XSLT stylesheets to render documents differently depending on their document type.
The Input XML
The input of the stylesheets is an XML document which has a structure as shown below. This is not an extensive schema containing every other element and attribute, but those that you'll need most often.
<document>
<user ... />
<d:document xmlns:d="http://outerx.org/daisy/1.0"
id="..."
name="..."
[... various other attributes ...] >
<d:fields>
<d:field typeId="..." name="..." label="..." valueFormatted="..."
[... other attributes and children ...]>
... more fields ...
</d:fields>
<d:parts>
<d:part typeId="..." mimeType="..." size="..." label="..." daisyHtml="true/false">
[... HTML content of the part including html/body if @daisyHtml=true ...]
</d:part>
... more parts ...
</d:parts>
<d:links>
<d:link title="..." target="..."/>
... more links ...
</d:links>
[... customFields, lockInfo, collectionIds ...]
</d:document>
</document>
Note that the actual Daisy document (d:document) is wrapped in a <document> element, which allows other contextual information to be passed along, currently only information about the user is included here. This allows to change the styling depending on for example the active roles of the user. The format of the user information is the same as in the input of the layout.xsl.
Expected stylesheet output
The output of the XSLT should be an embeddable chunk of HTML (or XSL-FO in the case of PDF). Thus no <html> and <body> elements, but something which can be inserted inside <body> (or inside a <div>, a <td >, etc). Where the produced output will end up depends on the stylesheet creating the general page layout, or in the case of included documents, the location of the inclusion.
Where the stylesheets should be put
The stylesheets should be placed inside the Daisy webapp, in the following directory:
daisy/resources/document-styling/<skin-name>/<format>
In which <skin-name> is the name of the skin you're using (by default: "default"), and <format> either "html" or "xslfo". Thus for the default skin for a html, this becomes:
daisy/resources/document-styling/default/html
The stylesheet should be named:
<document-type-name>.xsl
It is recommended to keep the stylesheet somewhere in a directory outside of the webapp, and let a script copy them over to there.
Example 1: styling fields in a custom way
Suppose we have a document type called "TestDocType" with a " SimpleDocumentContent" part , and two fields called "field1" and "field2". The default layout will first place the parts, then the fields (in a table), and then the out-of-line links (if any).
The stylesheet below shows how to put the fields at the top of the document:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:d="http://outerx.org/daisy/1.0">
<xsl:import href="daisyskin:xslt/document-to-html.xsl"/>
<xsl:template match="d:document">
<h1 class="daisy-document-name"><xsl:value-of select="@name"/></h1>
<p>
Hi there! Here's the value of field1:
<xsl:value-of select="d:fields/d:field[@name='field1']/@valueFormatted"/>
and field 2:
<xsl:value-of select="d:fields/d:field[@name='field2']/@valueFormatted"/>
</p>
<xsl:apply-templates select="d:parts/d:part"/>
<xsl:apply-templates select="d:links"/>
<!-- xsl:apply-templates select="d:fields"/ -->
</xsl:template>
</xsl:stylesheet>
To minize our efforts, we import the default stylesheet and only redefine what is needed. For comparison, the default template for d:document looks as follows:
<xsl:template match="d:document"> <h1 class="daisy-document-name"><xsl:value-of select="@name"/></h1> <xsl:apply-templates select="d:parts/d:part"/> <xsl:apply-templates select="d:links"/> <xsl:apply-templates select="d:fields"/> </xsl:template>
This new stylesheet must be saved as:
daisy/resources/document-styling/default/html/TestDocType.xsl
Now surf to a document based on TestDocType, and you should see the result.
Example 2: styling parts in a custom way
In this example, suppose we have a document type called "Article" with parts "Abstract" and "Body". We would like to render the abstract in a box. The below stylesheet shows how this can be done.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:d="http://outerx.org/daisy/1.0">
<xsl:import href="daisyskin:xslt/document-to-html.xsl"/>
<xsl:template match="d:document">
<h1 class="daisy-document-name"><xsl:value-of select="@name"/></h1>
<div style="margin: 20px; padding: 10px; border: 1px solid black; background-color: #ffd76c">
<xsl:apply-templates select="d:parts/d:part[@name='Abstract']"/>
</div>
<xsl:apply-templates select="d:parts/d:part[@name='Body']"/>
<xsl:apply-templates select="d:links"/>
<xsl:apply-templates select="d:fields"/>
</xsl:template>
</xsl:stylesheet>



There are no comments.