Created
April 27, 2011 07:59
-
-
Save makenosound/943886 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Call the template --> | |
<xsl:call-template name="replace-node-set"> | |
<xsl:with-param name="input" select="hints/text()" /> | |
<xsl:with-param name="node" select="//pattern" /> | |
<xsl:with-param name="total" select="count(//pattern)" /> | |
</xsl:call-template> | |
<!-- Recursive template that iterates through the pattern nodes --> | |
<xsl:template name="replace-node-set"> | |
<xsl:param name="node" /> | |
<xsl:param name="input" /> | |
<xsl:param name="total" /> | |
<xsl:param name="count" select="1"/> | |
<xsl:variable name="result"> | |
<xsl:call-template name="search-and-replace-whole-words-only"> | |
<xsl:with-param name="input" select="$input" /> | |
<xsl:with-param name="search-string" select="$node[$count]/old" /> | |
<xsl:with-param name="replace-string" select="$node[$count]/new" /> | |
</xsl:call-template> | |
</xsl:variable> | |
<xsl:choose> | |
<xsl:when test="$total = $count"> | |
<xsl:value-of select="$result"/> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:call-template name="replace-node-set"> | |
<xsl:with-param name="input" select="$result" /> | |
<xsl:with-param name="node" select="$node" /> | |
<xsl:with-param name="total" select="$total" /> | |
<xsl:with-param name="count" select="$count + 1" /> | |
</xsl:call-template> | |
</xsl:otherwise> | |
</xsl:choose> | |
</xsl:template> | |
<!-- Find and replace on whole words only --> | |
<xsl:template name="search-and-replace-whole-words-only"> | |
<xsl:param name="input"/> | |
<xsl:param name="search-string"/> | |
<xsl:param name="replace-string"/> | |
<xsl:variable name="punc" | |
select="concat('.,;:( )[ ]!?$@&"',"'")"/> | |
<xsl:choose> | |
<!-- See if the input contains the search string --> | |
<xsl:when test="contains($input,$search-string)"> | |
<!-- If so, then test that the before and after characters are word | |
delimiters. --> | |
<xsl:variable name="before" | |
select="substring-before($input,$search-string)"/> | |
<xsl:variable name="before-char" | |
select="substring(concat(' ',$before),string-length($before) +1, 1)"/> | |
<xsl:variable name="after" | |
select="substring-after($input,$search-string)"/> | |
<xsl:variable name="after-char" | |
select="substring($after,1,1)"/> | |
<xsl:value-of select="$before"/> | |
<xsl:choose> | |
<xsl:when test="(not(normalize-space($before-char)) or | |
contains($punc,$before-char)) and | |
(not(normalize-space($after-char)) or | |
contains($punc,$after-char))"> | |
<xsl:value-of select="$replace-string"/> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:value-of select="$search-string"/> | |
</xsl:otherwise> | |
</xsl:choose> | |
<xsl:call-template name="search-and-replace-whole-words-only"> | |
<xsl:with-param name="input" select="$after"/> | |
<xsl:with-param name="search-string" select="$search-string"/> | |
<xsl:with-param name="replace-string" select="$replace-string"/> | |
</xsl:call-template> | |
</xsl:when> | |
<xsl:otherwise> | |
<!-- There are no more occurrences of the search string so | |
just return the current input string --> | |
<xsl:value-of select="$input"/> | |
</xsl:otherwise> | |
</xsl:choose> | |
</xsl:template> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment