Search code examples
xmlxsltapache-fop

How to get array length in XSL?


I need to show table, if array have elements, and some another block, if array is empty? I have only for-each now. My array has "items" name.

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

I want another variant, something like this, but in XSL style:

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

How i can do it? I need if for FOP (pdf generator).


Solution

  • You can do this:

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

    But this would be a better approach:

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