Using XSLT to Break XML Data into Rows

I have some general XML data:

        <news-item level="1">
            <item-title>FLASHWAVE 4100 R4.1 Readies for PR1</item-title>
            <item-text>Work continues to bring the 4100 Project (4.1) toward PR1</item-text>
            <item-text>The NMIS has been updated and is Draft ready for PR1</item-text>
</pre><BR>There can be an arbitrary number of item-text elements.<BR><BR>I am using Apache's Forrest to display this information on a web site. I decided I wanted to break the item-text information in 2 columns so it takes less room.<BR><BR>The XSLT I have so far is:<BR><pre class="ip-ubbcode-code-pre">    <!--==================================================================== -->
    <xsl:template match="news-item" mode="new">
        <xsl:variable name="num-items" select="count(item-text)"/>
        <table width="80%">
                    <xsl:if test="$num-items > 1">
                        <xsl:attribute name="colspan">2</xsl:attribute>
                    <xsl:value-of select="item-title"/>
            <xsl:for-each select="item-text">
                <xsl:if  test="position() mod 2 != 0"><tr></xsl:if>
                <td><xsl:value-of select="."/></td>
                <xsl:if  test="position() mod 2 = 0 or position() = last()"></tr></xsl:if>

Which is obviously not working.

I have read Michael Kay’s WROX Book: XSLT Programmer’s Reference and found where he specifically says Don’t Do This!

Yup, that’s what I want to do, delay the writing of the end-tag until I have an even numbered position() or I’m at the end of my for-each list.

What am I missing to see the solution to this problem?



GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

Michael Hare

Hi Michael,

the idea is to apply the rule that generates a data row to every
other item-text entry and to use the following-sibling axis to
make the row contain the current item as well as the following
one (if it exists):

<xsl:template match=“news-item”>

<xsl:apply-templates select=“item-text[position() mod 2 = 1]”/>


<xsl:template match =“item-text”>

<xsl:apply-templates select = “following-sibling::item-text[1]/node()”/>