Newer
Older
Handbook / make-includes / xslt_functions.make
################################################################################
#
# File: $Id$
#
# Make functions for handling XSLT tasks.
#
################################################################################


################################################################################
#
# XSLT must be defined as an environment variable. It's an error not to do so.
#
XSLT?=$(error The required environment variable XSLT has not been defined. Please set it to either "saxon", "xalan-c" or "xalan-j", to indicate to your preferred XSLT processor)


################################################################################
#
# Resolve a URI for some XML entity such as a stylesheet into a path to
# the actual item. This works by calling the example resolver application
# that comes with the entity resolver, then extracting the full path from
# the result. If the URI cannot be resolved, the function returns the
# original filename so that the XSLT processor can fail with a meaningful
# error message.
# 
# This is a workaround for the mysterious breakage that occured on Nigel's
# machine in early 2006 where all of the XSLT processors suddenly stopped
# accepting resolver class names as command line arguments.
# 
# This approach, while apparently kludgey, does have a particular
# advantage, however, in that it can be used with non-Java based XSLT
# processors such as Xalan C++, whereas the previous approach only worked
# with Saxon and Xalan-J. In other words, this will work with ANY XSLT
# processor.
#
# Note that the function adds the "file:///" protocol to the front of the
# argument, so callers should NOT add this themselves!
#
# For reference, the typical output from the resolver application looks
# something like this for a successful resolution:
#
# % java org.apache.xml.resolver.apps.resolver -u file:///foo.xsl uri
# Resolve URI (uri):
#         uri: file:///foo.xsl
# Result: file:/path/to/item/foo.xsl
#
# and like this for a failure:
#
# % java org.apache.xml.resolver.apps.resolver -u file:///missing.xsl uri
# Resolve URI (uri):
#         uri: file:///missing.xsl
# Result: null
#
# Arguments:
#	$(1)	file name of item to be resolved, WITHOUT the "file:///"
#
resolve_uri = `java org.apache.xml.resolver.apps.resolver -u file:///$(1) uri | ( grep 'Result: file:' || echo '::$(1)' ) | cut -d':' -f3-`


################################################################################
#
# Pass stylesheet parameters as appropriate to the XSLT processor.
# $(XSLT) should be either defined as an environment variable, or
# passed in as a parameter to make.
#
# Note: we expect the value of the stylesheet parameter to be quoted,
# because it may potentially contain spaces. If it's not quoted before
# being passed to this function, Weird Things May Occur.
#
# Arguments:
#	$(1)	name of stylesheet parameter
#	$(2)	value for stylesheet parameter (quoted)
#
xslt_parameter = $(if $(findstring $(XSLT),'xalan-c'),-p $(1) $(2),$(if $(findstring $(XSLT),'xalan-j'),-param $(1) $(2),$(if $(findstring $(XSLT),'saxon'),$(1)=$(2))))


################################################################################
#
# XSLT processor specific calling templates/functions, with parameters for
# subject-code, paper-number, standalone, showanswers and base-path.
# Unfortunately each of the major XSLT processors differs in the way it
# handles command-line arguments :(  Even worse, Xalan-C differs from
# Xalan-J!!
# 
# Examples:
# 
# 	Xalan -p department "'INFO'" -p paper "'111'" -p standalone "'no'" 
# 	      -p showanswers "'no'" -p base-path "'../Tutorials'" 
# 	      -p image-format "'eps'" $< xml2latex.xsl > $@
# 
# 	saxon $< xml2latex.xsl department='INFO' paper='111' standalone='no'
# 	      showanswers='no' base-path='../Tutorials' image-format='eps' > $@
#
# Arguments:
#	$(1)	source XML file
#	$(2)	XSL stylesheet
#	$(3)->	Parameter strings for various stylesheet parameters, as many
#			as required. Currently supported are "department", "paper",
#			"standalone", "showanswers", "base-path", "image-format").
#			The order of parameters doesn't matter.
#
# Parameter strings must be pre-formatted using the xslt_parameter function.
#

#
# Xalan-C
#
# xalanc = Xalan $(3) $(4) $(5) $(6) $(7) $(8) $(1) $(2)
#
xalanc = Xalan $(3) $(4) $(5) $(6) $(7) $(8) $(1) $(call resolve_uri,$(2))

#
# Xalan-J
#
# xalanj = xalan -in $(1) -xsl $(2) \
# 			-uriresolver org.apache.xml.resolver.tools.CatalogResolver \
# 			$(3) $(4) $(5) $(6) $(7) $(8)
#
xalanj = xalan -in $(1) -xsl $(call resolve_uri,$(2)) $(3) $(4) $(5) $(6) $(7) $(8)

#
# SAXON
#
# saxon = saxon -x org.apache.xml.resolver.tools.ResolvingXMLReader \
# 			-y org.apache.xml.resolver.tools.ResolvingXMLReader \
# 			-r org.apache.xml.resolver.tools.CatalogResolver \
# 			$(1) file:///$(2) $(3) $(4) $(5) $(6) $(7) $(8)
#
saxon = saxon $(1) $(call resolve_uri,$(2)) $(3) $(4) $(5) $(6) $(7) $(8)


################################################################################
#
# Generic function for runnning the local preferred XSLT processor.
# Redirecting the output to an appropriate output file is handled by the
# caller.
#
# Arguments:
#	$(1)	source XML file
#	$(2)	XSL stylesheet
#	$(3)->	Parameter strings for various stylesheet parameters, as many
#			as required. Currently supported are "department", "paper",
#			"standalone", "showanswers", "base-path", "image-format").
#			The order of parameters doesn't matter.
#
# Parameter strings must be pre-formatted using the xslt_parameter function.
#
xslt = $(if $(findstring $(XSLT),'xalan-c'),$(call xalanc,$(1),$(2),$(3),$(4),$(5),$(6),$(7),$(8)),$(if $(findstring $(XSLT),'xalan-j'),$(call xalanj,$(1),$(2),$(3),$(4),$(5),$(6),$(7),$(8)),$(if $(findstring $(XSLT),'saxon'),$(call saxon,$(1),$(2),$(3),$(4),$(5),$(6),$(7),$(8)))))


# xslt_debug:
# 	@announce "XSLT invocation (change value of XSLT to test others)"
# 	@echo "$(call xslt,input.xml,stylesheet.xsl,$(call xslt_parameter,department,'$(SUBJECT_CODE)'),$(call xslt_parameter,paper,'$(PAPER_NUMBER)'),$(call xslt_parameter,standalone,'no'),$(call xslt_parameter,showanswers,'no'),$(call xslt_parameter,base-path,'$(SECTION)'),$(call xslt_parameter,image-format,'pdf')) > output_file"