【问题标题】:xsl:when not working in xsl-fo?xsl:当不在 xsl-fo 中工作时?
【发布时间】:2026-01-22 04:15:01
【问题描述】:

我有这样的示例代码:

<fo:block>
    <xsl:value-of select="totalsForMonth/@totalI" />
</fo:block>

<xsl:when test="totalsForMonth/@totalI != '0.0'">
    <fo:block>
        <xsl:value-of select="totalsForMonth/@totalI" />
    </fo:block>
</xsl:when>

输出将是:

13.0

在导出的 pdf 中。

我期待看到

13.0
13.0

我做错了什么?

【问题讨论】:

  • &lt;xsl:when&gt; 必须包含在 &lt;xsl:choose&gt; 中。还请向我们提供输入 XML。

标签: xml xslt xsl-fo


【解决方案1】:

您将xsl:whenxsl:if 混淆了。

xsl:when 是在xsl:choose 块(http://xml.apache.org/xalan-j/xsltc/xsl_choose_design.html)中选择不同的案例。对于单个测试,请使用 xsl:if (http://xml.apache.org/xalan-j/xsltc/xsl_if_design.html)。

与 C 或 Java 相比:xsl:chooseswitch 语句并没有完全不同,除了 条件when 块中而不是在 choose 本身中。这意味着您可以在每个测试中放置您想要的任何测试,更像是一个长链 if .. else if .. 块,其中 xsl:otherwise 具有最终 else 的作用。

【讨论】:

  • @KorayTugay:我看到你对 Java 和 C 都有经验,所以我添加了一个比较:)