如何在XSL中获取数组长度?

时间:2013-03-01 12:32:49

标签: xml xslt apache-fop

我需要显示表,如果数组有元素,还有一些其他块,如果数组为空?我现在只为每个人。我的数组有“项目”名称。

<some table header code...>
<xsl:for-each select="items/item">
    <some row code...>
</xsl:for-each>

我想要另一种变体,就像这样,但是在XSL风格中:

<xsl:if list is empty>
    <block> There is no elements!!! </block>
<xsl:else>
    <table code>
</xsl:if>

我怎么做?我需要FOP(pdf生成器)。

1 个答案:

答案 0 :(得分:2)

你可以这样做:

<xsl:choose>
   <xsl:when test="items/item">
       <xsl:for-each select="items/item">
            <some row code...>
       </xsl:for-each>
   </xsl:when>
   <xsl:otherwise>
       <block>  ... </block>
   </xsl:otherwise>
</xsl:choose>

但这是一种更好的方法:

<xsl:apply-templates select="items[not(item)] | items/item" />

...

<xsl:template match="items">
   <block> ... </block>
</xsl:template>

<xsl:template match="item">
   <!-- Row code -->
</xsl:template>