XSLT Dynamic Table Column Headers and Rows

C

ccjunk

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
 
C

ccjunk

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

David Carlisle

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
 
N

nstonge

David said:
<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
 
C

ccjunk

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top