You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

295 lines
11 KiB

4 years ago
  1. <?xml version="1.0" encoding="UTF-8"?><?xar XSLT?>
  2. <!--
  3. OVERVIEW - iso_abstract_expand.xsl
  4. This is a preprocessor for ISO Schematron, which implements abstract patterns.
  5. It also
  6. * extracts a particular schema using an ID, where there are multiple
  7. schemas, such as when they are embedded in the same NVDL script
  8. * experimentally, allows parameter recognition and substitution inside
  9. text as well as @context, @test, & @select.
  10. This should be used after iso-dsdl-include.xsl and before the skeleton or
  11. meta-stylesheet (e.g. iso-svrl.xsl) . It only requires XSLT 1.
  12. Each kind of inclusion can be turned off (or on) on the command line.
  13. -->
  14. <!--
  15. VERSION INFORMATION
  16. 2008-09-18 RJ
  17. * move out param test from iso:schema template to work with XSLT 1. (Noah Fontes)
  18. 2008-07-29 RJ
  19. * Create. Pull out as distinct XSL in its own namespace from old iso_pre_pro.xsl
  20. * Put everything in private namespace
  21. * Rewrite replace_substring named template so that copyright is clear
  22. 2008-07-24 RJ
  23. * correct abstract patterns so for correct names: param/@name and
  24. param/@value
  25. 2007-01-12 RJ
  26. * Use ISO namespace
  27. * Use pattern/@id not pattern/@name
  28. * Add Oliver Becker's suggests from old Schematron-love-in list for <copy>
  29. * Add XT -ism?
  30. 2003 RJ
  31. * Original written for old namespace
  32. * http://www.topologi.com/resources/iso-pre-pro.xsl
  33. -->
  34. <!--
  35. LEGAL INFORMATION
  36. Copyright (c) 2000-2008 Rick Jelliffe and Academia Sinica Computing Center, Taiwan
  37. This software is provided 'as-is', without any express or implied warranty.
  38. In no event will the authors be held liable for any damages arising from
  39. the use of this software.
  40. Permission is granted to anyone to use this software for any purpose,
  41. including commercial applications, and to alter it and redistribute it freely,
  42. subject to the following restrictions:
  43. 1. The origin of this software must not be misrepresented; you must not claim
  44. that you wrote the original software. If you use this software in a product,
  45. an acknowledgment in the product documentation would be appreciated but is
  46. not required.
  47. 2. Altered source versions must be plainly marked as such, and must not be
  48. misrepresented as being the original software.
  49. 3. This notice may not be removed or altered from any source distribution.
  50. -->
  51. <xslt:stylesheet version="1.0" xmlns:xslt="http://www.w3.org/1999/XSL/Transform"
  52. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  53. xmlns:iso="http://purl.oclc.org/dsdl/schematron"
  54. xmlns:nvdl="http://purl.oclc.org/dsdl/nvdl"
  55. xmlns:iae="http://www.schematron.com/namespace/iae"
  56. >
  57. <xslt:param name="schema-id"></xslt:param>
  58. <!-- Driver for the mode -->
  59. <xsl:template match="/">
  60. <xsl:apply-templates select="." mode="iae:go" />
  61. </xsl:template>
  62. <!-- ================================================================================== -->
  63. <!-- Normal processing rules -->
  64. <!-- ================================================================================== -->
  65. <!-- Output only the selected schema -->
  66. <xslt:template match="iso:schema" >
  67. <xsl:if test="string-length($schema-id) =0 or @id= $schema-id ">
  68. <xslt:copy>
  69. <xslt:copy-of select="@*" />
  70. <xslt:apply-templates mode="iae:go" />
  71. </xslt:copy>
  72. </xsl:if>
  73. </xslt:template>
  74. <!-- Strip out any foreign elements above the Schematron schema .
  75. -->
  76. <xslt:template match="*[not(ancestor-or-self::iso:*)]" mode="iae:go" >
  77. <xslt:apply-templates mode="iae:go" />
  78. </xslt:template>
  79. <!-- ================================================================================== -->
  80. <!-- Handle Schematron abstract pattern preprocessing -->
  81. <!-- abstract-to-real calls
  82. do-pattern calls
  83. macro-expand calls
  84. multi-macro-expand
  85. replace-substring -->
  86. <!-- ================================================================================== -->
  87. <!--
  88. Abstract patterns allow you to say, for example
  89. <pattern name="htmlTable" is-a="table">
  90. <param name="row" value="html:tr"/>
  91. <param name="cell" value="html:td" />
  92. <param name="table" value="html:table" />
  93. </pattern>
  94. For a good introduction, see Uche Ogbujii's article for IBM DeveloperWorks
  95. "Discover the flexibility of Schematron abstract patterns"
  96. http://www-128.ibm.com/developerworks/xml/library/x-stron.html
  97. However, note that ISO Schematron uses @name and @value attributes on
  98. the iso:param element, and @id not @name on the pattern element.
  99. -->
  100. <!-- Suppress declarations of abstract patterns -->
  101. <xslt:template match="iso:pattern[@abstract='true']" mode="iae:go" >
  102. <xslt:comment>Suppressed abstract pattern <xslt:value-of select="@id"/> was here</xslt:comment>
  103. </xslt:template>
  104. <!-- Suppress uses of abstract patterns -->
  105. <xslt:template match="iso:pattern[@is-a]" mode="iae:go" >
  106. <xslt:comment>Start pattern based on abstract <xslt:value-of select="@is-a"/></xslt:comment>
  107. <xslt:call-template name="iae:abstract-to-real" >
  108. <xslt:with-param name="caller" select="@id" />
  109. <xslt:with-param name="is-a" select="@is-a" />
  110. </xslt:call-template>
  111. </xslt:template>
  112. <!-- output everything else unchanged -->
  113. <xslt:template match="*" priority="-1" mode="iae:go" >
  114. <xslt:copy>
  115. <xslt:copy-of select="@*" />
  116. <xslt:apply-templates mode="iae:go"/>
  117. </xslt:copy>
  118. </xslt:template>
  119. <!-- Templates for macro expansion of abstract patterns -->
  120. <!-- Sets up the initial conditions for the recursive call -->
  121. <xslt:template name="iae:macro-expand">
  122. <xslt:param name="caller"/>
  123. <xslt:param name="text" />
  124. <xslt:call-template name="iae:multi-macro-expand">
  125. <xslt:with-param name="caller" select="$caller"/>
  126. <xslt:with-param name="text" select="$text"/>
  127. <xslt:with-param name="paramNumber" select="1"/>
  128. </xslt:call-template>
  129. </xslt:template>
  130. <!-- Template to replace the current parameter and then
  131. recurse to replace subsequent parameters. -->
  132. <xslt:template name="iae:multi-macro-expand">
  133. <xslt:param name="caller"/>
  134. <xslt:param name="text" />
  135. <xslt:param name="paramNumber" />
  136. <xslt:choose>
  137. <xslt:when test="//iso:pattern[@id=$caller]/iso:param[ $paramNumber]">
  138. <xslt:call-template name="iae:multi-macro-expand">
  139. <xslt:with-param name="caller" select="$caller"/>
  140. <xslt:with-param name="paramNumber" select="$paramNumber + 1"/>
  141. <xslt:with-param name="text" >
  142. <xslt:call-template name="iae:replace-substring">
  143. <xslt:with-param name="original" select="$text"/>
  144. <xslt:with-param name="substring"
  145. select="concat('$', //iso:pattern[@id=$caller]/iso:param[ $paramNumber ]/@name)"/>
  146. <xslt:with-param name="replacement"
  147. select="//iso:pattern[@id=$caller]/iso:param[ $paramNumber ]/@value"/>
  148. </xslt:call-template>
  149. </xslt:with-param>
  150. </xslt:call-template>
  151. </xslt:when>
  152. <xslt:otherwise><xslt:value-of select="$text" /></xslt:otherwise>
  153. </xslt:choose>
  154. </xslt:template>
  155. <!-- generate the real pattern from an abstract pattern + parameters-->
  156. <xslt:template name="iae:abstract-to-real" >
  157. <xslt:param name="caller"/>
  158. <xslt:param name="is-a" />
  159. <xslt:for-each select="//iso:pattern[@id= $is-a]">
  160. <xslt:copy>
  161. <xslt:choose>
  162. <xslt:when test=" string-length( $caller ) = 0">
  163. <xslt:attribute name="id"><xslt:value-of select="concat( generate-id(.) , $is-a)" /></xslt:attribute>
  164. </xslt:when>
  165. <xslt:otherwise>
  166. <xslt:attribute name="id"><xslt:value-of select="$caller" /></xslt:attribute>
  167. </xslt:otherwise>
  168. </xslt:choose>
  169. <xslt:apply-templates select="*|text()" mode="iae:do-pattern" >
  170. <xslt:with-param name="caller"><xslt:value-of select="$caller"/></xslt:with-param>
  171. </xslt:apply-templates>
  172. </xslt:copy>
  173. </xslt:for-each>
  174. </xslt:template>
  175. <!-- Generate a non-abstract pattern -->
  176. <xslt:template mode="iae:do-pattern" match="*">
  177. <xslt:param name="caller"/>
  178. <xslt:copy>
  179. <xslt:for-each select="@*[name()='test' or name()='context' or name()='select']">
  180. <xslt:attribute name="{name()}">
  181. <xslt:call-template name="iae:macro-expand">
  182. <xslt:with-param name="text"><xslt:value-of select="."/></xslt:with-param>
  183. <xslt:with-param name="caller"><xslt:value-of select="$caller"/></xslt:with-param>
  184. </xslt:call-template>
  185. </xslt:attribute>
  186. </xslt:for-each>
  187. <xslt:copy-of select="@*[name()!='test'][name()!='context'][name()!='select']" />
  188. <xsl:for-each select="node()">
  189. <xsl:choose>
  190. <!-- Experiment: replace macros in text as well, to allow parameterized assertions
  191. and so on, without having to have spurious <iso:value-of> calls and multiple
  192. delimiting -->
  193. <xsl:when test="self::text()">
  194. <xslt:call-template name="iae:macro-expand">
  195. <xslt:with-param name="text"><xslt:value-of select="."/></xslt:with-param>
  196. <xslt:with-param name="caller"><xslt:value-of select="$caller"/></xslt:with-param>
  197. </xslt:call-template>
  198. </xsl:when>
  199. <xsl:otherwise>
  200. <xslt:apply-templates select="." mode="iae:do-pattern">
  201. <xslt:with-param name="caller"><xslt:value-of select="$caller"/></xslt:with-param>
  202. </xslt:apply-templates>
  203. </xsl:otherwise>
  204. </xsl:choose>
  205. </xsl:for-each>
  206. </xslt:copy>
  207. </xslt:template>
  208. <!-- UTILITIES -->
  209. <!-- Simple version of replace-substring function -->
  210. <xslt:template name="iae:replace-substring">
  211. <xslt:param name="original" />
  212. <xslt:param name="substring" />
  213. <xslt:param name="replacement" select="''"/>
  214. <xsl:choose>
  215. <xsl:when test="not($original)" />
  216. <xsl:when test="not(string($substring))">
  217. <xsl:value-of select="$original" />
  218. </xsl:when>
  219. <xsl:when test="contains($original, $substring)">
  220. <xsl:variable name="before" select="substring-before($original, $substring)" />
  221. <xsl:variable name="after" select="substring-after($original, $substring)" />
  222. <xsl:value-of select="$before" />
  223. <xsl:value-of select="$replacement" />
  224. <!-- recursion -->
  225. <xsl:call-template name="iae:replace-substring">
  226. <xsl:with-param name="original" select="$after" />
  227. <xsl:with-param name="substring" select="$substring" />
  228. <xsl:with-param name="replacement" select="$replacement" />
  229. </xsl:call-template>
  230. </xsl:when>
  231. <xsl:otherwise>
  232. <!-- no substitution -->
  233. <xsl:value-of select="$original" />
  234. </xsl:otherwise>
  235. </xsl:choose>
  236. </xslt:template>
  237. </xslt:stylesheet>