335

如何使用XSL检查值是 null 还是空?

例如,如果categoryName是空的?我在选择构造时使用。

例如:

<xsl:choose>
    <xsl:when test="categoryName !=null">
        <xsl:value-of select="categoryName " />
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="other" />
    </xsl:otherwise>
</xsl:choose>
4

14 回答 14

333
test="categoryName != ''"

编辑:在我看来,这涵盖了从问题中推断出的“[not] null or empty”最可能的解释,包括它的伪代码和我自己对 XSLT 的早期经验。即,“以下 Java 的等价物是什么?”:

// Equivalent Java, NOT XSLT
!(categoryName == null || categoryName.equals(""))

有关更多详细信息,例如,明确识别 null 与空,请参阅下面 johnvey 的答案和/或我从该答案改编的 XSLT 'fiddle',其中包括 Michael Kay 评论中的选项以及第六种可能的解释。

于 2009-05-05T16:53:02.780 回答
291

如果没有任何其他信息,我将假设以下 XML:

<group>
    <item>
        <id>item 1</id>
        <CategoryName>blue</CategoryName>
    </item>
    <item>
        <id>item 2</id>
        <CategoryName></CategoryName>
    </item>
    <item>
        <id>item 3</id>
    </item>
    ...
</group>

示例用例如下所示:

<xsl:for-each select="/group/item">
    <xsl:if test="CategoryName">
        <!-- will be instantiated for item #1 and item #2 -->
    </xsl:if>
    <xsl:if test="not(CategoryName)">
        <!-- will be instantiated for item #3 -->
    </xsl:if>
    <xsl:if test="CategoryName != ''">
        <!-- will be instantiated for item #1 -->
    </xsl:if>
    <xsl:if test="CategoryName = ''">
        <!-- will be instantiated for item #2 -->
    </xsl:if>
</xsl:for-each>
于 2009-05-05T17:06:31.890 回答
77

空元素

测试某个节点的值是否为空

这取决于你所说的空是什么意思。

  • 不包含子节点:not(node())
  • 不包含文本内容:not(string(.))
  • 不包含除空格以外的文本:not(normalize-space(.))
  • 除了评论之外什么都不包含:not(node()[not(self::comment())])
于 2009-05-05T16:53:13.107 回答
24

关于什么?

test="not(normalize-space(categoryName)='')"
于 2011-12-02T15:05:30.697 回答
10

前两个处理空值,后两个处理空字符串。

<xsl:if test="USER/FIRSTNAME">
    USERNAME is not null
</xsl:if>
<xsl:if test="not(USER/FIRSTNAME)">
    USERNAME is null
 </xsl:if>
 <xsl:if test="USER/FIRSTNAME=''">
     USERNAME is empty string
 </xsl:if>
 <xsl:if test="USER/FIRSTNAME!=''">
     USERNAME is not empty string
 </xsl:if>
于 2012-08-10T12:58:34.700 回答
8

如何使用 XSL 检查值是否为空或为空?

例如,如果categoryName是空的?

这可能是最简单的 XPath 表达式(接受答案中的表达式提供了相反的测试,如果否定,会更长):

not(string(categoryName))

说明

not()上述函数的参数false()恰好是上下文项没有categoryName子项(“null”),或者(单个这样的)categoryName子项具有字符串值——空字符串。

在选择构造时使用。

例如:

<xsl:choose>
    <xsl:when test="categoryName !=null">
        <xsl:value-of select="categoryName " />
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="other" />
    </xsl:otherwise>
</xsl:choose>

在 XSLT 2.0 中使用

<xsl:copy-of select="concat(categoryName,  $vOther[not(string(current()/categoryName))])"/>

这是一个完整的例子

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:variable name="vOther" select="'Other'"/>

 <xsl:template match="/">
  <xsl:copy-of select="concat(categoryName,$vOther[not(string(current()/categoryName))])"/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时:

<categoryName>X</categoryName>

产生了想要的正确结果

X

应用于此 XML 文档时

<categoryName></categoryName>

或对此:

<categoryName/>

或在此

<somethingElse>Y</somethingElse>

产生正确的结果

Other

同样,使用这个XSLT 1.0转换:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:variable name="vOther" select="'Other'"/>

  <xsl:template match="/">
    <xsl:copy-of select=
    "concat(categoryName,  substring($vOther, 1 div not(string(categoryName))))"/>
  </xsl:template>
</xsl:stylesheet>

请注意:根本没有使用条件。在这个不错的 Pluralsight 课程中了解更多关于避免条件构造的重要性:

.NET 中的战术设计模式:控制流

于 2016-01-01T01:16:10.547 回答
7

在某些情况下,您可能想知道该值何时为 null,这在使用从 .NET 对象序列化的 XML 时尤其必要。虽然接受的答案适用于此,但当字符串为空白或空时,它也会返回相同的结果,即'',因此您无法区分。

<group>
    <item>
        <id>item 1</id>
        <CategoryName xsi:nil="true" />
    </item>
</group>

因此,您可以简单地测试该属性。

<xsl:if test="CategoryName/@xsi:nil='true'">
   Hello World.
</xsl:if>

有时需要知道确切的状态,你不能简单地检查 CategoryName 是否被实例化,因为不像 Javascript

<xsl:if test="CategoryName">
   Hello World.
</xsl:if>

将为 null 元素返回 true。

于 2011-08-12T08:26:09.710 回答
6

我知道这个问题很老,但是在所有答案中,我错过了一个在 XSLT 开发中这个用例的常用方法。

我想象 OP 中缺少的代码看起来像这样:

<xsl:template match="category">
    <xsl:choose>
        <xsl:when test="categoryName !=null">
            <xsl:value-of select="categoryName " />
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="other" />
        </xsl:otherwise>
    </xsl:choose>
</category>

输入看起来像这样:

<categories>
    <category>
       <categoryName>Books</categoryName>
    </category>
    <category>
       <categoryName>Magazines</categoryName>
       <categoryName>Periodicals</categoryName>
       <categoryName>Journals</categoryName>
    </category>
    <category>
        <categoryName><!-- please fill in category --></categoryName>
    </category>
    <category>
        <categoryName />
    </category>
    <category />
</categories>

即,我假设可以有零个、空的、单个或多个categoryName元素。使用 -style 构造来处理所有这些情况xsl:choose,或者换句话说,命令式的,很快就会变得混乱(如果元素可以处于不同的级别,那就更糟了!)。XSLT 中的一个典型编程习惯是使用模板(因此在 XSLT 中使用 T),它是声明式编程,而不是命令式编程(你不告诉处理器要做什么,你只告诉在满足某些条件时你想要输出什么)。对于此用例,可能类似于以下内容:

<!-- positive test, any category with a valid categoryName -->
<xsl:template match="category[categoryName[text()]]">
    <xsl:apply-templates />
</xsl:template>

<!-- any other category (without categoryName, "null", with comments etc) -->
<xsl:template match="category">
    <xsl:text>Category: Other</xsl:text>
</xsl:template>

<!-- matching the categoryName itself for easy handling of multiple names -->
<xsl:template match="categoryName">
    <xsl:text>Category: </xsl:text>
    <xsl:value-of select="." />
</xsl:template>

这有效(适用于任何 XSLT 版本),因为上面的第一个具有更高的优先级(它有一个谓词)。“直通”匹配模板,第二个,捕获任何无效的东西。然后第三个负责categoryName以适当的方式输出值。

请注意,在这种情况下不需要专门匹配categoriesor category,因为处理器会自动处理所有子级,除非我们另有说明(在本例中,第二个和第三个模板不会进一步处理子级,因为xsl:apply-templates没有他们)。

这种方法比命令式方法更容易扩展,因为它自动处理多个类别,并且可以通过添加另一个匹配模板来扩展其他元素或异常。没有 if 分支的编程

null注意:在 XML中没有这样的东西。有xsi:nil,但很少使用,尤其是在没有某种模式的无类型场景中。

于 2014-07-21T02:27:19.607 回答
4

如果 XML 中可能不存在该元素,我将测试该元素是否存在以及字符串长度是否大于零:

<xsl:choose>
    <xsl:when test="categoryName and string-length(categoryName) &gt; 0">
        <xsl:value-of select="categoryName " />
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="other" />
    </xsl:otherwise>
</xsl:choose>
于 2013-06-14T15:47:53.883 回答
3

如果一个节点在输入 xml 中没有可用的值,如下面的 xpath,

<node>
    <ErrorCode/>
</node>

string() 函数转换为空值。所以这很好用:

string(/Node/ErrorCode) =''
于 2015-04-23T09:35:26.693 回答
2

像这样的东西对我有用:

<xsl:choose>
  <xsl:when test="string(number(categoryName)) = 'NaN'"> - </xsl:when> 
  <xsl:otherwise> 
    <xsl:number value="categoryName" />
  </xsl:otherwise>
</xsl:choose>

或者反过来:

<xsl:choose>
  <xsl:when test="string(number(categoryName)) != 'NaN'">
    <xsl:number value="categoryName" />
  </xsl:when> 
  <xsl:otherwise> - </xsl:otherwise>
</xsl:choose>

注意:如果您不检查空值或处理空值,IE7 将返回 -2147483648 而不是 NaN。

于 2011-08-18T10:12:01.397 回答
1

实际上,我发现只测试字符串长度会更好,因为很多时候该字段不是空的,只是空的

<xsl:when test="string-length(field-you-want-to-test)<1">

于 2016-06-23T08:54:53.193 回答
0

根据我的经验,最好的方法是:

<xsl:when test="not(string(categoryName))">
    <xsl:value-of select="other" />
</xsl:when>
<otherwise>
    <xsl:value-of select="categoryName" />
</otherwise>
于 2013-07-29T07:40:58.007 回答
0

使用简单的 categoryName/text() 这样的测试<categoryName/>也可以正常工作<categoryName></categoryName>

<xsl:choose>
    <xsl:when test="categoryName/text()">
        <xsl:value-of select="categoryName" />
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="other" />
    </xsl:otherwise>
</xsl:choose>
于 2015-09-10T13:00:03.407 回答