【问题标题】:Display Yes/No based on returned value of attribute根据属性的返回值显示是/否
【发布时间】:2012-09-20 13:02:06
【问题描述】:

我是 XML/XSL 的新手(比如 2 天的新手)。我有一行我正在执行 xsl:value-of 选择,它返回 True/False 属性。我想让它显示是/否,但我试图这样做没有成功。下面是我目前的线路。有人可以告诉我需要添加什么以使其显示是或否吗?

<fo:block>Automatic Sprinklers Required: &#xA0;
      <xsl:value-of select="Attributes/AttributeInstance/Attribute[@id='1344297']/../Value"/>
</fo:block>

【问题讨论】:

    标签: xml xsl-fo


    【解决方案1】:

    使用 xsl:choose 块来测试值。选择的语法如下:

    <xsl:choose>
       <xsl:when test="some Boolean condition">
        <!-- "if" stuff -->
      </xsl:when>
      <xsl:otherwise>
        <!-- "else" stuff -->
      </xsl:otherwise>
    </xsl:choose>
    

    我重新格式化了您的代码以调用执行此测试并根据布尔值打印是/否的模板:

    <fo:block>Automatic Sprinklers Required: &#xA0;
        <xsl:call-template name="formatBoolean">
           <xsl:with-param name="theBoolValue" select="Attributes/AttributeInstance/Attribute[@id='1344297']/../Value"/>
        </xsl:call-template>
    </fo:block>
    
    
    <xsl:template name="formatBoolean">
       <xsl:param name="theBoolValue"/>
    
       <xsl:choose>
           <xsl:when test="$theBoolValue = true()">
              Yes
           </xsl:when>
           <xsl:otherwise>
              No
           </xsl:otherwise>
       </xsl:choose>
    </xsl:template>
    

    这段代码应该可以工作,虽然我没有测试它是否有任何语法错误。

    祝你好运!

    科比

    【讨论】:

    • 谢谢,这是我缺少的 = true() 部分,是一个梦想
    【解决方案2】:

    这应该很简单,以下是我的 XML:

    <form>
        <value>False</value>
    </form>
    

    还有我的 XSL:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"
        xmlns:date="http://exslt.org/dates-and-times" xmlns:str="http://exslt.org/strings">
        <xsl:template match="/">
            <xsl:choose>
                <xsl:when test="/form/value = 'True'">
                    <xsl:text>Yes</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>No</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 2010-11-25
      • 2022-11-21
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多