【问题标题】:xsl: when expression with boolean datatypexsl:当表达式具有布尔数据类型时
【发布时间】:2013-05-31 14:07:13
【问题描述】:

我在使用布尔数据类型的 xsl:when 语句时遇到问题。正如您所看到的,当我将布尔表达式与另一种数据类型一起使用并使用布尔表达式来测试属性“存在”(Phone/@Type)时,它可以工作,但是当我使用测试时...... =“'true' " 表达式或相反,我得到两条记录的“否”或两条记录的“是”,而不是 1 是和 1 否。根据我在这里找到的答案,我尝试了许多测试表达式的变体。作为我正在做的练习的一部分,IsManager 属性应该是布尔值,而不仅仅是测试它的表达式。

这是我的 xml:

 <Person IsManager="true">
  <FirstName>Alex</FirstName>
  <LastName>Chilton</LastName>
  <Phone Type="Cell">555-1212</Phone>
  <IM>Alex1092</IM>

还有第二条记录:

<Person>
  <FirstName>Laura</FirstName>
  <LastName>Chilton</LastName>
  <Phone>555-5678</Phone>
  <IM>LaurethSulfate</IM>

这是 xsl:

    <xsl:for-each select="//Person">
       <tr>
         <td><xsl:value-of select="FirstName"/></td>
         <td><xsl:value-of select="LastName"/></td>
        <xsl:choose> 
          <xsl:when test="boolean(Phone/@Type)">
             <td><xsl:value-of select="Phone/@Type"/></td>
          </xsl:when>
          <xsl:otherwise>
            <td>Home</td>
          </xsl:otherwise>
    </xsl:choose>
          <td><xsl:value-of select="Phone"/></td> 
          <td><xsl:value-of select="IM"/></td>
        <xsl:choose>     
          <xsl:when test="(Person/@IsManager)='true'">
            <td>Yes</td>  
          </xsl:when>   
          <xsl:otherwise>
            <td>No</td>      
          </xsl:otherwise>
        </xsl:choose>
       </tr>
    </xsl:for-each>

结果如下:

名字 姓氏 电话类型 电话 IM 经理 Alex Chilton 单元 555-1212 Alex1092 否 Laura Chilton Home 555-5678 LaurethSulfate No

任何帮助将不胜感激。谢谢你。

【问题讨论】:

    标签: xslt


    【解决方案1】:

    您位于for-each select="//Person" 中,因此上下文节点是Person 元素。因此,而不是

    <xsl:when test="(Person/@IsManager)='true'">
    

    你只需要

    <xsl:when test="@IsManager='true'">
    

    请注意,这不是与布尔值进行比较,而是将IsManager 属性的值与字符串“true”进行比较。

    【讨论】:

      猜你喜欢
      • 2010-09-20
      • 2011-05-25
      • 1970-01-01
      • 2016-01-21
      • 2011-04-29
      • 2018-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多