XSLT Style Design Template - Separate General and Special Templates in Different Style Sheets

I was wondering if there is a good approach in the XSLT style design template to separate the general and specific presentation of the data.

I tried, but was very embarrassed and lost. I would appreciate any tips, hints, tips where I can read about how best to separate XSLT stylesheets. And also, it would be very helpful to help with the example below, since it does not work = / THANKS!

I need to create a variety of HTML documents with different views that will reuse some data. For example, document date, signature details (name, position), etc. In addition, I use quite a few global variables (since XML is not structured and data is reused in all documents).

What I tried to do is move all the templates that will create a common HTML structure in one stylesheet, and then all the specific bits will be in their own stylesheet.

Something like the following:

General Temple Style Sheets "commonTemplates.xsl"

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- Variables Local --> ... <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" /> <xsl:template match="/Bookings"> <html> <head> <!-- populated by a template in a specific stylesheet --> <title><xsl:call-template name="docTitle"/></title> </head> <body> <xsl:apply-templates /> </body> </html> </xsl:template> <!-- general template for date --> <xsl:template match="/Bookings/Booking" name="docDate"> <p class="date"><xsl:value-of select="./@Date"/></p> </xsl:template> <!-- general template for signature --> <xsl:template match="/Bookings/Booking/Users" name="signature"> <xsl:param name="signatureLine" select="'Yours sincerely,'"/> <div id="signature"> <p><xsl:value-of select="$signatureLine"/></p> <p class="details"> <!-- populated by a template in a specific stylesheet --> <xsl:apply-templates select="." mode="signature"/> </p> </div> </xsl:template> <!-- dummy templates signatures otherwise it complains that there is no such template --> <xsl:template name="docTitle"/> </xsl:stylesheet> 

A specific table of styles of temples:

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- Imports --> <xsl:import href="commonTemplates.xsl"/> <!-- BODY CONTENT OF HTML PAGE --> <xsl:template match="/Bookings/Booking"> <xsl:call-template name="docDate"/> <!-- document content --> <div> <xsl:call-template name="content" /> </div> </xsl:template> <xsl:template name="docTitle"> <xsl:text>Here is the document title</xsl:text> </xsl:template> <!-- some content at the end of which signature should be inserted --> <xsl:template name="content"> <p>SOME CONTENT</p> <xsl:apply-templates /> </xsl:template> <!-- specific rule to insert appropriate data for signature --> <xsl:template match="/Bookings/Booking/Users" mode="signature"> <span class="name"><xsl:value-of select="./@Name"/></span> <span class="job"><xsl:value-of select="./@Title"/></span> </xsl:template> </xsl:stylesheet> 

Unfortunately, the signature template does not work, and I cannot understand why :( It does this for docTitle.

HTML results are as follows:

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body> <p class="date">16 February 2010</p> <div id="secondDeposit"> <p>SOME CONTENT</p> <!-- here I get lots of empty space --> </div> </body> 

I was wondering if it is possible to realize this idea as a whole and how to do it correctly, it is obvious that my work does not work.

Also, which approach would be better in this case: include or import a stylesheet? I think that with one of them I do not need to list all the variables again.

I would be grateful for any help! Sorry for the long post, and if it is not so clear.

Thanks!

+4
source share
2 answers

I am using the following method:

Template File (/templates/Main.xslt)

 <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" encoding="utf-8" /> <xsl:include href="../functions/General.xslt"/> <xsl:template match="/"> <html xml:lang="en"> <head> </head> <body> Template Content <xsl:call-template name="PageContent" /> </body> </html> </xsl:template> </xsl:stylesheet> 

Function File (/functions/General.xslt)

 <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- Generic functions used in the site go in here --> </xsl:stylesheet> 

Page File (/default.xslt)

Some of them will define different layouts in the template. This is the XSLT file that you invoke when you perform your transformation.

 <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:import href="templates/Main.xslt" /> <xsl:output method="xml" encoding="utf-8" /> <xsl:template match="/"> <xsl:apply-imports /> </xsl:template> <!-- CONTENT --> <xsl:template name="PageContent"> <!-- Put your specific page stuff here --> </xsl:template> </xsl:stylesheet> 
+3
source

It is recommended that you use the most commonly used templates in style files that will be imported by specific XSLT applications. Collections of such useful xstylesheet files make libraries useful.

Of course, an imported style sheet module often imports other style sheet modules.

An example is the FXSL library for functional programming in XSLT. See how XSLT applications (sample testxxx.xsl files) import the functions they need.

+2
source

Source: https://habr.com/ru/post/1301605/


All Articles