Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: [xsl] Search and Replace to add HTML tags

From: "G. Ken Holman" <gkholman@----------------.--->
To: xsl-list@-----.------------.---
Date: 6/30/2009 5:40:00 PM
At 2009-06-30 13:30 -0400, Sharon_Harris@u... wrote:
>I am trying to wrap specific text in a string within HTML tags. For
>example, if a strings contains the word "Note:", then I want to enclose
>this word within a set of bold tags (<b>Note:</b>). I do not have control
>over the XML data source and cannot modify the structure/schema.
>
>I have tried using the replace function to search for the specific text and
>then replace it with a defined variable.

The replace() function only works with strings, not with nodes.

>The variable contains the text
>wrapped within the HTML tags. Unfortunately, only the content within the
>variable is getting pulled and the HTML tags are being ignored.

Correct ... because the arguments to replace() are cast to strings.

>Can anyone
>tell me how to get the tags added as well during the search and replace?

By analyzing the string rather than using replace.

>XML excerpt:
>    <Row>
>    <ReleaseNote>Before: The application did this. Now: The application does
>    this. Note: The application may also do this.</ReleaseNote>
>    </Row>
>    <Row>
>    <ReleaseNote>Before: The application did not do this. Now: The
>    application does this.</ReleaseNote>
>    </Row>
>
>XSLT excerpt:
>    <xsl:variable name="NoteBold"><b>Note:</b></xsl:variable>
>    <xsl:variable name="BeforeBold"><b>Before:</b></xsl:variable>
>    <xsl:variable name="NowBold"><b>Now:</b></xsl:variable>
>
>    <xsl:template match="text()">
>       <xsl:value-of select="replace(replace(replace(.,'Note:',$NoteBold),
>    'Before:',$BeforeBold), 'Now:', $NowBold)"/>
>    </xsl:template>

This is an incorrect approach because you are matching *all* text 
nodes in the *entire* document and doing the replace on *every* piece 
of text you have.

I hope the example below helps ... you have a very straightforward 
requirement because you are only wrapping text with a bold element 
node, not massaging the text.

. . . . . . . . . . . Ken

T:\ftemp>type sharon.xml
<test>
    <Row>
    <ReleaseNote>Before: The application did this. Now: The application does
    this. Note: The application may also do this.</ReleaseNote>
    </Row>
    <Row>
    <ReleaseNote>Before: The application did not do this. Now: The
    application does this.</ReleaseNote>
    </Row>
</test>

T:\ftemp>call xslt2 sharon.xml sharon.xsl
<?xml version="1.0" encoding="UTF-8"?><test>
    <Row>
    <ReleaseNote><b>Before:</b> The application did this. <b>Now:</b> 
The applica
tion does
    this. <b>Note:</b> The application may also do this.</ReleaseNote>
    </Row>
    <Row>
    <ReleaseNote><b>Before:</b> The application did not do this. 
<b>Now:</b> The
    application does this.</ReleaseNote>
    </Row>
</test>
T:\ftemp>type sharon.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 version="2.0">

<xsl:template match="ReleaseNote">
   <xsl:copy>
     <xsl:analyze-string select="." regex="Before:|Note:|Now:">
       <xsl:matching-substring>
         <b><xsl:value-of select="."/></b>
       </xsl:matching-substring>
       <xsl:non-matching-substring>
         <xsl:value-of select="."/>
       </xsl:non-matching-substring>
     </xsl:analyze-string>
   </xsl:copy>
</xsl:template>

<xsl:template match="@*|node()"><!--identity for all other nodes-->
   <xsl:copy>
     <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>


--
Possible July/August XSLT/XQuery/XSL-FO training in Oakland/CA/USA
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman@C...
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe@l...>
--~--



transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent