This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: How to Replace Special Characters ?


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]