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