XSLT Template allowing separation of data from XSLT presentation
"James Lavery" <[email protected]> Mon, 3 Nov 2003 12:14:40 -0000
| Newsgroups | gmane.comp.krysalis.user |
|---|---|
| Message-ID | <[email protected]> |
Hi everyone,
First of all - congratulations and thanks to the Krysalis team for such
a good system - it's just what we needed for generating barcodes in our
PDF documents.
One problem I had with the sample data and stylesheets which came with
Krysalis was the fact that the barcode data (barcode.xml) had the
barcode format and definition in it. It seems to me that the barcode
formatting should be held in the XSL file, not the XML data.
To this end, I've put together an XSL template which takes the data to
be converted to a barcode, along with a variable containing the barcode
configuration. I think this is an improvement on the examples supplied
- and hope others find it useful!
I assume I can't send attachments to the list, so below my signature are
snips of the files concerned. Comments/criticisms/suggestions welcome.
James Lavery
XML DATA FILE:
====================
<?xml version="1.0" encoding="UTF-8"?>
<barcode>
<msg>00123456789
</msg>
</barcode>
====================
XSL Stylesheet:
======================
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:barcode="org.krysalis.barcode.xalan.BarcodeExt"
xmlns:common="http://exslt.org/common"
xmlns:xalan="http://xml.apache.org" exclude-result-prefixes="barcode
common xalan">
<xsl:output method="xml" version="1.0" omit-xml-declaration="no"
indent="yes"/>
<!--
========================================================================
===================================================== -->
<!-- set up variables holding configuration for the different
formats -->
<!-- code39 with default settings -->
<xsl:variable name="bc_code39">
<barcode>
<code39/>
</barcode>
</xsl:variable>
<!-- code39 with custom settings -->
<xsl:variable name="bc_code39_tall">
<barcode>
<code39>
<height>
20
</height>
</code39>
</barcode>
</xsl:variable>
<!-- code128 with default settings -->
<xsl:variable name="bc_code128">
<barcode>
<code128/>
</barcode>
</xsl:variable>
<!-- codabar with default settings -->
<xsl:variable name="bc_codabar">
<barcode>
<codabar/>
</barcode>
</xsl:variable>
<!--
========================================================================
===================================================== -->
<xsl:template match="barcode">
<fo:root font-family="sans-serif" font-size="10pt">
<fo:layout-master-set>
<fo:simple-page-master master-name="simpleA4"
page-height="29.7cm" page-width="21cm" margin-top="2cm"
margin-bottom="1cm" margin-left="2cm" margin-right="2cm">
<fo:region-body margin-bottom="1.1cm"/>
<fo:region-after extent="1cm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="simpleA4"
language="de_CH">
<fo:static-content flow-name="xsl-region-after">
<fo:block text-align="center"
font-style="italic">This PDF was generated using
Krysalis Barcode (
<fo:basic-link
external-destination="url(http://www.krysalis.org/barcode)">http://www.k
rysalis.org/barcode</fo:basic-link
<http://www.krysalis.org/barcode)">http://www.krysalis.org/barcode</fo:b
asic-link> >
) and Apache FOP (
<fo:basic-link
external-destination="url(http://xml.apache.org/fop)">http://xml.apache.
org/fop</fo:basic-link
<http://xml.apache.org/fop)">http://xml.apache.org/fop</fo:basic-link> >
)</fo:block>
</fo:static-content>
<fo:flow flow-name="xsl-region-body">
<fo:block break-before="page" font-weight="bold"
font-size="16pt" padding-top="1mm" padding-bottom="1mm"
background-color="blue" color="white" space-after="3mm"
text-align="center">
This stylesheet demonstrates using the
barcode_generate template to specify all barcode configuration in the
stylesheet itself.
</fo:block>
<fo:table table-layout="fixed" space-before="3mm">
<fo:table-column column-width="8.5cm"/>
<fo:table-column column-width="8.5cm"/>
<fo:table-body>
<!-- CODE39 -->
<fo:table-row>
<fo:table-cell padding="1mm"
border="solid" border-width="1px">
<fo:block>
Code 39 - default settings
</fo:block>
</fo:table-cell>
<fo:table-cell
background-color="lightgrey" padding="1mm" border="solid"
border-width="1px">
<xsl:call-template
name="barcode_generate">
<xsl:with-param name="config"
select="$bc_code39"/>
<xsl:with-param name="val"
select="normalize-space(string())"/>
</xsl:call-template>
</fo:table-cell>
</fo:table-row>
<!-- CODE39 - taller -->
<fo:table-row>
<fo:table-cell padding="1mm"
border="solid" border-width="1px">
<fo:block>
Code
39 - custom settings
</fo:block>
</fo:table-cell>
<fo:table-cell
background-color="lightgrey" padding="1mm" border="solid"
border-width="1px">
<xsl:call-template
name="barcode_generate">
<xsl:with-param name="config"
select="$bc_code39_tall"/>
<xsl:with-param name="val"
select="normalize-space(string())"/>
</xsl:call-template>
</fo:table-cell>
</fo:table-row>
<!-- CODE128 -->
<fo:table-row>
<fo:table-cell padding="1mm"
border="solid" border-width="1px">
<fo:block>
Code 128 - default settings
</fo:block>
</fo:table-cell>
<fo:table-cell
background-color="lightgrey" padding="1mm" border="solid"
border-width="1px">
<xsl:call-template
name="barcode_generate">
<xsl:with-param name="config"
select="$bc_code128"/>
<xsl:with-param name="val"
select="normalize-space(string())"/>
</xsl:call-template>
</fo:table-cell>
</fo:table-row>
<!-- codabar -->
<fo:table-row>
<fo:table-cell padding="1mm"
border="solid" border-width="1px">
<fo:block>
Codabar - default settings
</fo:block>
</fo:table-cell>
<fo:table-cell
background-color="lightgrey" padding="1mm" border="solid"
border-width="1px">
<xsl:call-template
name="barcode_generate">
<xsl:with-param name="config"
select="$bc_codabar"/>
<xsl:with-param name="val"
select="normalize-space(string())"/>
</xsl:call-template>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<!-- Below is the template. This takes a configuration variable and
value variable, to generate the barcode.
Note that is is up to the calling XSLT to make sure that the
data passed in as the $val parameter is in the correct format -
i.e. correct length and characters for the barcode being
generated.
-->
<xsl:template name="barcode_generate">
<xsl:param name="config"/>
<xsl:param name="val"/>
<fo:block>
<fo:instream-foreign-object>
<xsl:variable name="bc"
select="barcode:generate($config, $val)"/>
<svg:svg xmlns:svg="http://www.w3.org/2000/svg
<http://www.w3.org/2000/svg> ">
<xsl:attribute name="width">
<xsl:value-of select="$bc/svg:svg/@width"/>
</xsl:attribute>
<xsl:attribute name="height">
<xsl:value-of select="$bc/svg:svg/@height"/>
</xsl:attribute>
<svg:rect x="0mm" y="0mm" fill="white">
<xsl:attribute name="width">
<xsl:value-of select="$bc/svg:svg/@width"/>
</xsl:attribute>
<xsl:attribute name="height">
<xsl:value-of select="$bc/svg:svg/@height"/>
</xsl:attribute>
</svg:rect>
<xsl:copy-of select="$bc"/>
</svg:svg>
</fo:instream-foreign-object>
</fo:block>
</xsl:template>
</xsl:stylesheet>
==================