[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: How to Replace Special Characters ?

Subject: Re: How to Replace Special Characters ?
From: Mike Brown <mike@xxxxxxxx>
Date: Thu, 8 Aug 2002 10:50:34 -0600 (MDT)
replace special characters in xml
Rohitav Samanta wrote:
> To get rid of the special characters I have written the following template.
> 
> <xsl:call-template name="transformXMLString">
> <xsl:with-param name="StringToTransform">
> <xsl:value-of select="//WorkHistory/xml/rs:data/z:row@Responsibilities" disable-output-escaping="yes"/>
> </xsl:with-param>
> </xsl:call-template>

1. disable-output-escaping has no effect here. It only affects serialization
of text nodes in the result tree. You aren't serializing anything (not
producing output).

2. You can also remove the value-of and put the select in the with-param.

3. When you select multiple nodes, value-of only looks at the first one,
so you should try not to select more than the one you need.

4. '//' is rarely necessary unless you really need every WorkHistory element
and you have no idea where those elements could be, relative to either the
root node or where you are now.

>       <xsl:when test="contains($StringToTransform,'&#x26;#8220;')">
>         <xsl:value-of select="translate($StringToTransform,'&#x26;#8220;','&quot;')" />
>       </xsl:when>

5. What you are testing for is the string '&#8220;' (7 characters)
but your XML has been parsed.. those 7 characters no longer exist
in the data. It is now one character: Unicode character number 8220.

6. You do not need a recursive template at all. The solution is simply to use
translate() once: (replace $foo with the actual string to act upon)

<xsl:variable name="qaa">""''</xsl:variable>
<xsl:value-of select="translate($foo,'&#8220;&#8221;&#8216;&#8217;',$qaa)"/>

> <!-- string contains linefeed -->
>       <xsl:when test="contains($StringToTransform,'&#10;')">
>          <xsl:value-of select="substring-before($StringToTransform,'&#10;')" />
>          <br></br>

OK, for this you do need a recursive template.

7. (your solution) Use the techique I demonstrate at
http://skew.org/xml/stylesheets/linefeed2br/

The lf2br template takes a string in and returns a result tree fragment
(special node tree) out. So thereafter you must not treat it like a 
string; if you put it in a variable, use xsl:copy-of to retrieve it,
not xsl:value-of.

I would do the translate() first, and pass that string in as the
$StringToTransform, like this:

<xsl:call-template name="lf2br">
  <xsl:with-param name="StringToTransform" select="translate($foo,'&#8220;&#8221;&#8216;&#8217;',$qaa)"/>
</xsl:call-template>

> <!-- string contains carriage return -->
>       <xsl:when test="contains($StringToTransform,'&#13;')">

8. (FYI) You do not ever need to test for CR because all CR+LF and CR are
normalized to LF during XML parsing. The only exception is in attribute values
where the character reference "&#13;" or "&#xD;" is used, in which case you do
get the CR, if I recall correctly (it's a very rarely used feature).

   - Mike
____________________________________________________________________________
  mike j. brown                   |  xml/xslt: http://skew.org/xml/
  denver/boulder, colorado, usa   |  resume: http://skew.org/~mike/resume/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.