xsl:if and nested td tag problem

Discussion in 'XML' started by Steven, Jul 23, 2003.

  1. Steven

    Steven Guest

    I'm using XSL to transform an XML document to HTML, however I'm encountering
    the following problem.I want to test a couple of values using an xsl:if
    statement and then print a couple of HTML tags only when the condition is
    met:

    <xsl:if test="position() = $countPar">

    </td>

    <td width="50%" valign="top">

    </xsl:if>

    However, the parser takes offence at the td closing tag following an xsl:if
    opening tag. What do I do?

    Thanks

    Steven
    Steven, Jul 23, 2003
    #1
    1. Advertising

  2. Steven wrote:

    > I've got a number of elements that each contain a paragraph of text, I want
    > to put that text into 2 columns of a HTML table, half the paragraphs in one
    > column, half in the other. So I loop through the paragraph elements (using
    > xsl:for-each), outputting each one in turn. When I'm on the middle element I
    > want to output HTML that will close the tag for the current cell and start
    > the new cell (or column). After that I just output the rest of paragraph
    > elements. So what I've been trying is to use an xsl:if statement in the
    > middle of the xsl:for-each to see if I'm on the middle element and then
    > creating the new cell at that point.

    Well, again, you can't program in XSLT as in C or Java, it's not imperative
    procedural language. You have to learn to think in a declarative way.
    So instead of processing paragraph and closing/opening cells XSLT requires you
    just to declare that you want two cells, half of text in one and the rest in
    the second.
    Why don't you post an example of you XML and the desired result?
    --
    Oleg Tkachenko
    http://www.tkachenko.com/blog
    Multiconn Technologies, Israel
    Oleg Tkachenko, Jul 23, 2003
    #2
    1. Advertising

  3. Steven

    Steven Guest

    Yeah, see what you mean. This is my first stab at XSL and I'm still not
    certain of what it is and isn't capable of. Now that you've confirmed that
    this won't be possible, and suggested the correct way of considering the
    problem I've found a solution - to do the loop twice, once in each cell. In
    the first cell I'll check that the element number is less that half of the
    total number of elements, and in the second cell that only those elements
    over half way through the total are output. Simple.

    Thanks for your help Oleg.

    Steven
    Steven, Jul 23, 2003
    #3
  4. Steven wrote:

    > Yeah, see what you mean. This is my first stab at XSL and I'm still not
    > certain of what it is and isn't capable of. Now that you've confirmed that
    > this won't be possible, and suggested the correct way of considering the
    > problem I've found a solution - to do the loop twice, once in each cell. In
    > the first cell I'll check that the element number is less that half of the
    > total number of elements, and in the second cell that only those elements
    > over half way through the total are output. Simple.

    Yes, but not really effective. Try something like this: consider the following
    XML fragment:
    <foo>
    <para>text1</para>
    <para>text2</para>
    <para>text3</para>
    <para>text4</para>
    </foo>

    Then to split para elements into 2 cells:
    <xsl:template match="foo">
    <xsl:variable name="size" select="count(para)"/>
    <td>
    <xsl:apply-templates select="para[position() &lt;= $size div 2]"/>
    </td>
    <td>
    <xsl:apply-templates select="para[position() > $size div 2]"/>
    </td>
    </xsl:template>

    --
    Oleg Tkachenko
    http://www.tkachenko.com/blog
    Multiconn Technologies, Israel
    Oleg Tkachenko, Jul 23, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RC
    Replies:
    2
    Views:
    426
    Malte
    May 10, 2005
  2. shruds
    Replies:
    1
    Views:
    783
    John C. Bollinger
    Jan 27, 2006
  3. RC
    Replies:
    2
    Views:
    640
  4. Replies:
    1
    Views:
    3,598
    A. Bolmarcich
    May 27, 2005
  5. Max
    Replies:
    1
    Views:
    536
    Martin Honnen
    Aug 19, 2007
Loading...

Share This Page