0

私のDBMSがアポストロフィ(')の受信について不平を言っているので、アポストロフィ(')のXMLコンテンツを削除する方法を探していました。

私は欲しい

<name> Jim O'Connor</name>

になる:

<name> Jim O''Connor</name>

ここで説明されている、に置き換えられるはず'の例を見て''、次のスクリプトを作成しました。

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output omit-xml-declaration="yes" indent="yes" />

      <xsl:template match="node()|@*">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
      </xsl:template>

      <xsl:template name="sqlApostrophe">
        <xsl:param name="string" />
        <xsl:variable name="apostrophe">'</xsl:variable>
        <xsl:choose>
          <xsl:when test="contains($string,$apostrophe)">
            <xsl:value-of select="concat(substring-before($string,$apostrophe), $apostrophe,$apostrophe)"
            disable-output-escaping="yes" />
            <xsl:call-template name="sqlApostrophe">
              <xsl:with-param name="string"
              select="substring-after($string,$apostrophe)" />
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$string"
            disable-output-escaping="yes" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>

      <xsl:template match="text()">
        <xsl:call-template name="sqlApostrophe">
          <xsl:with-param name="string" select="."/>
        </xsl:call-template>
      </xsl:template>

    </xsl:stylesheet>

更新:正常に動作します

ご協力いただきありがとうございます

4

2 に答える 2

0

構文的にいくつかの問題があります...

  1. apply-templatesタグにname属性を設定することはできません。
  2. xpath "node()|@*"があいまいです。

これをデバッガーで実行しましたか?酸素をお勧めします。

于 2010-05-13T15:36:56.330 に答える
0

主な問題は最後のテンプレートにあります。dacracotが指摘しているように、属性xsl:apply-templatesを取りません。name名前付きテンプレートを呼び出すには、を使用しますxsl:call-template

SQLエスケープをすべてのテキストノードに適用する場合は、最後のテンプレートを次のようなものに置き換えてみてください。

<xsl:template match="text()">
  <xsl:call-template name="sqlApostrophe">
    <xsl:with-param name="string" select="."/>
  </xsl:call-template>
</xsl:template>

また、なぜdisable-output-escapingですか?テキストに特殊文字(<、、 )が含まれている場合>&出力として不正な形式のXMLが取得されます。

于 2010-05-13T17:58:35.270 に答える