473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

XSLT Dynamic Table Column Headers and Rows

This may be a simple one but I have been working on it without joy for
a couple of days now. Reading books and searching these groups hasn't
helped yet either.

I am trying to create a table dynamically with column headers based on
the value of certain nodes and the rows populated ewith the values of
certain child nodes of the same parent.

My xml is something like this:

<docroot>
<target name="target1">
<types>
<type display_name="type1">
<type_mode>"something"</type_mode>
<typetype>"something"</typetype>
</type>
<type display_name="type2">
<type_mode>"something"</type_mode>
<typetype>"something"</typetype>
</type>
<type display_name="type3">
<type_mode>"something"</type_mode>
<typetype>"something"</typetype>
</type>
</types>
</target>
<target name="target2">
<items>
<item display_name="item1">
<item_mode>"something"</item_mode>
<itemitem>"something"</itemitem>
</item>
<item display_name="item2">
<item_mode>"something"</item_mode>
<itemitem>"something"</itemitem>
</item>
<item display_name="item3">
<item_mode>"something"</item_mode>
<itemitem>"something"</itemitem>
</item>
</items>
</target>
</docroot>
The table I am trying to generate will use the "name" attribute of each
"target" as the header for each column. The rows will be populated by
the value of each "item_mode" listed for that "target".

The goal is to list the information gathered for each target in columns
side by side to allow a visual comparison.

To complicate things a little further, the number of targets will vary
as will the number of "items" for each target.

Can anyone help with this at all?

Thanks a lot
CC

Jul 20 '05 #1
5 6736
Apologies, XML should read:

<docroot>
<target name="target1">
<items>
<item display_name="item1">
<item_mode>"something"</item_m*ode>
<itemitem>"something"</itemite*m>
</item>
<item display_name="item2">
<item_mode>"something"</item_m*ode>
<itemitem>"something"</itemite*m>
</item>
<item display_name="item3">
<item_mode>"something"</item_m*ode>
<itemitem>"something"</itemite*m>
</item>
</items>
</target>
<target name="target2">
<items>
<item display_name="item1">
<item_mode>"something"</item_m*ode>
<itemitem>"something"</itemite*m>
</item>
<item display_name="item2">
<item_mode>"something"</item_m*ode>
<itemitem>"something"</itemite*m>
</item>
<item display_name="item3">
<item_mode>"something"</item_m*ode>
<itemitem>"something"</itemite*m>
</item>
</items>
</target>
</docroot>

Jul 20 '05 #2

something like

<xsl:template match="docroot">
<table>
<thead>
<tr>
<xsl:for-each select="target">
<th><xsl:value-of select="@name"/></th>
</xsl:for-each>
</thead>
<tbody>
<xsl:for-each select="target[1]/items/item">
<tr>
<xsl:variable name="c" select="position()"/>
<xsl:for-each select="/docroot/target/items/item[$c]">
<td><xsl:value-of select="item_mode"/></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</tbody>
</table>
</xsl:template>
David
Jul 20 '05 #3
Thanks a lot David. Not only does it work, but makes sense to even me!

Jul 20 '05 #4
David wrote:

<xsl:template match="docroot">
<table>
<thead>
<tr>
<xsl:for-each select="target">
<th><xsl:value-of select="@name"/></th>
</xsl:for-each>
</thead>
<tbody>
<xsl:for-each select="target[1]/items/item">
<tr>
<xsl:variable name="c" select="position()"/>
<xsl:for-each select="/docroot/target/items/item[$c]">
<td><xsl:value-of select="item_mode"/></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</tbody>
</table>
</xsl:template>


This will work with the example XML, but I see two problems based on the
original criteria of multiple targets and multiple items per target.

1) If the first target has fewer item elements than any other target, then
the additional item elements will be skipped. You're going to need to
find the target with the maximum number of item elements.

2) If there are more than two target elements and any of the internal
(not first or last) ones have fewer item elements than the first, then the
subsequent <td> entries will collapse to the left and appear in the wrong
column.

Change:
<xsl:for-each select="/docroot/target/items/item[$c]">
<td><xsl:value-of select="item_mode"/></td>
</xsl:for-each>
To:
<xsl:for-each select="/docroot/target">
<td><xsl:value-of select="items/item[$c]/item_mode"/></td>
</xsl:for-each>

This will insert a blank <td> into the table to maintain the column
integrity. If the <xsl:value-of> throws an error, then you'll need to use
an <xsl:if test="items/item[$c]"> around it.

Normand
Jul 20 '05 #5
Normand

Regarding your first point, I did some testing and you are right about
issues when the first target has fewer items. By jumping out into some
script (msxsl:script) I can determine which target has the most items.
What I can't do is use this information in building the table. Ideally
the order in which the targets are presented in the table will be the
same order they appear in the xml file.

Any ideas?

Thanks again

Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: dschectman | last post by:
I have an interesting issue. I need to implement a dynamic table to mimic a select list. Each time you double click from the master list, a row is added to the list of selected items. The list...
7
by: Doug Heeren | last post by:
I have the following section of VB.NET code that transforms a simple dataset into an Excel xml workbook. It works fine for < 50 rows or so, but I have about 8,000 rows I need to transform. Is there...
2
by: Scamjunk | last post by:
I have been desperately looking for a treeview-type solution for my problem for the past three weeks and have been greatly unsuccessful. I am totally new to the world of XSLT and I *don't know*...
0
by: DAnne | last post by:
Hi, I'm very new to xslt and this is my first time posting to a Forum so please forgive me if I transgress any protocols. I have to do a tally report. This report is divided up into sections....
0
by: Eniac | last post by:
Hi, I've been working on a custom user control that needs to be modified and the validation is causing me headaches. The control used to generate a table of 4 rows x 7 columns to display all...
0
by: miamikk | last post by:
I am XML newbie. I have question about inserting dynamic text in the header of HTML table. This is the site I have created (Only Report Type 1 is working)...
7
by: TG | last post by:
hi! I am trying to create a sql server table from an excel sheet. Here is the code I have: 'This procedure the xlsx file and dumps it to a table in SQL Server
2
by: djnokturnal | last post by:
Hey guys / gals, First time posting and of course I am sure it is something that has been answered 100 times but for some reason I just cant find the answer :) First off here is the structure...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.