Newer
Older
Handbook / make-includes / xslt_functions.make
  1. ################################################################################
  2. #
  3. # File: $Id$
  4. #
  5. # Make functions for handling XSLT tasks.
  6. #
  7. ################################################################################
  8.  
  9.  
  10. ################################################################################
  11. #
  12. # XSLT must be defined as an environment variable. It's an error not to do so.
  13. #
  14. 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)
  15.  
  16.  
  17. ################################################################################
  18. #
  19. # Resolve a URI for some XML entity such as a stylesheet into a path to
  20. # the actual item. This works by calling the example resolver application
  21. # that comes with the entity resolver, then extracting the full path from
  22. # the result. If the URI cannot be resolved, the function returns the
  23. # original filename so that the XSLT processor can fail with a meaningful
  24. # error message.
  25. #
  26. # This is a workaround for the mysterious breakage that occured on Nigel's
  27. # machine in early 2006 where all of the XSLT processors suddenly stopped
  28. # accepting resolver class names as command line arguments.
  29. #
  30. # This approach, while apparently kludgey, does have a particular
  31. # advantage, however, in that it can be used with non-Java based XSLT
  32. # processors such as Xalan C++, whereas the previous approach only worked
  33. # with Saxon and Xalan-J. In other words, this will work with ANY XSLT
  34. # processor.
  35. #
  36. # Note that the function adds the "file:///" protocol to the front of the
  37. # argument, so callers should NOT add this themselves!
  38. #
  39. # For reference, the typical output from the resolver application looks
  40. # something like this for a successful resolution:
  41. #
  42. # % java org.apache.xml.resolver.apps.resolver -u file:///foo.xsl uri
  43. # Resolve URI (uri):
  44. # uri: file:///foo.xsl
  45. # Result: file:/path/to/item/foo.xsl
  46. #
  47. # and like this for a failure:
  48. #
  49. # % java org.apache.xml.resolver.apps.resolver -u file:///missing.xsl uri
  50. # Resolve URI (uri):
  51. # uri: file:///missing.xsl
  52. # Result: null
  53. #
  54. # Update (2007-07-11): Added fix for mysterious breakage on Chris's machine
  55. # that occurred for no apparent reason. An extra / was being put on the
  56. # front of the returned Windows path, e.g., /D:/blah/blah. Added a sed
  57. # to remove it when necessary.
  58. #
  59. # Arguments:
  60. # $(1) file name of item to be resolved, WITHOUT the "file:///"
  61. #
  62. resolve_uri = `$(JAVA) org.apache.xml.resolver.apps.resolver -u file:///$(1) uri | ( $(GREP) 'Result: file:' || $(ECHO) '::$(1)' ) | $(CUT) -d':' -f3- | $(SED) -e 's|/\([A-Z]:\)|\1|'`
  63.  
  64.  
  65. ################################################################################
  66. #
  67. # Pass stylesheet parameters as appropriate to the XSLT processor.
  68. # $(XSLT) should be either defined as an environment variable, or
  69. # passed in as a parameter to make.
  70. #
  71. # Note: we expect the value of the stylesheet parameter to be quoted,
  72. # because it may potentially contain spaces. If it's not quoted before
  73. # being passed to this function, Weird Things May Occur.
  74. #
  75. # Arguments:
  76. # $(1) name of stylesheet parameter
  77. # $(2) value for stylesheet parameter (quoted)
  78. #
  79. xslt_parameter = $(if $(findstring $(XSLT),'xalan-c'),-p $(1) $(2),$(if $(findstring $(XSLT),'xalan-j'),-param $(1) $(2),$(if $(findstring $(XSLT),'saxon'),$(1)=$(2),$(if $(findstring $(XSLT),'saxon-b'),$(1)=$(2)))))
  80.  
  81.  
  82. ################################################################################
  83. #
  84. # XSLT processor specific calling templates/functions, with parameters for
  85. # subject-code, paper-number, standalone, showanswers and base-path.
  86. # Unfortunately each of the major XSLT processors differs in the way it
  87. # handles command-line arguments :( Even worse, Xalan-C differs from
  88. # Xalan-J!!
  89. #
  90. # Note that the path to the XSLT processor is stored in the make variable
  91. # XSLTPROC, which is set by the local configuration script.
  92. #
  93. # Examples:
  94. #
  95. # Xalan -p department "'INFO'" -p paper "'111'" -p standalone "'no'"
  96. # -p showanswers "'no'" -p base-path "'../Tutorials'"
  97. # -p image-format "'eps'" $< xml2latex.xsl > $@
  98. #
  99. # saxon $< xml2latex.xsl department='INFO' paper='111' standalone='no'
  100. # showanswers='no' base-path='../Tutorials' image-format='eps' > $@
  101. #
  102. # Arguments:
  103. # $(1) source XML file
  104. # $(2) XSL stylesheet
  105. # $(3)-> Parameter strings for various stylesheet parameters, as many
  106. # as required. Currently supported are "department", "paper",
  107. # "standalone", "showanswers", "base-path", "image-format").
  108. # The order of parameters doesn't matter.
  109. #
  110. # Parameter strings must be pre-formatted using the xslt_parameter function.
  111. #
  112.  
  113. #
  114. # Xalan-C
  115. #
  116. # xalanc = Xalan $(3) $(4) $(5) $(6) $(7) $(8) $(9) $(10) $(1) $(2)
  117. #
  118. xalanc = $(XSLTPROC) $(3) $(4) $(5) $(6) $(7) $(8) $(9) $(10) $(1) $(call resolve_uri,$(2))
  119.  
  120. #
  121. # Xalan-J
  122. #
  123. # xalanj = xalan -in $(1) -xsl $(2) \
  124. # -uriresolver org.apache.xml.resolver.tools.CatalogResolver \
  125. # $(3) $(4) $(5) $(6) $(7) $(8) $(9) $(10)
  126. #
  127. xalanj = $(XSLTPROC) -in $(1) -xsl $(call resolve_uri,$(2)) $(3) $(4) $(5) $(6) $(7) $(8) $(9) $(10)
  128.  
  129. #
  130. # SAXON
  131. #
  132. # saxon = saxon -x org.apache.xml.resolver.tools.ResolvingXMLReader \
  133. # -y org.apache.xml.resolver.tools.ResolvingXMLReader \
  134. # -r org.apache.xml.resolver.tools.CatalogResolver \
  135. # $(1) file:///$(2) $(3) $(4) $(5) $(6) $(7) $(8) $(9) $(10)
  136. #
  137. saxon = $(XSLTPROC) $(1) $(call resolve_uri,$(2)) $(3) $(4) $(5) $(6) $(7) $(8) $(9) $(10)
  138.  
  139. #
  140. # SAXON-B
  141. #
  142. # saxon = saxon-b -s:$(1) -xsl:file:///$(2) $(3) $(4) $(5) $(6) $(7) $(8) $(9) $(10)
  143. #
  144. saxonb = $(XSLTPROC) -s:$(1) -xsl:$(call resolve_uri,$(2)) $(3) $(4) $(5) $(6) $(7) $(8) $(9) $(10)
  145.  
  146.  
  147. ################################################################################
  148. #
  149. # Generic function for runnning the local preferred XSLT processor.
  150. # Redirecting the output to an appropriate output file is handled by the
  151. # caller.
  152. #
  153. # Arguments:
  154. # $(1) source XML file
  155. # $(2) XSL stylesheet
  156. # $(3)-> Parameter strings for various stylesheet arguments, as many
  157. # as required. Currently supported are "subject-code",
  158. # "paper-number", "standalone", "showanswers", "base-path",
  159. # "image-format"). The order of parameters doesn't matter.
  160. #
  161. # Parameter strings must be pre-formatted using the xslt_parameter function.
  162. #
  163. xslt = $(if $(findstring $(XSLT),'xalan-c'),$(call xalanc,$(1),$(2),$(3),$(4),$(5),$(6),$(7),$(8),$(9),$(10)),$(if $(findstring $(XSLT),'xalan-j'),$(call xalanj,$(1),$(2),$(3),$(4),$(5),$(6),$(7),$(8),$(9),$(10)),$(if $(findstring $(XSLT),'saxon'),$(call saxon,$(1),$(2),$(3),$(4),$(5),$(6),$(7),$(8),$(9),$(10)),$(if $(findstring $(XSLT),'saxon-b'),$(call saxonb,$(1),$(2),$(3),$(4),$(5),$(6),$(7),$(8),$(9),$(10))))))
  164.  
  165.  
  166. # xslt_debug:
  167. # @announce "XSLT invocation (change value of XSLT to test others)"
  168. # @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"