 |
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

December 23rd, 2003, 04:07 PM
|
|
Authorized User
|
|
Join Date: Dec 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

December 24th, 2003, 04:30 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

December 24th, 2003, 01:37 PM
|
|
Authorized User
|
|
Join Date: Dec 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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>
|
|

December 25th, 2003, 02:14 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

December 25th, 2003, 03:48 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

December 25th, 2003, 09:33 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

December 25th, 2003, 03:33 PM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

December 25th, 2003, 04:52 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks both for your comments. I'll have it in mind when reaching that level, shortly I hope.
Merry Christmas :D
|
|

December 26th, 2003, 11:01 AM
|
|
Authorized User
|
|
Join Date: Dec 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

December 26th, 2003, 04:02 PM
|
|
Authorized User
|
|
Join Date: Dec 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I will be thankful if everybody can give their suggestion.
Happy Holidays!
|
|
 |