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 July 27th, 2010, 03:46 PM
Registered User
 
Join Date: Jul 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using Replace template

I need to use this replace template 3 times for the following scenarios
replace ' with '
replace " with "
replace \ with \

The template I have below, I was only able to get it to work for one replace. I need it to handle all scenarios. requesterInfo\name contain both a ' and \
Code:
<request>
	<requestType/>
	<propertyID/>
	<EzbuyPO>4400000091</EzbuyPO>
	<UNSPSC/>
	<transit/>
	<category1/>
	<category2/>
	<category3/>
	<requesterInfo>
		<name>T. USER' 1\</name>
		<phone>01 01 01 01</phone>
		<email>[email protected]</email>
	</requesterInfo>
        </request>
Need it to run through this XSLT

Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" omit-xml-declaration="yes" media-type="text/xml" indent="yes" encoding="UTF-8"/>
	<xsl:strip-space elements="*"/>
	<xsl:template match="/">
		<xsl:for-each select="child::*">
			<xsl:call-template name="replace-string">
				<xsl:with-param name="text">
					<xsl:value-of select="ancestor-or-self::*"/>
				</xsl:with-param>
				<xsl:with-param name="replace">'</xsl:with-param>
				<xsl:with-param name="with">'</xsl:with-param>
			</xsl:call-template>
		</xsl:for-each>
	</xsl:template>

	<xsl:template name="replace-string">
		<xsl:param name="text"/>
		<xsl:param name="replace"/>
		<xsl:param name="with"/>
		<xsl:choose>
			<xsl:when test="contains($text,$replace)">
				<xsl:value-of select="substring-before($text,$replace)"/>
				<xsl:value-of select="$with"/>
				<xsl:call-template name="replace-string">
					<xsl:with-param name="text" select="substring-after($text,$replace)"/>
					<xsl:with-param name="replace" select="$replace"/>
					<xsl:with-param name="with" select="$with"/>
				</xsl:call-template>
			</xsl:when>			
			<xsl:otherwise>
				<xsl:value-of select="$text"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>
 
Old July 28th, 2010, 03:12 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Firstly, in XML there is no actual difference between " and &quot; - they are different representations of the same character - a bit like saying 101 in binary is different from 5 in base 10 - there is no difference.

Secondly, I don't know if it is the forum software but you appear to have written that you want to replace a single quote with itself, and a forward slash with itself - i.e. no change.

Finally, to pass the value from a template to a template simply do something like this:

Code:
<xsl:call-template name="template">
  <xsl:with-param name="first">
    <xsl:call-template name="template">
      <xsl:with-param name="first" select="'A'"/>
      <xsl:with-param name="second" select="'B'"/>
    </xsl:call-template>
  </xsl:with-param>
  <xsl:with-param name="second" select="'C'"/>
</xsl:call-template>
Finally, I'm not sure why you've written "ancestor-or-self::*" when "." would do the same (with this particular XML) and would likely work better for different examples of XML.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old July 28th, 2010, 09:46 AM
Registered User
 
Join Date: Jul 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I need the output to contain those characters is that possible?.. When I do it in stylus studio it looks the same, doesn't put the hex representation?
 
Old July 28th, 2010, 09:55 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

No, and why would you want to, seeing as they are identical.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old July 28th, 2010, 09:57 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I suspect the forum software has mangled your post. I can't believe you actually want to replace backslash with backslash. Please try to explain the requirement again - spell out the characters or strings wanted if necessary, or putting them within [ code ] tags as advised should solve the problem
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old July 28th, 2010, 10:00 AM
Registered User
 
Join Date: Jul 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The target application can't read " or ' or \

Code:
replace ' with ""& #039;""
replace " with &quot;
replace \ with ""& #092;""

Last edited by cshah; July 28th, 2010 at 10:11 AM..
 
Old July 28th, 2010, 10:11 AM
Registered User
 
Join Date: Jul 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

there is no space betwen & and #
 
Old July 29th, 2010, 12:37 PM
Registered User
 
Join Date: Jul 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by cshah View Post
The target application can't read " or ' or \

Code:
replace ' with ""& #039;""
replace " with &quot;
replace \ with ""& #092;""
Ok i figure the best way to replace ' will be with this &amp;#039;

Now stuck on call the template once to handle all replaces at once.





Similar Threads
Thread Thread Starter Forum Replies Last Post
String Replace Template w/out Outputting tclotworthy XSLT 4 September 26th, 2007 03:38 PM
replace ' and " with \' and \" Brian Campbell XSLT 4 May 24th, 2006 10:49 AM
calling one template in other template VijayKumar XSLT 3 September 15th, 2005 11:12 AM
Replace crmpicco VB How-To 15 May 20th, 2005 01:34 PM
replace() Missy_K Javascript 1 January 16th, 2004 11:17 AM





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