Go Back   Velocity Reviews > Newsgroups > XML
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

XML - Trying to put data into an alternating 2 column table.

 
Thread Tools Search this Thread
Old 09-20-2006, 09:59 PM   #1
Default Trying to put data into an alternating 2 column table.


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>
<xsltherwise>
<tr><td><xsl:value-of
select="currency_code"></xsl:value-of><xsl:value-of
select="currency_description"></xsl:value-of></td>
</xsltherwise>
</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
  Reply With Quote
Old 09-20-2006, 10:24 PM   #2
Joseph Kesselman
 
Posts: n/a
Default Re: Trying to put data into an alternating 2 column table.

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
  Reply With Quote
Old 09-21-2006, 08:46 AM   #3
p.lepin@ctncorp.com
 
Posts: n/a
Default Re: Trying to put data into an alternating 2 column table.


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
xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xslutput
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
xslutput to get what you need.)

--
Pavel Lepin

  Reply With Quote
Old 09-21-2006, 02:24 PM   #4
Dimitre Novatchev
 
Posts: n/a
Default Re: Trying to put data into an alternating 2 column table.

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>
<xsltherwise>
<tr><td><xsl:value-of
select="currency_code"></xsl:value-of><xsl:value-of
select="currency_description"></xsl:value-of></td>
</xsltherwise>
</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


  Reply With Quote
Old 09-22-2006, 09:49 PM   #5
eric.goforth@gmail.com
 
Posts: n/a
Default Re: Trying to put data into an alternating 2 column table.


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

  Reply With Quote
Old 09-23-2006, 02:07 AM   #6
Dimitre Novatchev
 
Posts: n/a
Default Re: Trying to put data into an alternating 2 column table.


<> 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


  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump