Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT 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
 
Old June 20th, 2011, 04:48 PM
Authorized User
 
Join Date: Apr 2011
Posts: 22
Thanks: 4
Thanked 0 Times in 0 Posts
Default Find and replace in entire doc

Hello XSLT Gurus,
I need to find for a particular string and replace it with another in the entire document. How to do this?

The catch is, I need this to be done dynamically, i.e i need to run a xpath evaluation before i start transforming.

Input XML:
<Alpha><A><B>Hello</B><C replace="true" toReplace="Hello" replaceValue="Hi" /></Alpha>

Output should be:
<Alpha><A><B>Hi</B><C replace="true" toReplace="Hi" replaceValue="Hi" /></Alpha>

I need to replace all occurrences of "Hello" with "Hi" (according to the Input), irrespective of where it occurs in the document.

Can XSLT do this for me?
 
Old June 20th, 2011, 06:14 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Doing the global replace is easy, provided the strings are not split across multiple text nodes. Just write an identity transformation that uses the identity template for elements:

Code:
<xsl:template match="*" mode="replace">
  <xsl:copy><xsl:apply-templates select="@*|node()" mode="replace"/></xsl:copy>
</xsl:template>
and then does the replacement for attributes and text node:

Code:
<xsl:template match="text()" mode="replace">
  <xsl:param name="from" as="xs:string" tunnel="yes"/>
  <xsl:param name="to" as="xs:string" tunnel="yes"/>
  <xsl:value-of select="replace(., $from, $to)"/>
</xsl:template>
<xsl:template match="@*" mode="replace">
  <xsl:param name="from" as="xs:string" tunnel="yes"/>
  <xsl:param name="to" as="xs:string" tunnel="yes"/>
  <xsl:attribute name="{name()}" select="replace(., $from, $to)"/>
</xsl:template>
Then fire the thing off like this:
Code:
<xsl:apply-templates select="$input" mode="replace">
   <xsl:with-param name="from" as="xs:string" tunnel="yes" select="'Hello'"/>
   <xsl:with-param name="to" as="xs:string" tunnel="yes" select="'Hi'"/>
</xsl:apply-templates>
All that remains is working out how to set the initial parameters - you haven't given enough detail to help with that.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old June 20th, 2011, 11:29 PM
Authorized User
 
Join Date: Apr 2011
Posts: 22
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Hi Kay, Thanks for responding. I was sure Identity transform was my only option, since i had copy all my input XML except for one.

But can you please explain how this code would work?

The first template is used to copy everything, except text and attributes.

When it comes to text() and attributes, you run a replace before actually copying them.

I am not sure on what the last one is for, and also I am not sure how i make it dynamic and not statically typed.

Thanks in advance.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and replace? Stuart Stalker SQL Server 2000 8 October 13th, 2005 02:49 AM
Open Word Doc from Access - find, find next save donaldmaloney Access VBA 1 May 25th, 2005 11:09 AM
can't find the regionstext.doc mjafferi Dreamweaver (all versions) 3 March 2nd, 2004 04:10 AM
Create a find and a find and replace in VB.NET snowy0 VB How-To 0 January 26th, 2004 07:03 PM





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