【问题标题】:XSLT 1.0 xsl:boolean choose testXSLT 1.0 xsl:boolean 选择测试
【发布时间】:2012-10-19 12:39:44
【问题描述】:

我有一个带有用于序列化的数据注释的对象。它有一个布尔属性,它是真的(用调试器测试)所以 100% 确定,但是当我在 xslt 中有这个时:

<xsl:for-each select="order/Coupons/orderedCoupons" >
    <tr>
        <td>Discount <xsl:value-of select="@code"/></td>
        <td>
            <xsl:choose>
                <xsl:when test="@isperc='true'">
                    <xsl:value-of select="@disvalue"/>%
                </xsl:when>
                <xsl:otherwise>Epic fail
                    &#8364; <xsl:value-of select="format-number(@disvalue, '#.###,00', 'euro')" />
                </xsl:otherwise>
            </xsl:choose>
        </td>
    </tr>
</xsl:for-each>

它总是显示epic fail,所以测试总是失败。我也试过:

  • @isperc='true'
  • @isperc=1
  • 布尔值(@isperc)
  • @isperc = true()

更多背景信息:它是一个 asp.net 对象,我使用内置数据注释对其进行序列化。 .Net 中的 XSLT 支持仅 1.0。

编辑我的coupon.cs

[XmlAttribute("isperc")]
public bool IsPerc
{
    get { return _isPerc; }
}

它不可为空,但正如第二位评论者指出的那样,&lt;xsl:value-of select="@isperc"/&gt; 的值是多少,它似乎是空的。它在 html 中解析为空。不是空格,不是 null 等等,这很奇怪,因为布尔值是 true...

edit2 这也没有输出任何东西&lt;xsl:value-of select="string(@isperc)"/&gt;,所以这也失败了&lt;xsl:when test="string(@isperc) = 'true'"&gt;

【问题讨论】:

  • 您能否展示一个您尝试处理的输入 XML 示例?
  • 您是否消除了案例问题?见stackoverflow.com/questions/8940695/…&lt;xsl:value-of select="@isperc" /&gt; 返回什么?
  • 必须提供源 XML 文档(请尽可能小)和完整的 XSLT 转换——不仅仅是一个片段。然后任何人都可以重现这个问题,这里的很多人都可以给出一个很好的解决方案。因此,在调试器中,获取序列化的结果并将此 XML 文档放入问题中。看起来,这是一个 C# 序列化问题,而不是 XSLT 问题。

标签: asp.net xslt xslt-1.0


【解决方案1】:
<xsl:when test="@IsPerc = true()">

应该匹配一个 C# 布尔类型。

不过,我见过的一种解决方法是将布尔值转换为数字;

<xsl:when test="number(@IsPerc) > 0">

【讨论】:

  • 我会尝试,但XmlAttribute 是小写的。 code 也是小写字母,作为 C# 属性 Code。但我会试一试,并会尽快更新你。谢谢你的回答
猜你喜欢
  • 1970-01-01
  • 2012-02-18
  • 2021-05-05
  • 1970-01-01
  • 2021-05-09
  • 1970-01-01
  • 2011-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多