Re: [xslt] XML_DOCUMENT_NODE and boolean testing



Phil Shafer schrieb:
I call a template and assign the returned RTF to a variable.  If I
test this variable, it's always true, even if the contents are
empty.

An RTF always evaluates to true. This may not be intuitive, but it is
correct as per the spec. A node-set with one document root also
evaluates to true.

      <xsl:variable name="t">
        <xsl:call-template name="make-stuff">
          <xsl:with-param name="value" select="true"/>
        </xsl:call-template>
      </xsl:variable>

      <xsl:variable name="f">
        <xsl:call-template name="make-stuff">
          <xsl:with-param name="value" select="false"/>
        </xsl:call-template>
      </xsl:variable>

You probably want "true()" and "false()" here. (Even if that doesn't
change the outcome in this case.) Instead, you're selecting child
element nodes named "true" and "false", which may or may not be present
in a given input document.

Both the ext:node-set() and the exsl:node-set() functions work
correctly. See the following stylesheet.

Michael Ludwig

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:exsl="http://exslt.org/common";
  extension-element-prefixes="exsl"
  xmlns:ext="http://xmlsoft.org/XSLT/namespace";>
  <xsl:output method="text"/>
  <!-- make-stuff returns stuff if the parameter 'value' is true -->
  <xsl:template name="make-stuff">
    <xsl:param name="value"/>
    <xsl:if test="$value">
      <stuff/>
    </xsl:if>
  </xsl:template>

  <xsl:template match="/">
    <xsl:variable name="t">
      <xsl:call-template name="make-stuff">
        <xsl:with-param name="value" select="true()"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="f">
      <xsl:call-template name="make-stuff">
        <xsl:with-param name="value" select="false()"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:value-of select="boolean($t)"/>
    <xsl:text>&#10;</xsl:text>
    <xsl:value-of select="boolean($f)"/>
    <xsl:text>&#10;</xsl:text>

    <xsl:value-of select="exsl:object-type($t)"/>
    <xsl:text>&#10;</xsl:text>
    <xsl:value-of select="exsl:object-type($f)"/>
    <xsl:text>&#10;</xsl:text>

    <xsl:value-of select="boolean(ext:node-set($t))"/>
    <xsl:text>&#10;</xsl:text>
    <xsl:value-of select="boolean(ext:node-set($f))"/>
    <xsl:text>&#10;</xsl:text>

    <xsl:value-of select="boolean(exsl:node-set($t))"/>
    <xsl:text>&#10;</xsl:text>
    <xsl:value-of select="boolean(exsl:node-set($f))"/>
    <xsl:text>&#10;</xsl:text>

    <xsl:value-of select="boolean(ext:node-set($t)/*)"/>
    <xsl:text>&#10;</xsl:text>
    <xsl:value-of select="boolean(ext:node-set($f)/*)"/>
    <xsl:text>&#10;</xsl:text>

    <xsl:value-of select="boolean(exsl:node-set($t)/*)"/>
    <xsl:text>&#10;</xsl:text>
    <xsl:value-of select="boolean(exsl:node-set($f)/*)"/>
    <xsl:text>&#10;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

mludwig forelle:~/Werkstatt/xsl > xsltproc /tmp/Phil.xsl /tmp/Phil.xsl
true
true
RTF
RTF
true
true
true
true
true
false
true
false



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]