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 May 20th, 2013, 10:21 AM
Authorized User
 
Join Date: Apr 2013
Posts: 31
Thanks: 3
Thanked 0 Times in 0 Posts
Unhappy Converting straight quotes to smart quotes in xslt 1.0

Hi,
I've the below xml

HTML Code:
<para>A number of the offences set out in the Companies Ordinance are expressed to apply to "officers" of the company. "Officer" includes directors, managers and the company secretary: Companies Ordinance, s.2(1).</para>
here actually the in the input the quotes given are " but i want to convert it to smart quotes. i used the below xslt for this.

HTML Code:
<xsl:template match="para/text()">
<xsl:when test="contains(.,$quot)">
<xsl:value-of select="translate(.,$quot,'“')"/>
<xsl:value-of select="translate(substring-after(.,$quot),$quot,'”')"/>
</xsl:when>
</xsl:template>
but i am getting below ooutput.

HTML Code:
A number of the offences set out in the Companies Ordinance are expressed to apply to “officers“ of the company. “Officer“ includes directors, managers and the company secretary: Companies Ordinance, s.2(1).
but i want to get it as below.
HTML Code:
A number of the offences set out in the Companies Ordinance are expressed to apply to “officers” of the company. “Officer” includes directors, managers and the company secretary: Companies Ordinance, s.2(1).
please let me know how do i solve this.


Thanks
 
Old May 20th, 2013, 11:19 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Well, for starters, I wouldn't attempt this in XSLT 1.0 unless I really had to. 2.0 is so much better for this kind of task.

But whatever your programming language, you need to specify the task. Are you going to assume that the quotes match properly in pairs, and that odd-numbered instances can be replaced by opening quote and even-numbered instances by closing quote? Or do you want some smarter validation?

I think you need a recursive template something like this:

Code:
<xsl:variable name="q">"</xsl:variable>
<xsl:variable name="smart-quotes">`'</xsl:variable>
<xsl:template name="replace-quotes">
  <xsl:param name="in" select="false()"/>
  <xsl:param name="count" select="1"/>
  <xsl:choose>
    <xsl:when test="contains($in, $q)">
      <xsl:value-of select="substring-before($in, $q)"/>
      <xsl:value-of select="substring($smart-quotes, $count mod 2 + 1, 1)"/>
      <xsl:call-template name="replace-quotes">
         <xsl:with-param name="in" select="substring-after($in, $q)"/>
         <xsl:with-param name="count" select="$count + 1"/>
      </
    </
    <xsl:otherwise>
      <xsl:value-of select="$in"/>
    </
  </
</
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
replace straight quotes with curley quotes sunnykeerthi XSLT 1 May 6th, 2013 03:14 PM
Is there any function in XSLT to handle double quotes LeoMathew XSLT 1 September 22nd, 2010 06:03 AM
displaying single quotes and double quotes ren_123 Classic ASP Databases 2 February 22nd, 2006 02:17 PM
Double Quotes langer123 Classic ASP Basics 2 March 16th, 2005 06:22 PM
Double Quotes and Single Quotes Problem phungleon Classic ASP Basics 7 May 27th, 2004 01:44 PM





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