检查XSLT中的字符串是空仍是空

如何使用XSL检查值是空仍是空? node

例如,若是categoryName为空? 我在选择构造使用的是。 编程

例如: app

<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>

#1楼

前两个处理空值,第二个处理空字符串。 ide

<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>

#2楼

若是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>

#3楼

根据个人经验,最好的方法是: 测试

<xsl:when test="not(string(categoryName))">
    <xsl:value-of select="other" />
</xsl:when>
<otherwise>
    <xsl:value-of select="categoryName" />
</otherwise>

#4楼

我知道这个问题很老,但在全部答案之间,我想念一个在XSLT开发中这个用例的经常使用方法。 spa

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

<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>

而且输入看起来像这样: xml

<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元素。 使用xsl:choose -style构造处理全部这些状况,或者换句话说,命令性地,很快就会变得混乱(若是元素能够处于不一样的级别,则更是如此!)。 XSLT中的典型编程习惯是使用模板(所以是XSLT中的T),这是声明性编程,而不是命令式的(您不告诉处理器该作什么,只要知足某些条件就告诉您想要输出什么)。 对于此用例,可能以下所示: ip

<!-- 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值。

请注意,在这种状况下,不须要特别匹配categoriescategory ,由于处理器将自动处理全部子项,除非咱们另外说明(在此示例中,第二个和第三个模板不会进一步处理子项,由于有no xsl:apply-templates in)。

这种方法比命令式方法更容易扩展,由于它自动处理多个类别,而且能够经过添加另外一个匹配模板来扩展其余元素或异常。 没有if-branches的编程

注意:XML中没有null这样的东西。 有xsi:nil ,但不多使用,特别是在没有某种模式的无类型场景中不多使用。


#5楼

若是节点在输入xml中没有可用的值,如xpath下面,

<node>
    <ErrorCode/>
</node>

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

string(/Node/ErrorCode) =''
相关文章
相关标签/搜索