0

我一直在寻找一种方法来去除我的 XML 内容中的撇号 ('),因为我的 DBMS 抱怨接收这些撇号。

我需要

<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

主要问题出在您的最后一个模板中。正如 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 回答
0

你在语法上有几个问题......

  1. 应用模板标签上不能有名称属性。
  2. 你的 xpath,“node()|@*”,是模棱两可的。

你有没有通过调试器运行它?我建议氧气。

于 2010-05-13T15:36:56.330 回答