Solved

xslt 1.0 replace text

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Animated .jpg? 13 114
PowerShell script to remove string in xml file 8 69
XML error not understood 5 27
ASP AND XML 3 41
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

739 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