Solved

xslt 1.0 replace text

Posted on 2011-02-14
5
705 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
  • 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

776 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