![]() |
|
|
|||||||
![]() |
XML - Trying to put data into an alternating 2 column table. |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
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 <tr><td><xsl:value-of select="currency_code"></xsl:value-of><xsl:value-of select="currency_description"></xsl:value-of></td> </xsl </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 eric.goforth@gmail.com |
|
|
|
|
#2 |
|
Posts: n/a
|
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 |
|
|
|
#3 |
|
Posts: n/a
|
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 version="1.0"> <xsl 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 -- Pavel Lepin |
|
|
|
#4 |
|
Posts: n/a
|
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.a...20020514091249 Cheers, Dimitre Novatchev <> wrote in message news: ups.com... 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 <tr><td><xsl:value-of select="currency_code"></xsl:value-of><xsl:value-of select="currency_description"></xsl:value-of></td> </xsl </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 |
|
|
|
#5 |
|
Posts: n/a
|
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.a...20020514091249 > > > 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 |
|
|
|
#6 |
|
Posts: n/a
|
<> wrote in message news: ups.com... > > 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.a...20020514091249 >> >> >> 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 |
|