p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XML (http://p2p.wrox.com/forumdisplay.php?f=91)
-   -   Search and Replace a text in XML file (http://p2p.wrox.com/showthread.php?t=7595)

AyatKh December 23rd, 2003 04:07 PM

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


armmarti December 24th, 2003 04:30 AM

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

AyatKh December 24th, 2003 01:37 PM

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>


armmarti December 25th, 2003 02:14 AM

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

Birger December 25th, 2003 03:48 AM

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

armmarti December 25th, 2003 09:33 AM

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

shbyland December 25th, 2003 03:33 PM

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



Birger December 25th, 2003 04:52 PM

Thanks both for your comments. I'll have it in mind when reaching that level, shortly I hope.
Merry Christmas :D

AyatKh December 26th, 2003 11:01 AM

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


AyatKh December 26th, 2003 04:02 PM

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



All times are GMT -4. The time now is 02:46 PM.

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