[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Usage of XSLT in the field of text replacement.


Subject: Re: [xsl] Usage of XSLT in the field of text replacement.
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 09 Oct 2008 08:12:06 -0400

At 2008-10-09 10:43 +0530, Mukul Gandhi wrote:
Here is a 2.0 solution for this (let's say this file is test.xsl),

I think, Mukul, the original poster should be aware that with your proposed solution the "temp.ini" file must express the "old" string value as a regular expression and not a simple string.


I think a safer solution would be to use simple substring-before() and substring-after() in recursion ... when I considered what to post as a solution I felt the original poster did not necessarily have control over the "old" string values in the control file. If there are sensitive regular expression characters in those values the results may not be as desired.

Then again, the original poster may want the power of regular expressions in the control file, in which case what you have presented is almost complete, but it does not give access to any grouping parentheses when specifying the "new" string value. For example, had you used replace() instead of analyze string the original poster would get access to "$" in the replacement string.

There are so many possible ways to approach the solution based on what the original poster wanted that I felt his request was unclear. But if you just look on the face of it, then I think the strings in "temp.ini" are not expected to be regular expressions, in which case the use of either <xsl:analyze-string> or replace() would be out of place for fear of unexpected results.

Then again perhaps using a simple replace() to protect the old string and new string by escaping any sensitive regular expression characters brings your solution to the fore again. Determine which characters are regular-expression-sensitive, escape any found in the "old" string and "new" string, and then walk through the string as you have done with <xsl:analyze-string/>.

I hope this helps.

. . . . . . . . . . . Ken

...
  <xsl:variable name="regex-pattern" select="string-join(for $x in
$replacement-data return (if
(starts-with(substring-before($x,'|'),'[') and
ends-with(substring-before($x,'|'),']')) then
concat('\',substring(substring-before($x,'|'),0,string-length(substring-before($x,'|'))),'\]')
else substring-before($x,'|')),'|')" />
...
      <xsl:analyze-string select="."
                          regex="{$regex-pattern}">
...
temp.ini

1|ONE
2|TWO
3|THREE
50|FOUR
[text]|[TEXT]
...
On Wed, Oct 8, 2008 at 4:09 PM, J. S. Rawat <jrawat@xxxxxxxxxxxxxx> wrote:
> Hi
> Can xslt2.0 play a role in the field of text replacement. I want to
> transform an xml to another xml and want to replace some text as well. For
> that purpose I want to restore "find and replace" text in a separate file
> with pipe separated delimeter.



-- Upcoming XSLT/XSL-FO hands-on courses: Wellington, NZ 2009-01 Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video sample lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg Video course overview: http://www.youtube.com/watch?v=VTiodiij6gE G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal


Current Thread
Keywords
xml