【问题标题】:XSLT: output " without it being parsedXSLT:输出“而不被解析
【发布时间】:2014-05-19 21:05:14
【问题描述】:

我正在尝试实现以下 XML 输出:

<Foo bar="&quot;" />

我的 XSLT 文件如下:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:template match="/">

        <xsl:variable name="quote">
            <xsl:text>&quot;</xsl:text>
        </xsl:variable>

        <Foo bar="{$quote}"/>
    </xsl:template>
</xsl:stylesheet>

不幸的是,这给了我输出:

<Foo bar="&#34;"/>

如何更改我的 XSLT 以输出 & quot;无需将其解析为 " 字符或 & #34;?

【问题讨论】:

  • 为什么重要?两者是等价的(还有各种其他等价的方式来表达同样的事情,例如&lt;Foo bar='"'/&gt;),并且 XML 解析器不会告诉应用程序它是在源代码中以哪种方式编写的,应用程序只会看到一个属性其值为双引号字符。
  • 你提出了一个很好的观点。我一直致力于以特定格式输出 XML - 以匹配 Microsoft Excel 似乎想要的,但你是对的,即使 " Excel 也能正确解析文件。用来。谢谢!

标签: parsing xslt quotes


【解决方案1】:

Ian Roberts 已经提出了很好的观点,即这实际上并不重要。但是,如果您真的非常想这样做,那么在 XSLT 2.0(但不是 XSLT 1.0)中,您可以使用字符映射,如下所示:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" use-character-maps="quotes" />

    <xsl:character-map name="quotes">
        <xsl:output-character character="&quot;" string="&amp;quot;" />
    </xsl:character-map>

    <xsl:template match="/">
        <xsl:variable name="quote">
            <xsl:text>&quot;</xsl:text>
        </xsl:variable>

        <Foo bar="{$quote}"/>
    </xsl:template>
</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多