Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XML to HTML table with XSL. Could someone help me please?

Reply
Thread Tools

XML to HTML table with XSL. Could someone help me please?

 
 
mslyman@gmail.com
Guest
Posts: n/a
 
      06-08-2006
Hi,
I could do with some help. I have this XML.

<region>
<region_code>567</region_code>
<store>
<store_code>345</store_code>
<dept>
<dept_code>32</dept_code>
</dept>
<dept>
<dept_code>33</dept_code>
</dept>
</store>
<store>
<store_code>7</store_code>
<dept>
<dept_code>9</dept_code>
</dept>
<dept>
<dept_code>8</dept_code>
</dept>
</store>
<store>
<!--- more depts -->
</store>
</region>
<region>
<region_code>776</region_code>
<!--- more stores -->
</region>

I need this output to html as follows:
<table>
<tr>
<td title="region">567</td>
<td title="store">345</td>
<td title="dept">34</td>
</tr>
<tr>
<td></td>
<td></td>
<td title="dept">33</td>
</tr>
<tr>
<td></td>
<td title="store">7</td>
<td title="dept">8</td>
</tr>
<tr>
<td></td>
<td></td>
<td title="dept">9</td>
</tr>
<tr>
<td title="region">776</td>
<!-- etc -->
</tr>
<!-- etc -->
</table>

I have tried to achieve this with my limited amount of xsl knowledge of
<xsl:for-each >, nested <xsl:for-each > and <xsl:if
test="position()=1"> and i get close ( by using <xsl:for-each
select="dept"> ) but it's not right. I guessing I have to
apply-templates/recursion but not sure how.
Please if anyone could help that would be great.

Thanks,
Mark

 
Reply With Quote
 
 
 
 
Joe Kesselman
Guest
Posts: n/a
 
      06-08-2006
You might want to look at the example at:

http://www.dpawson.co.uk/xsl/sect2/N7450.html#d9849e709
 
Reply With Quote
 
 
 
 
A. Bolmarcich
Guest
Posts: n/a
 
      06-08-2006
On 2006-06-08, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:
> Hi,
> I could do with some help. I have this XML.
>
><region>
> <region_code>567</region_code>
> <store>
> <store_code>345</store_code>
> <dept>
> <dept_code>32</dept_code>
> </dept>
> <dept>
> <dept_code>33</dept_code>
> </dept>
> </store>
> <store>
> <store_code>7</store_code>
> <dept>
> <dept_code>9</dept_code>
> </dept>
> <dept>
> <dept_code>8</dept_code>
> </dept>
> </store>
> <store>
> <!--- more depts -->
> </store>
></region>
><region>
> <region_code>776</region_code>
> <!--- more stores -->
></region>
>
> I need this output to html as follows:
><table>
><tr>
><td title="region">567</td>
><td title="store">345</td>
><td title="dept">34</td>
></tr>
><tr>
><td></td>
><td></td>
><td title="dept">33</td>
></tr>
><tr>
><td></td>
><td title="store">7</td>
><td title="dept">8</td>
></tr>
><tr>
><td></td>
><td></td>
><td title="dept">9</td>
></tr>
><tr>
><td title="region">776</td>
> <!-- etc -->
></tr>
><!-- etc -->
></table>
>
> I have tried to achieve this with my limited amount of xsl knowledge of
><xsl:for-each >, nested <xsl:for-each > and <xsl:if
> test="position()=1"> and i get close ( by using <xsl:for-each
> select="dept"> ) but it's not right. I guessing I have to
> apply-templates/recursion but not sure how.
> Please if anyone could help that would be great.


It would help if you posted what you tried that was "close" but "not
right".

Having nested for-each loops on region, store, and dept and generating
a table row in the dept loop should work. To determine whether to
output a <td title="region"> element or an empty <td> element you need
to test whether the dept position is 1 and the store position is 1.
In the store loop you can set a variable to the value of position() and
test that variable in the dept loop.

In you example xml with region=567 and store=7, dept=9 was before
dept=8, but in the output the corresponding table row for dept=9 was
after the table row for dept=8. Was this intentional, that is, is
the order of table rows in the output not the same as the order in the
xml document?
 
Reply With Quote
 
Stephen Slyman
Guest
Posts: n/a
 
      06-09-2006
Hi,

Many thanks for reply.
Firstly order of nodes is not important for my example.

Like you said, I did nested for-each and generated the table row in the
dept loop however once inside "store" for-each I was repeating
output the region_code within the region. This is something like I'm
doing ** Note title attribute on <td> in my original post was to make
the out more readable *** :

<xsp:template match="region">
<xsp:for-each select="store">
<xsp:for-each select="dept">
<tr>
<xsp:choose>
<xsp:when test="position()=1">
<td> <xsp:value select="../../region_code"/></td>
<td> <xsp:value select="../store_code"/></td>
<td> <xsp:value select="dept_code"/></td>
</xsp:when>
<xsptherwise>
<td> </td>
<td> </td>
<td> <xsp:value select="dept_code"/></td>

</xsptherwise>

</xsp:choose>
</tr>
</xsp:for-each>
</xsp:fior-each>
</xsp:template>
However this produced following:
<tr>
<td>567</td>
<td>345</td>
<td>34</td>
</tr>
<tr>
<td></td>
<td></td>
<td>33</td>
</tr>
<tr>
<td>567</td> <!--- loop repeats the region code and I dont what that
-->
<td>7</td>
<td>8</td>
</tr>
<tr>
<td></td>
<td></td>
<td>9</td>
</tr>

However having read your post, I think can prevent repeat of region
code output within store, by creating a variable for position() of
region for-each loop. That make sense I think. But it's 1am here and my
bed is calling. Will try tomorrow.

Thanks to you (and everyone) for your posts again,

Regards

mark
A. Bolmarcich wrote:
> On 2006-06-08, (E-Mail Removed) <(E-Mail Removed)> wrote:
> > Hi,
> > I could do with some help. I have this XML.
> >
> ><region>
> > <region_code>567</region_code>
> > <store>
> > <store_code>345</store_code>
> > <dept>
> > <dept_code>32</dept_code>
> > </dept>
> > <dept>
> > <dept_code>33</dept_code>
> > </dept>
> > </store>
> > <store>
> > <store_code>7</store_code>
> > <dept>
> > <dept_code>9</dept_code>
> > </dept>
> > <dept>
> > <dept_code>8</dept_code>
> > </dept>
> > </store>
> > <store>
> > <!--- more depts -->
> > </store>
> ></region>
> ><region>
> > <region_code>776</region_code>
> > <!--- more stores -->
> ></region>
> >
> > I need this output to html as follows:
> ><table>
> ><tr>
> ><td title="region">567</td>
> ><td title="store">345</td>
> ><td title="dept">34</td>
> ></tr>
> ><tr>
> ><td></td>
> ><td></td>
> ><td title="dept">33</td>
> ></tr>
> ><tr>
> ><td></td>
> ><td title="store">7</td>
> ><td title="dept">8</td>
> ></tr>
> ><tr>
> ><td></td>
> ><td></td>
> ><td title="dept">9</td>
> ></tr>
> ><tr>
> ><td title="region">776</td>
> > <!-- etc -->
> ></tr>
> ><!-- etc -->
> ></table>
> >
> > I have tried to achieve this with my limited amount of xsl knowledge of
> ><xsl:for-each >, nested <xsl:for-each > and <xsl:if
> > test="position()=1"> and i get close ( by using <xsl:for-each
> > select="dept"> ) but it's not right. I guessing I have to
> > apply-templates/recursion but not sure how.
> > Please if anyone could help that would be great.

>
> It would help if you posted what you tried that was "close" but "not
> right".
>
> Having nested for-each loops on region, store, and dept and generating
> a table row in the dept loop should work. To determine whether to
> output a <td title="region"> element or an empty <td> element you need
> to test whether the dept position is 1 and the store position is 1.
> In the store loop you can set a variable to the value of position() and
> test that variable in the dept loop.
>
> In you example xml with region=567 and store=7, dept=9 was before
> dept=8, but in the output the corresponding table row for dept=9 was
> after the table row for dept=8. Was this intentional, that is, is
> the order of table rows in the output not the same as the order in the
> xml document?


 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Could someone help me out with my namespace problems in my XSD Mike_August XML 5 01-24-2006 12:16 PM
Could someone talk a little about how firefox saves html John Toliver Firefox 0 08-27-2004 06:19 AM
Could Someone Help me on this CODE.... THANKS!! 1111111111 C++ 11 07-12-2004 01:03 AM
If Someone Could Help, Please? Curious HTML 2 04-17-2004 11:31 AM
Could not load type VTFixup Table from assembly Invalid token in v-table fix-up table. David Williams ASP .Net 2 08-12-2003 07:55 AM



Advertisments