【问题标题】:XSLT 1.0: escaping double quotesXSLT 1.0:转义双引号
【发布时间】:2016-09-28 06:58:41
【问题描述】:

我有一个遵循此方案的 XML 文件:

<translationData>
<product>
<attributeValue>
<value>1/4"</value>
<value1>1/4"</value1>
<currentValue>aaaa;bbbb</currentValue>
</attributeValue>
</product>
</translationData>

由于“currentValue”中的分号,我需要转义分号和“value”中的双引号。 我可以通过将所有文本放在 qoutes 中来转义分号,如下所示:

XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" encoding="utf-8" />

  <xsl:param name="delim" select="';'" />
  <xsl:param name="quote" select="'&quot;'" />
  <xsl:param name="break" select="'&#xA;'" />

  <xsl:template match="/">
    <xsl:apply-templates select="translationData/product/attributeValue" />
  </xsl:template>

  <xsl:template match="attributeValue">
    <xsl:apply-templates />
    <xsl:if test="following::*">
      <xsl:value-of select="$break" />
    </xsl:if>
  </xsl:template>


  <xsl:template match="*">
    <!-- remove normalize-space() if you want keep white-space at it is -->

    <xsl:value-of select="concat($quote, translate(.,'&quot;','\&quot;'), $quote)" />
    <xsl:if test="following-sibling::*">
      <xsl:value-of select="$delim" />
    </xsl:if>
  </xsl:template>

  <xsl:template match="text()" />
</xsl:stylesheet>

但不知何故,输出是:

"1/4\";"1/4\";"aaaa;bbbb"

而不是

"1/4\"";"1/4\"";"aaaa;bbbb"

我哪里错了?

我是 XML 和 XSLT 的新手,在处理这个特定案例时没有发现任何问题。


XSLT 代码来自@Tomalak 对另一个问题的回答。见here

【问题讨论】:

  • R 来自哪里?
  • 请不要在没有给予他人信任的情况下发布其他人的代码。您已经从stackoverflow.com/a/365372/18771 复制了这段XSLT。每当您使用不是您自己编写的代码时,请说出来。
  • @michael.hor257k 不应该在那里。刚刚删除它。
  • @Tomalak:抱歉不知道。再也不会发生!将立即对其进行编辑。

标签: xslt escaping


【解决方案1】:

translate() 函数只会将每个单个字符替换为另一个 单个 字符。

要将单个字符 " 替换为两个字符的字符串\",您需要使用命名递归模板,或者 - 如果您的处理器支持它 - 扩展函数,例如 EXSLT str:replace()

以下是使用递归模板的示例:

...

<xsl:template match="*">
    <xsl:text>"</xsl:text>
    <xsl:call-template name="replace">
        <xsl:with-param name="text" select="."/>
    </xsl:call-template>
    <xsl:text>"</xsl:text>
    <xsl:if test="position()!=last()">
        <xsl:text>;</xsl:text>
    </xsl:if>
</xsl:template>

...

<xsl:template name="replace">
    <xsl:param name="text"/>
    <xsl:param name="searchString">"</xsl:param>
    <xsl:param name="replaceString">\"</xsl:param>
    <xsl:choose>
        <xsl:when test="contains($text,$searchString)">
            <xsl:value-of select="substring-before($text,$searchString)"/>
            <xsl:value-of select="$replaceString"/>
           <!--  recursive call -->
            <xsl:call-template name="replace">
                <xsl:with-param name="text" select="substring-after($text,$searchString)"/>
                <xsl:with-param name="searchString" select="$searchString"/>
                <xsl:with-param name="replaceString" select="$replaceString"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

...

【讨论】:

  • 完美运行,完美回答我的问题!非常感谢!
  • 您的代码可以正常工作。但现在我发现我的问题不正确。输出为:"1/4\"";"1/4\"";"";如果“currentValue”为空。那里没问题。但是,一旦我在 Excel 中打开 CSV,它就会在一个单元格中显示输出,例如:1/4\";1/4\"" 它应该把它放在不同的单元格中。我认为这个问题可以通过替换 " by \"。但Excel似乎不明白这一点。
  • 尝试将 replaceString 参数更改为 &lt;xsl:param name="replaceString"&gt;""&lt;/xsl:param&gt; - 请参阅:en.wikipedia.org/wiki/Comma-separated_values#Standardization
  • 您先生,是我的英雄!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-11
  • 2020-11-26
  • 1970-01-01
  • 1970-01-01
  • 2013-06-17
相关资源
最近更新 更多