Note that there are some explanatory texts on larger screens.

plurals
  1. POXSLT stylesheets design pattern - separate common and specific templates in different stylesheets
    primarykey
    data
    text
    <p>I was wondering if there is a good approach in XSLT stylesheet design pattern to separate common and specific data representation.</p> <p>I was trying, but got very confused and lost. I would appreciate any advice, tips, hints where I can read up on how to better separate XSLT stylesheets. And also, would really appreciate help with the example below as it doesn't work =/ THANK YOU!</p> <p>I need to create a variety of HTML docs with different looks that would reuse some of the data. For example date of a document, signature details (name, job title) and so on. Also, I'm using quite a lot of global variables (because XML isn't structured well and data reused throughout a doc).</p> <p>What I was trying to do, is to move all the templates that would create a common HTML structure in one stylesheet and then all specific bits will be in their own stylesheet.</p> <p>Something like the following:</p> <p>Common templetes stylesheet "commonTemplates.xsl"</p> <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt; &lt;!-- Variables Local --&gt; ... &lt;xsl:output method="html" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" doctype-system="http://www.w3.org/TR/html4/loose.dtd" indent="yes" /&gt; &lt;xsl:template match="/Bookings"&gt; &lt;html&gt; &lt;head&gt; &lt;!-- populated by a template in a specific stylesheet --&gt; &lt;title&gt;&lt;xsl:call-template name="docTitle"/&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;xsl:apply-templates /&gt; &lt;/body&gt; &lt;/html&gt; &lt;/xsl:template&gt; &lt;!-- general template for date --&gt; &lt;xsl:template match="/Bookings/Booking" name="docDate"&gt; &lt;p class="date"&gt;&lt;xsl:value-of select="./@Date"/&gt;&lt;/p&gt; &lt;/xsl:template&gt; &lt;!-- general template for signature --&gt; &lt;xsl:template match="/Bookings/Booking/Users" name="signature"&gt; &lt;xsl:param name="signatureLine" select="'Yours sincerely,'"/&gt; &lt;div id="signature"&gt; &lt;p&gt;&lt;xsl:value-of select="$signatureLine"/&gt;&lt;/p&gt; &lt;p class="details"&gt; &lt;!-- populated by a template in a specific stylesheet --&gt; &lt;xsl:apply-templates select="." mode="signature"/&gt; &lt;/p&gt; &lt;/div&gt; &lt;/xsl:template&gt; &lt;!-- dummy templates signatures otherwise it complains that there is no such template --&gt; &lt;xsl:template name="docTitle"/&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>Specific templetes stylesheet:</p> <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt; &lt;!-- Imports --&gt; &lt;xsl:import href="commonTemplates.xsl"/&gt; &lt;!-- BODY CONTENT OF HTML PAGE --&gt; &lt;xsl:template match="/Bookings/Booking"&gt; &lt;xsl:call-template name="docDate"/&gt; &lt;!-- document's content --&gt; &lt;div&gt; &lt;xsl:call-template name="content" /&gt; &lt;/div&gt; &lt;/xsl:template&gt; &lt;xsl:template name="docTitle"&gt; &lt;xsl:text&gt;Here is the document title&lt;/xsl:text&gt; &lt;/xsl:template&gt; &lt;!-- some content at the end of which signature should be inserted --&gt; &lt;xsl:template name="content"&gt; &lt;p&gt;SOME CONTENT&lt;/p&gt; &lt;xsl:apply-templates /&gt; &lt;/xsl:template&gt; &lt;!-- specific rule to insert appropriate data for signature --&gt; &lt;xsl:template match="/Bookings/Booking/Users" mode="signature"&gt; &lt;span class="name"&gt;&lt;xsl:value-of select="./@Name"/&gt;&lt;/span&gt; &lt;span class="job"&gt;&lt;xsl:value-of select="./@Title"/&gt;&lt;/span&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>Unfortunately, the template for signature doesn't work and I can't figure out why :( It does for the docTitle though.</p> <p>The results HTML looks like this:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;p class="date"&gt;16 February 2010&lt;/p&gt; &lt;div id="secondDeposit"&gt; &lt;p&gt;SOME CONTENT&lt;/p&gt; &lt;!-- here I get lots of empty space --&gt; &lt;/div&gt; &lt;/body&gt; </code></pre> <p></p> <p>I was wondering if such idea could be implemented in general and how to do it properly, obviously mine doesn't work. </p> <p>Also, which approach will be better in this case: include or import stylesheet? I think with one of them I don't need to list all the variables again.</p> <p>I would appreciate any help! Sorry for a long post and if it's not as clear.</p> <p>Thank you!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload