Solved

xslt 1.0 replace text

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Merge 2 XML Files together 5 47
How do i send an api key in a request header 3 57
XML & .net 5 41
Transform XML to Excel using XSL 1 23
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

920 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

15 Experts available now in Live!

Get 1:1 Help Now