Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XML
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 23rd, 2003, 03:07 PM
Authorized User
 
Join Date: Dec 2003
Location: , , USA.
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to AyatKh
Default Search and Replace a text in XML file

I want to be able to find a text in the XML file and replace it with something else(Text). Does anybody know how to do that?
Thanks

Reply With Quote
  #2 (permalink)  
Old December 24th, 2003, 03:30 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Nor Hachin, Kotayk, Armenia.
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Quote:
quote:Originally posted by AyatKh
 I want to be able to find a text in the XML file and replace it with something else(Text). Does anybody know how to do that?
Thanks

You can do it with XSLT. Post your XML document and describe what you want to replace by what. There are couple of methods, but each is good in concrete situation, so we have to see your XML.

Regards,
Armen
Reply With Quote
  #3 (permalink)  
Old December 24th, 2003, 12:37 PM
Authorized User
 
Join Date: Dec 2003
Location: , , USA.
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to AyatKh
Default

For example I need to replace "Barnes And Noble" with "Test" in this code. what is the best way to search for that text? Which object I should use?

<book genre="novel" ISBN="1-861001-57-5">
  <title>Pride And Prejudice</title>
  <seller>Barnes And Noble</seller>
  <price>$45</price>
</book>

Reply With Quote
  #4 (permalink)  
Old December 25th, 2003, 01:14 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Nor Hachin, Kotayk, Armenia.
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Quote:
quote:Originally posted by AyatKh
 For example I need to replace "Barnes And Noble" with "Test" in this code. what is the best way to search for that text? Which object I should use?

<book genre="novel" ISBN="1-861001-57-5">
  <title>Pride And Prejudice</title>
  <seller>Barnes And Noble</seller>
  <price>$45</price>
</book>

You can achieve this by processing the DOM tree(by parsing the document using TrAX and then manipulate the DOM tree using Java, for example). Another approach is a stylesheet:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="what" select="'Barnes And Noble'"/>
    <xsl:param name="by-what" select="'Test'"/>
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="node() | @*">
        <xsl:choose>
            <xsl:when test="self::text()">
                <xsl:variable name="this-text" select="self::text()"/>
                <xsl:choose>
                    <xsl:when test="contains($this-text, $what)">
                        <xsl:call-template name="replace">
                            <xsl:with-param name="str" select="$this-text"/>
                            <xsl:with-param name="what" select="$what"/>
                            <xsl:with-param name="by-what" select="$by-what"/>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:copy-of select="."/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates select="node() | @*"/>
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="replace">
        <xsl:param name="str"/>
        <xsl:param name="what"/>
        <xsl:param name="by-what"/>

        <xsl:choose>
            <xsl:when test="not($str)"/>
            <xsl:when test="starts-with($str, $what)">
                <xsl:value-of select="$by-what"/>
                <xsl:call-template name="replace">
                    <xsl:with-param name="str" select="substring($str, string-length($what)+1)"/>
                    <xsl:with-param name="what" select="$what"/>
                    <xsl:with-param name="by-what" select="$by-what"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="substring($str, 1, 1)"/>
                <xsl:call-template name="replace">
                    <xsl:with-param name="str" select="substring($str, 2)"/>
                    <xsl:with-param name="what" select="$what"/>
                    <xsl:with-param name="by-what" select="$by-what"/>                
                </xsl:call-template>    
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
This stylesheet replaces all occurences of string kept in variable $what with a string kept in variable $by-what in all text nodes.

Regards,
Armen
Reply With Quote
  #5 (permalink)  
Old December 25th, 2003, 02:48 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , Denmark.
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi armmati!
That's a lot of code for a Search/Replace. Wouldn't it be possible to have such a function available in another file to be called when needed?

Thanks in advance
Reply With Quote
  #6 (permalink)  
Old December 25th, 2003, 08:33 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Nor Hachin, Kotayk, Armenia.
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Quote:
quote:Originally posted by Birger
 Hi armmati!
That's a lot of code for a Search/Replace. Wouldn't it be possible to have such a function available in another file to be called when needed?

Thanks in advance
If you want to search the whole XML document, this code is quite small :). If you know where your text can be found exactly (that is, the parent node of the text node containing the text you want to replace; it's the element "seller" in your example), then you can avoid walking through the whole document and perform replacements. Instead, you can get the content of the text node and call the "replace" template.
Or if you don't want XSLT, you have to write recursive method, in Java for example, (if you still want to search the whole XML doc) which again will walk through the whole tree and perform appropriate replacements.
About keeping the "replace" template in a separate file: it's a good idea of course, since the template is general; you can freely move the template into separate file, say replace-string.xsl, then include it in the main stylesheet.

Regards,
Armen
Reply With Quote
  #7 (permalink)  
Old December 25th, 2003, 02:33 PM
Authorized User
 
Join Date: Oct 2003
Location: , PA, USA.
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I had a similar problem that I chose to solve by maintaining a database of strings to be searched for and the corresponding strings to replace with. However, in my situation, I had a *string*, i.e. not an entire XML document, that could contain any number of occurrences of certain strings that had to be replaced with other strings. So I chose to create a database, for example:

<SubstitutionTable>
    <CharacterSubstitutions>
        <SearchString>BadString1</SearchString>
        <ReplaceString>GoodString1</ReplaceString>
    </CharacterSubstitutions>
    <CharacterSubstitutions>
        <SearchString>BadString2</SearchString>
        <ReplaceString>GoodString2</ReplaceString>
    </CharacterSubstitutions>
etc
</SubstitutionTable>

I then applied templates selecting the database above (changing the context node to the first occurrence of CharacterSubstitutions) and passing the string to be searched as a parameter, and walked along the search database calling a named template similar to what Armen wrote to perform systematic substitutions of SearchString with ReplaceString through the whole database.

Perhaps some combination of the stylesheets Armen provided and the use of a database similar to above will solve your problem.

Happy Holidays,

…sam


Reply With Quote
  #8 (permalink)  
Old December 25th, 2003, 03:52 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , Denmark.
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks both for your comments. I'll have it in mind when reaching that level, shortly I hope.
Merry Christmas :D
Reply With Quote
  #9 (permalink)  
Old December 26th, 2003, 10:01 AM
Authorized User
 
Join Date: Dec 2003
Location: , , USA.
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to AyatKh
Default

Hi Armen,
Thank you very much for your great response. I think this is the best time to ask your advice for my project.

Right now our users are dealing with downloading more than 200 word2002 files from our application. what it does is it search for some text fields in the word file and replace those fields with related data from Database. The problem is it's so slow. So we want to convert those file into Word2003 and save them as XML file so that the search and replace would be easier. Do you think using XML and XSLT for doing this search and replace is the best solution?
Thanks
Ayat

Reply With Quote
  #10 (permalink)  
Old December 26th, 2003, 03:02 PM
Authorized User
 
Join Date: Dec 2003
Location: , , USA.
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to AyatKh
Default

I will be thankful if everybody can give their suggestion.
Happy Holidays!

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP - Find text / Replace with email link (XML or peakfx XML 1 July 13th, 2007 09:07 PM
XSL to replace text in XML file neetukk XSLT 1 November 29th, 2006 04:20 AM
Global Search&Replace Text in all fields in sql db buddyz SQL Server 2000 10 September 14th, 2006 08:24 AM
Binary Search on a text file scoobie Pro Java 1 August 25th, 2006 12:43 AM
How to replace elements(from existed XML file)? hbcontract XML 1 October 30th, 2003 04:49 AM



All times are GMT -4. The time now is 01:40 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.