空字符串的 XSLT 布尔值打印 true

XSLT boolean of an empty string prints true

这是我的 xslt。当 boolean($y) 具有相同的值时,为什么 boolean($x) 打印 true 而 boolean($y) 打印 false 的任何原因。唯一的区别是 x 通过调用模板获取空字符串。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <xsl:variable name="x">
      <xsl:call-template name="tmplate"></xsl:call-template>
    </xsl:variable>

    ###x-bool:[<xsl:value-of select="boolean($x)"/>]
    ###x:[<xsl:value-of select="$x"/>]

    <xsl:variable name="y" select="''"/>
    ###y-bool:[<xsl:value-of select="boolean($y)"/>]
    ###y:[<xsl:value-of select="$y"/>]

  </xsl:template>

  <xsl:template name="tmplate">
    <xsl:value-of select="''"/>
  </xsl:template>
</xsl:stylesheet>

the only difference is that x gets its empty string by calling a template.

不,这不是唯一的 difference.When 您将变量定义为:

<xsl:variable name="y" select="''"/>

变量的数据类型是string。但是当你将它定义为:

<xsl:variable name="x">
    <xsl:value-of select="''"/>
</xsl:variable>

数据类型为result-tree-fragment。它包含一个包含空字符串的文本节点。因此它不是空的,并且在转换为布尔值时将被评估为 true() 。