2009-09-02 6 views
1
私はプログラム的に

どのタイポグラファーの重引用符(別名カーリー引用符)で二重引用符を置き換えるために

私の最初の考えは、このようなものであるタイポグラファーの重引用符で"通常の二重引用符"を交換する必要があり

 <xsl:variable name="text"> 
      <xsl:call-template name="replace-string"><!-- FYI: replace-string is a custom method that works like you would expect--> 
       <xsl:with-param name="text" select="."/> 
       <xsl:with-param name="replace" select="string(' &quot;')" /><!-- left quote because of space before --> 
       <xsl:with-param name="with" select="string('“')"/> 
      </xsl:call-template> 
     </xsl:variable> 

     <xsl:variable name="text2"> 
      <xsl:call-template name="replace-string"> 
       <xsl:with-param name="text" select="$text"/> 
       <xsl:with-param name="replace" select="string('&quot; ')" /><!-- right quote because of space after --> 
       <xsl:with-param name="with" select="string('”')"/> 
      </xsl:call-template> 
     </xsl:variable> 

     <xsl:apply-templates select="$text2" /> 

私の心配は、見積もりによって決定スペースがない状況です。これらのような。

"これは素晴らしい"と言います。 私はトラは好きです( "大きな猫")。

適用するルールや別の戦略を知っている人は誰もいませんか?

ありがとうございます!

答えて

4

ソリューション拡張機能なしで動作するものは、

<xsl:template name="typograpic-quotes"> 
    <xsl:param name="text" select="''" /> 
    <xsl:param name="quote" select="'&quot;'" /> 
    <xsl:param name="open" select="'“'" /> 
    <xsl:param name="close" select="'”'" /> 
    <xsl:param name="inquote" select="false()" /> 

    <xsl:choose> 
    <xsl:when test="contains($text, $quote)"> 
     <xsl:value-of select="substring-before($text, $quote)" /> 
     <xsl:choose> 
     <xsl:when test="$inquote"> 
      <xsl:value-of select="$close" /> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$open" /> 
     </xsl:otherwise> 
     </xsl:choose> 
     <xsl:call-template name="typograpic-quotes"> 
     <xsl:with-param name="text" select="substring-after($text, $quote)" /> 
     <xsl:with-param name="quote" select="$quote" /> 
     <xsl:with-param name="open" select="$open" /> 
     <xsl:with-param name="close" select="$close" /> 
     <xsl:with-param name="inquote" select="not($inquote)" /> 
     </xsl:call-template> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:value-of select="$text" /> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

入力の不均衡な引用符でこれが失敗することは言うまでもない。

+0

助けてくれてありがとう! – joe

3

正規表現。

regex:replace($textVariable, '&quot;([^&quot;]*)&quot;' , 'gi', '“$1”') 

Haventはこれをテストしましたが、これは簡単な方法です。あなたはすべて一致しています("0回以上"と入力し、他の印刷見積もりと置き換えます$ 1は最初のマッチのバックリファレンスですが、間違ってネストされたテキスト、など、閉じていない引用符は、私はあなたがこのいずれかの正規表現を書き換えると、それをテストする。このような何かを示唆しています。

これはあなたのXSLTプロセッサがEXSLT拡張をサポートしていることを想定している。

+0

助けてくれてありがとう! regeditは私がレギュレータでテストするときに動作するようです。 – joe

+0

Regex(正規表現)ではなく、regedit(レジストリエディタ)! – Eric

0

私たちはいくつかの不均衡な引用符を持っているので。私はもう少し実用的なものに取り組んでいます。以下は、私が試したすべてのケースで機能するようです。

<!-- Get text. All quotes defaulted to right quote --> 
     <xsl:variable name="text"> 
      <xsl:call-template name="replace-string"> 
       <xsl:with-param name="text" select="."/> 
       <xsl:with-param name="replace" select="string('&quot;')" /> 
       <xsl:with-param name="with" select="string('”')"/> 
      </xsl:call-template> 
     </xsl:variable> 

     <!-- Turn quotes preceded by a space into left quote --> 
     <xsl:variable name="text2"> 
      <xsl:call-template name="replace-string"> 
       <xsl:with-param name="text" select="$text"/> 
       <xsl:with-param name="replace" select="string(' ”')" /> 
       <!-- right quote because of space after --> 
       <xsl:with-param name="with" select="string(' “')"/> 
      </xsl:call-template> 
     </xsl:variable> 

     <!-- Turn quotes preceded by a parenthesy into left quote --> 
     <xsl:variable name="text3"> 
      <xsl:call-template name="replace-string"> 
       <xsl:with-param name="text" select="$text2"/> 
       <xsl:with-param name="replace" select="string('(”')" /> 
       <!-- right quote because of space after --> 
       <xsl:with-param name="with" select="string('(“')"/> 
      </xsl:call-template> 
     </xsl:variable> 

     <!-- Turn quotes that are the first character in the text into left quote --> 
     <!-- Note: this one is still a little funky. For some reason the first character is always whitespace. So I am checking the second character because it is really the first. --> 
     <xsl:variable name="text4" > 
      <xsl:choose> 
       <xsl:when test="normalize-space(substring($text3, 2, 2)) = string('”')"> 
        <xsl:value-of select="string('“')"/> 
        <xsl:value-of select="substring($text3, 3)"/> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:value-of select="$text3"/> 
       </xsl:otherwise> 
      </xsl:choose> 
     </xsl:variable> 

     <xsl:apply-templates select="$text4" /> 
+0

ご意見がありがとうございます – joe

関連する問題