Search code examples
xmlxsltapache-fop

XSL - check if the first listitem is a <br /> tag


this is my .xml file

    <list type="point">
            <listitem>
                <text>
                    <br/>
                    <br/>
                     .
                     .
                     .
                </text>
        </listitem>
    </list>

here my .xsl File

<xsl:template match="list/listitem">
    <fo:list-item>
        <fo:list-item-body start-indent="body-start()">
            <fo:block>
                <xsl:choose>
                    <xsl:when
                        test=".//list/listitem[1]/[*node*]">
                        &#160;
                        <xsl:apply-templates />
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates />
                    </xsl:otherwise>
                </xsl:choose>
            </fo:block>
        </fo:list-item-body>
    </fo:list-item>
</xsl:template>

and here my question: is it possible to check at test=[*node*] if the node is a break? Because if there is a break I want to add a blank and if not just apply the other templates without a blank. I've already tried it to check if the node is empty, but it is possible that there are other empty nodes.

The result should be like that:

<fo:list-block provisional-label-separation="15px" provisional-distance-between-starts="10px">
            <fo:list-item>
                <fo:list-item-body start-indent="body-start()">
                    <fo:block>
                        <fo:block space-before="6pt" space-after="3pt">
                            &#160; 
                            <fo:block>\r\n</fo:block>
                            <fo:block>\r\n</fo:block>
                        </fo:block>
                    </fo:block>
                </fo:list-item-body>
            </fo:list-item>
       </fo:list-block>

Kind regards


Solution

  • You current expression is this...

    <xsl:when test=".//list/listitem[1]/[*node*]">
    

    But you positioned on a listitem at this point, so this expression will look for a descendant list item of the current listitem, and will find nothing.

    Try this instead....

    <xsl:when test="not(preceding-sibling::listitem) and */br">
    

    Alternatively, you could try simplifying the expression to this....

    <xsl:when test="position() = 1 and */br">
    

    But you would probably need to ensure the listitem elements are explicitly selected to ensure position() is calculated correctly. For example....

    <xsl:template match="list">
        <xsl:apply-templates select="listitem" />
    </xsl:template>