util.xsl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
  3. xmlns:eat="http://msqr.us/xsd/ieat"
  4. xmlns:xweb="http://msqr.us/xsd/jaxb-web"
  5. exclude-result-prefixes="eat xweb">
  6. <!--
  7. Named Template: javascript-string
  8. Replace occurances of " in a string with \".
  9. Parameters:
  10. output-string - the text to seach/replace in
  11. -->
  12. <xsl:template name="javascript-string">
  13. <xsl:param name="output-string"/>
  14. <xsl:call-template name="global-replace">
  15. <xsl:with-param name="output-string" select="$output-string"/>
  16. <xsl:with-param name="target"><xsl:text>"</xsl:text></xsl:with-param>
  17. <xsl:with-param name="replacement"><xsl:text>\"</xsl:text></xsl:with-param>
  18. </xsl:call-template>
  19. </xsl:template>
  20. <!--
  21. Named Template: single-quote-string
  22. Replace occurances of ' in a string with \'.
  23. Parameters:
  24. output-string - the text to seach/replace in
  25. -->
  26. <xsl:template name="single-quote-string">
  27. <xsl:param name="output-string"/>
  28. <xsl:call-template name="global-replace">
  29. <xsl:with-param name="output-string" select="$output-string"/>
  30. <xsl:with-param name="target"><xsl:text>'</xsl:text></xsl:with-param>
  31. <xsl:with-param name="replacement"><xsl:text>\'</xsl:text></xsl:with-param>
  32. </xsl:call-template>
  33. </xsl:template>
  34. <!--
  35. Named Template: escape-string
  36. Replace occurances of a string with that string preceeded by a '\'
  37. character.
  38. Parameters:
  39. output-string - the text to seach/replace in
  40. target - the text to search for
  41. -->
  42. <xsl:template name="escape-string">
  43. <xsl:param name="output-string"/>
  44. <xsl:param name="target"/>
  45. <xsl:call-template name="global-replace">
  46. <xsl:with-param name="output-string" select="$output-string"/>
  47. <xsl:with-param name="target" select="$target"/>
  48. <xsl:with-param name="replacement">
  49. <xsl:text>\</xsl:text>
  50. <xsl:value-of select="$target"/>
  51. </xsl:with-param>
  52. </xsl:call-template>
  53. </xsl:template>
  54. <!--
  55. Named Template: global-replace
  56. Replace occurances of one string with another.
  57. Parameters:
  58. output-string - the text to seach/replace in
  59. target - the text to search for
  60. replacement - the text to replace occurances of 'target' with
  61. -->
  62. <xsl:template name="global-replace">
  63. <xsl:param name="output-string"/>
  64. <xsl:param name="target"/>
  65. <xsl:param name="replacement"/>
  66. <xsl:choose>
  67. <xsl:when test="contains($output-string,$target)">
  68. <xsl:value-of select=
  69. "concat(substring-before($output-string,$target), $replacement)"/>
  70. <xsl:call-template name="global-replace">
  71. <xsl:with-param name="output-string"
  72. select="substring-after($output-string,$target)"/>
  73. <xsl:with-param name="target" select="$target"/>
  74. <xsl:with-param name="replacement"
  75. select="$replacement"/>
  76. </xsl:call-template>
  77. </xsl:when>
  78. <xsl:otherwise>
  79. <xsl:value-of select="$output-string"/>
  80. </xsl:otherwise>
  81. </xsl:choose>
  82. </xsl:template>
  83. <!--
  84. Named Template: truncate-at-word
  85. Truncate a string at a word break (space). If the input text
  86. is shorter than max-length the text is returned unchanged.
  87. Otherwise the text is truncated at the max-length plus any
  88. characters up to the next space, and a ellipsis character is
  89. appended.
  90. Parameters:
  91. text - the text to truncate
  92. max-length - the maximum number of characters to allow
  93. -->
  94. <xsl:template name="truncate-at-word">
  95. <xsl:param name="text"/>
  96. <xsl:param name="max-length">350</xsl:param>
  97. <xsl:choose>
  98. <xsl:when test="string-length($text) &lt; $max-length">
  99. <xsl:value-of select="$text" disable-output-escaping="yes"/>
  100. </xsl:when>
  101. <xsl:otherwise>
  102. <xsl:variable name="start" select="substring($text,1,$max-length)"/>
  103. <xsl:variable name="after" select="substring($text,($max-length+1))"/>
  104. <xsl:variable name="word" select="substring-before($after,' ')"/>
  105. <xsl:value-of select="$start" disable-output-escaping="yes"/>
  106. <xsl:value-of select="$word" disable-output-escaping="yes"/>
  107. <xsl:text>&#x2026;</xsl:text>
  108. </xsl:otherwise>
  109. </xsl:choose>
  110. </xsl:template>
  111. </xsl:stylesheet>