Advertisement
ratclma

process_template.xsl

Aug 12th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.37 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. This stylesheet is the standard template processing
  4. stylesheet for use with the DirXML Manual Task Service Driver.
  5. -->
  6. <xsl:transform
  7. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  8. xmlns:form="http://www.novell.com/dirxml/manualtask/form"
  9. xmlns:query="http://www.novell.com/nxsl/java/com.novell.nds.dirxml.driver.manualtask.ManualTaskQueryProcessor"
  10. xmlns:xdsdn="http://www.novell.com/nxsl/java/com.novell.nds.dirxml.driver.XdsDN"
  11. exclude-result-prefixes="form query"
  12. version="1.0"
  13. >
  14.  
  15. <xsl:strip-space elements="*"/>
  16.  
  17. <xsl:param name="replacement-data" select="/.."/>
  18. <xsl:param name="queryProcessor"/>
  19.  
  20. <!--
  21. match any element and construct it anew so we can add handle replacing
  22. attribute values with replacement token values, etc.
  23. -->
  24. <xsl:template match="*">
  25. <xsl:element name="{name()}">
  26. <!-- iterate through the attributes, looking for replacement data -->
  27. <xsl:apply-templates select="@*"/>
  28. <!-- go process any content -->
  29. <xsl:apply-templates select="node()"/>
  30. </xsl:element>
  31. </xsl:template>
  32.  
  33. <xsl:template match="@*">
  34. <xsl:attribute name="{name()}">
  35. <xsl:choose>
  36. <xsl:when test="contains(.,'$')">
  37. <xsl:call-template name="replace-tokens">
  38. <xsl:with-param name="source" select="string(.)"/>
  39. </xsl:call-template>
  40. </xsl:when>
  41. <xsl:otherwise>
  42. <xsl:value-of select="."/>
  43. </xsl:otherwise>
  44. </xsl:choose>
  45. </xsl:attribute>
  46. </xsl:template>
  47.  
  48. <xsl:template match="text()">
  49. <xsl:choose>
  50. <xsl:when test="contains(.,'$')">
  51. <xsl:call-template name="replace-tokens">
  52. <xsl:with-param name="source" select="string(.)"/>
  53. </xsl:call-template>
  54. </xsl:when>
  55. <xsl:otherwise>
  56. <xsl:value-of select="."/>
  57. </xsl:otherwise>
  58. </xsl:choose>
  59. </xsl:template>
  60.  
  61. <xsl:template match="form:input[@name]">
  62. <xsl:variable name="items" select="$replacement-data/replacement-data/item[@name = current()/@name]"/>
  63. <xsl:variable name="type" select="string(@*[name() = 'type' or name() = 'TYPE'])"/>
  64. <xsl:variable name="use-value" select="@value = 'yes'"/>
  65. <xsl:for-each select="$items">
  66. <INPUT type="{$type}" name="{@name}">
  67. <xsl:choose>
  68. <xsl:when test="$use-value">
  69. <xsl:attribute name="value"><xsl:value-of select="."/></xsl:attribute>
  70. </xsl:when>
  71. <xsl:otherwise>
  72. <xsl:value-of select="."/>
  73. </xsl:otherwise>
  74. </xsl:choose>
  75. </INPUT>
  76. </xsl:for-each>
  77. </xsl:template>
  78.  
  79. <xsl:template match="form:if-item-exists[@name]">
  80. <!--
  81. process content if an <item> element exists whose name attribute matches
  82. the name attribute of the form:if-item-exists element
  83. -->
  84. <xsl:if test="$replacement-data/replacement-data/item[@name = current()/@name]">
  85. <xsl:apply-templates/>
  86. </xsl:if>
  87. </xsl:template>
  88.  
  89. <xsl:template match="form:if-multiple-items[@name]">
  90. <!--
  91. process content if more than one <item> element exists whose name attributes match
  92. the name attribute of the form:if-multiple-items element
  93. -->
  94. <xsl:if test="count($replacement-data/replacement-data/item[@name = current()/@name]) > 1">
  95. <xsl:apply-templates/>
  96. </xsl:if>
  97. </xsl:template>
  98.  
  99. <xsl:template match="form:if-single-item[@name]">
  100. <!--
  101. process content if more than one <item> element exists whose name attributes match
  102. the name attribute of the form:if-multiple-items element
  103. -->
  104. <xsl:if test="count($replacement-data/replacement-data/item[@name = current()/@name]) = 1">
  105. <xsl:apply-templates/>
  106. </xsl:if>
  107. </xsl:template>
  108.  
  109. <!-- create a <SELECT><OPTION/><OPTION/></SELECT> construct for an HTML form -->
  110. <xsl:template match="form:menu[@name]">
  111. <xsl:variable name="items" select="$replacement-data/replacement-data/item[@name = current()/@name]"/>
  112. <xsl:if test="$items">
  113. <SELECT name="{@name}">
  114. <OPTION selected="selected"><xsl:value-of select="$items[1]"/></OPTION>
  115. <xsl:for-each select="$items[position() > 1]">
  116. <OPTION><xsl:value-of select="."/></OPTION>
  117. </xsl:for-each>
  118. </SELECT>
  119. </xsl:if>
  120. </xsl:template>
  121.  
  122. <xsl:template match="form:*">
  123. <!-- strip form: elements that aren't explicitly handled -->
  124. <xsl:apply-templates/>
  125. </xsl:template>
  126.  
  127. <xsl:template name="replace-tokens">
  128. <xsl:param name="source"/>
  129. <!-- see if we have a replacement token -->
  130. <xsl:variable name="before" select="substring-before($source, '$')"/>
  131. <xsl:variable name="temp" select="substring-after($source, '$')"/>
  132. <xsl:variable name="token" select="substring-before($temp, '$')"/>
  133. <xsl:variable name="after" select="substring-after($temp, '$')"/>
  134.  
  135. <xsl:choose>
  136. <xsl:when test="$token = '' and not(starts-with($temp,'$'))">
  137. <!-- no token, just output source string -->
  138. <xsl:value-of select="$source"/>
  139. </xsl:when>
  140. <xsl:otherwise>
  141. <!-- output text before substitution token -->
  142. <xsl:value-of select="$before"/>
  143. <!-- output substitution text -->
  144. <xsl:choose>
  145. <xsl:when test="$token = ''">
  146. <!-- adjacent $$, replace with $ -->
  147. <xsl:value-of select="'$'"/>
  148. </xsl:when>
  149. <xsl:when test="starts-with($token,'query:')">
  150. <xsl:call-template name="get-current-data">
  151. <xsl:with-param name="name" select="substring-after($token,'query:')"/>
  152. </xsl:call-template>
  153. </xsl:when>
  154. <xsl:otherwise>
  155. <xsl:call-template name="get-replacement-data">
  156. <xsl:with-param name="name" select="$token"/>
  157. </xsl:call-template>
  158. </xsl:otherwise>
  159. </xsl:choose>
  160. <!-- recurse to finish string -->
  161. <xsl:if test="$after != ''">
  162. <xsl:call-template name="replace-tokens">
  163. <xsl:with-param name="source" select="$after"/>
  164. </xsl:call-template>
  165. </xsl:if>
  166. </xsl:otherwise>
  167. </xsl:choose>
  168. </xsl:template>
  169.  
  170. <xsl:template name="get-replacement-data">
  171. <xsl:param name="name"/>
  172. <xsl:value-of select="$replacement-data/replacement-data/item[@name = $name]"/>
  173. </xsl:template>
  174.  
  175. <xsl:template name="get-current-data">
  176. <xsl:param name="name"/>
  177. <xsl:variable name="association">
  178. <xsl:call-template name="get-replacement-data">
  179. <xsl:with-param name="name" select="'association'"/>
  180. </xsl:call-template>
  181. </xsl:variable>
  182. <xsl:variable name="src-dn">
  183. <xsl:call-template name="get-replacement-data">
  184. <xsl:with-param name="name" select="'src-dn'"/>
  185. </xsl:call-template>
  186. </xsl:variable>
  187. <xsl:variable name="src-entry-id">
  188. <xsl:call-template name="get-replacement-data">
  189. <xsl:with-param name="name" select="'src-entry-id'"/>
  190. </xsl:call-template>
  191. </xsl:variable>
  192. <xsl:variable name="query">
  193. <query scope="entry">
  194. <!-- use entry-id value if we have it -->
  195. <xsl:choose>
  196. <xsl:when test="$src-entry-id">
  197. <xsl:attribute name="dest-entry-id"><xsl:value-of select="$src-entry-id"/></xsl:attribute>
  198. </xsl:when>
  199. <xsl:when test="$src-dn">
  200. <xsl:attribute name="dest-dn"><xsl:value-of select="$src-dn"/></xsl:attribute>
  201. </xsl:when>
  202. <xsl:otherwise>
  203. <association><xsl:value-of select="$association"/></association>
  204. </xsl:otherwise>
  205. </xsl:choose>
  206. <read-attr attr-name="{$name}"/>
  207. </query>
  208. </xsl:variable>
  209. <xsl:variable name="result" select="query:query($queryProcessor,$query)"/>
  210. <xsl:value-of select="$result//attr[@attr-name=$name]/value[1]"/>
  211. </xsl:template>
  212.  
  213. <!-- strip comments from templates -->
  214. <xsl:template match="comment()"/>
  215.  
  216. <!-- match everything else not handled above -->
  217. <xsl:template match="processing-instruction()">
  218. <xsl:copy/>
  219. </xsl:template>
  220.  
  221. </xsl:transform>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement