Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xsl highlight function breaking html tags 1

Status
Not open for further replies.

werD420

Technical User
Sep 14, 2004
181
US
hello,
Ive come across a highlight function that i was able to add to a search feature i have on a page. however the current method it uses will wrap text that is found even inside of html so its making the tags no longer work. has anyone dealt with this type of thing before?

here's the highlighter template
example of problem
$text = <a href=" steps to walkthrough</a>
$what = walk
output=<a href=" style...>walk</font>throughs.com">Simple steps to <font style...>walk</font>through</a>
Code:
<xsl:template name="highlighter">
    <xsl:param name="text"/>
    <xsl:param name="what"/>
    <xsl:variable name="test-text">
      <xsl:call-template name="lower">
        <xsl:with-param name="tolower" select="$text" />
      </xsl:call-template>      
    </xsl:variable>
    <xsl:variable name="test-what">
      <xsl:call-template name="lower">
        <xsl:with-param name="tolower" select="$what" />
      </xsl:call-template>      
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="contains($test-text, $test-what)">
        <xsl:variable name="before" select="substring-before($test-text,$test-what)"/>
        <xsl:variable name="after" select="substring-after($test-text,$test-what)"/>
        <xsl:variable name="real-before" select="substring($text, 1,string-length($before))"/>
        <xsl:variable name="real-after" select="substring($text,string-length($before) + string-length($what) + 1)"/>
        <xsl:value-of select="$real-before" disable-output-escaping="yes"/>
        <font style="background-color:#66FF66"><xsl:value-of select="$what" disable-output-escaping="yes"/></font>
        <xsl:call-template name="highlighter">
          <xsl:with-param name="text" select="$real-after"/>
          <xsl:with-param name="what" select="$what"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text" disable-output-escaping="yes"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

Id appreciate any insight on this. i thought that perhaps i could examine for an unclosed < in before variable and unopened > in after but im not sure what the best way to do this would be

Thanks in advance


MCP, .Net Solutions Development <%_%>
 
this is what im thinking.

the higlighter template uses before term and after

so
$text = <a href=" steps to walkthrough</a>
$what = walk

before1 = <a href="term1 = walk
after1 =throughs.com">Simple steps to walkthrough</a>



im thinking that i can use a reverse function and have
before1rev = . a<
after1rev = >a/<hguorhtklaw ot spets elpmiS>"moc.shguorht

i was thinking i could run some logic off of that to get an intended result but now im just dizzy :S

any thoughts?
Code:
  <xsl:template name="nonhtmlreplace">
    <xsl:param name="before" />
    <xsl:param name="before-reversed"/>
    <xsl:param name="after" />
    <xsl:param name="after-reversed" />
    <xsl:param name="what" />
    <xsl:choose>
      <xsl:when test="contains($before-reversed,'&lt;') and not(contains($before-reversed,'&gt;'))">
        /\<xsl:value-of select="$before" disable-output-escaping="yes"/><xsl:value-of select="$what" disable-output-escaping="yes"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="string-length(substring-before($before-reversed,'&lt;')) &lt; string-length(substring-before($before-reversed,'&gt;'))">
            <xsl:value-of select="$before" disable-output-escaping="yes"/>
            <font style="background-color:#66FF66">
              <xsl:value-of select="$what" disable-output-escaping="yes"/>
            </font>
          </xsl:when>
          <xsl:otherwise>
            -<xsl:value-of select="$before" disable-output-escaping="yes"/>
            <xsl:value-of select="$what" disable-output-escaping="yes"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
    
  </xsl:template>

here's the reverse function im using
Code:
<xsl:template name="reverse">
    <xsl:param name="theString"/>

    <xsl:variable name="thisLength" select="string-length($theString)"/>
    <xsl:choose>
      <xsl:when test="$thisLength = 1">
        <xsl:value-of select="$theString"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="restReverse">
          <xsl:call-template name="reverse">
            <xsl:with-param name="theString"
				 select="substring($theString, 1, $thisLength -1)"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:value-of
          select="concat(substring($theString,
                                                   $thisLength, 
                                                   1
                                                   ) 
                                                    ,$restReverse
                                         )"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>





MCP, .Net Solutions Development <%_%>
 
Using xslt 2.0 would simplify the task. The general look of the renderment would be something like this.
[tt]
<xsl:template name="highlighter">
<xsl:param name="text"/>
<xsl:param name="what"/>
<xsl:variable name="test-text">
<xsl:call-template name="lower">
<xsl:with-param name="tolower" select="$text" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="test-what">
<xsl:call-template name="lower">
<xsl:with-param name="tolower" select="$what" />
</xsl:call-template>
</xsl:variable>
[blue]<xsl:analyze-string select="$test-text" regex="&lt;[^&gt;]*?&gt;">
<xsl:matching-substring>
<xsl:value-of select="." disable-output-escaping="yes" />
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:analyze-string select="." regex="{$test-what}">
<xsl:matching-substring>
<font style="background-color:#66FF66"><xsl:value-of select="$test-what" disable-output-escaping="yes"/></font>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="." />
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:non-matching-substring>
</xsl:analyze-string>[/blue]
</xsl:template>
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top