Solved

xslt 1.0 replace text

Posted on 2011-02-14
5
736 Views
Last Modified: 2013-11-18
I use the suggestion from ticket 24821667 on how to replace text with xslt 1.0, the problem it removes html code without me specifing it in the text to replace. any idea why?
0
Comment
Question by:itinfserv
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 34889315
Maybe you need to post both XSLT and XML as attachments.
Without further specification we don't know what is wrong

Note that this works on text replacements,
your HTML tags likely are gone because that are element nodes who are not copied
0
 
LVL 1

Author Comment

by:itinfserv
ID: 34895900
I use the following template to replace a string
        <xsl:template name="replace-str">
            <xsl:param name="str"/>
            <xsl:param name="find"/>
            <xsl:param name="replace"/>
            <xsl:choose>
                <xsl:when test="contains($str, $find)">
                    <xsl:value-of select="substring-before($str, $find)"/>
                    <xsl:value-of select="$replace"/>
                    <xsl:call-template name="replace-str">
                        <xsl:with-param name="str" select="substring-after($str, $find)"/>
                        <xsl:with-param name="find" select="$find"/>
                        <xsl:with-param name="replace" select="$replace"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$str"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>

Open in new window

0
 
LVL 1

Author Comment

by:itinfserv
ID: 34895917
and I try to replace the code for this XML, this is only portion of the XML
<S>
<b>...</b>
<b>Staff</b>
 directory. Please email amendments to your entry in the 
<b>staff</b>
 directory
<br/>
 to Oracle. Useful number: IS Service Desk: 012 2182 
<b>...</b>
</S>

Open in new window


The problem when I used the template all the <b> and <br> was scraped.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 34895954
I need more of your XSLT
As I allready expressed earlier seems indeed to be the cause of this problem

If you pass the content of S to the named template,
all the text nodes will be concatenated and sent in the replace functionality
and you loose all the <b>

what you need to do is apply-templates for all nodes at the S level
and call the replacing functionality on both S/text() as S/b/text()

If you post more of your XSLT (maybe the template for S)
I can fix it
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 34896038
to give more detail

    <xsl:template match="S">
        <xsl:call-template name="replace-str">
            <xsl:with-param name="find" select="'a'"/>
            <xsl:with-param name="replace" select="'FOO'"/>
            <xsl:with-param name="str" select="."/>
        </xsl:call-template>
    </xsl:template>

select="." in the param $str will flatten the text-nodes out

Here is how to fix that
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
    <xsl:template match="S">
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="S/*">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="S/*/text() | S/text()">
        <xsl:call-template name="replace-str">
            <xsl:with-param name="find" select="'a'"/>
            <xsl:with-param name="replace" select="'FOO'"/>
            <xsl:with-param name="str" select="."/>
        </xsl:call-template>
    </xsl:template>
    
    <xsl:template name="replace-str">
        <xsl:param name="str"/>
        <xsl:param name="find"/>
        <xsl:param name="replace"/>
        <xsl:choose>
            <xsl:when test="contains($str, $find)">
                <xsl:value-of select="substring-before($str, $find)"/>
                <xsl:value-of select="$replace"/>
                <xsl:call-template name="replace-str">
                    <xsl:with-param name="str" select="substring-after($str, $find)"/>
                    <xsl:with-param name="find" select="$find"/>
                    <xsl:with-param name="replace" select="$replace"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$str"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0

Featured Post

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question