Solved

xslt 1.0 replace text

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now