Trying to put data into an alternating 2 column table.

Discussion in 'XML' started by eric.goforth@gmail.com, Sep 20, 2006.

  1. Guest

    Hello,

    I have some xml data that look like:

    <currencysummary rec_count="16">
    <currency>
    <currency_description>$</_currency_description>
    <currency_code>1</_currency_code>
    </currency>
    <currency>
    <currency_description>£</_currency_description>
    <currency_code>2</_currency_code>
    </currency>
    <currency>
    <currency_description>€</_currency_description>
    <currency_code>3</_currency_code>
    </currency>
    <currency>
    <currency_description>$</_currency_description>
    <currency_code>4</_currency_code>
    </currency>
    </currencysummary>

    I'd like to get it to look like:

    <table cellpadding="2" cellspacing="0" border="0">
    <tr><td>1$</td><td>2£</td></tr>
    <tr><td>3€</td><td>4$</td></tr>
    </table>

    I've tried

    <table cellpadding="2" cellspacing="0" border="0">
    <xsl:for-each select="/page/contents/currencysummary/currency">
    <xsl:choose>
    <xsl:when test="((currency_code mod 2) = 0)">
    <td><xsl:value-of
    select="currency_code"></xsl:value-of><xsl:value-of
    select="currency_description"></xsl:value-of></td></tr>
    </xsl:when>
    <xsl:eek:therwise>
    <tr><td><xsl:value-of
    select="currency_code"></xsl:value-of><xsl:value-of
    select="currency_description"></xsl:value-of></td>
    </xsl:eek:therwise>
    </tr>
    </xsl:choose>
    </xsl:for-each>
    </table>

    But the xml parser doesn't like it because I have incomplete <tr> tags.
    I also tried doing a CDATA around the beginning and ending <tr> tags.
    Does anyone know how to do this?

    -Eric
     
    , Sep 20, 2006
    #1
    1. Advertising

  2. wrote:
    > But the xml parser doesn't like it because I have incomplete <tr> tags.


    You can't work in terms of tags. Think in terms of elements. Generate an
    element, and generate content nested within it.

    See some of the examples at
    http://www.dpawson.co.uk/xsl/sect2/N7450.html


    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Sep 20, 2006
    #2
    1. Advertising

  3. Guest

    wrote:
    > I have some xml data that look like:
    >
    > <currencysummary rec_count="16">
    > <currency>
    > <currency_description>$</_currency_description>
    > <currency_code>1</_currency_code>
    > </currency>
    > <currency>
    > <currency_description>£</_currency_description>
    > <currency_code>2</_currency_code>
    > </currency>
    > <currency>
    > <currency_description>_</_currency_description>
    > <currency_code>3</_currency_code>
    > </currency>
    > <currency>
    > <currency_description>$</_currency_description>
    > <currency_code>4</_currency_code>
    > </currency>
    > </currencysummary>
    >
    > I'd like to get it to look like:
    >
    > <table cellpadding="2" cellspacing="0" border="0">
    > <tr><td>1$</td><td>2£</td></tr>
    > <tr><td>3_</td><td>4$</td></tr>
    > </table>


    OT, but you should use styles instead of obsolete HTML
    attributes.

    > I've tried


    [XSLT]

    > But the xml parser doesn't like it because I have
    > incomplete <tr> tags. I also tried doing a CDATA around
    > the beginning and ending <tr> tags.
    > Does anyone know how to do this?


    Try the following:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:eek:utput
    method="xml"
    version="1.0"
    encoding="UTF-8"/>
    <xsl:template match="/">
    <xsl:apply-templates select="currencysummary"/>
    </xsl:template>
    <xsl:template match="currencysummary">
    <table>
    <xsl:apply-templates
    select="currency[(position() mod 2)=1]"/>
    </table>
    </xsl:template>
    <xsl:template match="currency[(position() mod 2)=1]">
    <tr>
    <td>
    <xsl:value-of select="currency_code"/>
    <xsl:value-of select="currency_description"/>
    </td>
    <xsl:apply-templates
    select="following-sibling::currency[1]"/>
    </tr>
    </xsl:template>
    <xsl:template match="currency[(position() mod 2)=0]">
    <td>
    <xsl:value-of select="currency_code"/>
    <xsl:value-of select="currency_description"/>
    </td>
    </xsl:template>
    </xsl:stylesheet>

    (Note that it outputs XML, not HTML -- tinker with
    xsl:eek:utput to get what you need.)

    --
    Pavel Lepin
     
    , Sep 21, 2006
    #3
  4. This is quite easy, in fact.

    The following is an example how to create a table with N columns and display
    every pair of rows in alternating colours:

    http://www.topxml.com/code/default.asp?p=3&id=v20020514091249


    Cheers,
    Dimitre Novatchev


    <> wrote in message
    news:...
    Hello,

    I have some xml data that look like:

    <currencysummary rec_count="16">
    <currency>
    <currency_description>$</_currency_description>
    <currency_code>1</_currency_code>
    </currency>
    <currency>
    <currency_description>£</_currency_description>
    <currency_code>2</_currency_code>
    </currency>
    <currency>
    <currency_description>?</_currency_description>
    <currency_code>3</_currency_code>
    </currency>
    <currency>
    <currency_description>$</_currency_description>
    <currency_code>4</_currency_code>
    </currency>
    </currencysummary>

    I'd like to get it to look like:

    <table cellpadding="2" cellspacing="0" border="0">
    <tr><td>1$</td><td>2£</td></tr>
    <tr><td>3?</td><td>4$</td></tr>
    </table>

    I've tried

    <table cellpadding="2" cellspacing="0" border="0">
    <xsl:for-each select="/page/contents/currencysummary/currency">
    <xsl:choose>
    <xsl:when test="((currency_code mod 2) = 0)">
    <td><xsl:value-of
    select="currency_code"></xsl:value-of><xsl:value-of
    select="currency_description"></xsl:value-of></td></tr>
    </xsl:when>
    <xsl:eek:therwise>
    <tr><td><xsl:value-of
    select="currency_code"></xsl:value-of><xsl:value-of
    select="currency_description"></xsl:value-of></td>
    </xsl:eek:therwise>
    </tr>
    </xsl:choose>
    </xsl:for-each>
    </table>

    But the xml parser doesn't like it because I have incomplete <tr> tags.
    I also tried doing a CDATA around the beginning and ending <tr> tags.
    Does anyone know how to do this?

    -Eric
     
    Dimitre Novatchev, Sep 21, 2006
    #4
  5. Guest

    Dimitre Novatchev wrote:
    > This is quite easy, in fact.
    >
    > The following is an example how to create a table with N columns and display
    > every pair of rows in alternating colours:
    >
    > http://www.topxml.com/code/default.asp?p=3&id=v20020514091249
    >
    >
    > Cheers,
    > Dimitre Novatchev
    >
    >


    I'm following this example and I'm seeing really bizarre behavior in
    XML Spy. I save the xml to a file and the xsl to a file then assign
    the xsl to the xml file. If I do a browser view in the XML window I
    see alternative colored rows. If I run the debugger do a browser view
    in the html code that's generated, I see one column. If I look at the
    html that's generated it appears that the browser view of the html is
    correct. Any idea what's going on?

    -Eric
     
    , Sep 22, 2006
    #5
  6. <> wrote in message
    news:...
    >
    > Dimitre Novatchev wrote:
    >> This is quite easy, in fact.
    >>
    >> The following is an example how to create a table with N columns and
    >> display
    >> every pair of rows in alternating colours:
    >>
    >> http://www.topxml.com/code/default.asp?p=3&id=v20020514091249
    >>
    >>
    >> Cheers,
    >> Dimitre Novatchev
    >>
    >>

    >
    > I'm following this example and I'm seeing really bizarre behavior in
    > XML Spy. I save the xml to a file and the xsl to a file then assign
    > the xsl to the xml file. If I do a browser view in the XML window I
    > see alternative colored rows. If I run the debugger do a browser view
    > in the html code that's generated, I see one column. If I look at the
    > html that's generated it appears that the browser view of the html is
    > correct. Any idea what's going on?



    I guess this is a question to XML SPy's developers. I have never had any
    problems using a compliant XSLT processor.

    Cheers,
    Dimitre Novatchev
     
    Dimitre Novatchev, Sep 23, 2006
    #6
    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. =?Utf-8?B?TWF4IFdlZWJsZXI=?=

    Alternating Item Style "ForeColor" not working in Template Column

    =?Utf-8?B?TWF4IFdlZWJsZXI=?=, Oct 26, 2005, in forum: ASP .Net
    Replies:
    9
    Views:
    934
    =?Utf-8?B?TWF4IFdlZWJsZXI=?=
    Oct 27, 2005
  2. savvy
    Replies:
    1
    Views:
    494
    savvy
    Jan 18, 2006
  3. Ralph Snart
    Replies:
    2
    Views:
    821
    Ralph Snart
    Aug 3, 2003
  4. Guest
    Replies:
    5
    Views:
    1,785
    Eliyahu Goldin
    Jul 13, 2006
  5. cho.cabot
    Replies:
    3
    Views:
    1,240
    Jonathan N. Little
    Nov 13, 2006
Loading...

Share This Page