
July 20th, 2005, 09:39 AM
| | | 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 | 
July 20th, 2005, 09:39 AM
| | | Re: XSLT Dynamic Table Column Headers and Rows
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> | 
July 20th, 2005, 09:39 AM
| | | Re: XSLT Dynamic Table Column Headers and Rows
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 | 
July 20th, 2005, 09:39 AM
| | | Re: XSLT Dynamic Table Column Headers and Rows
Thanks a lot David. Not only does it work, but makes sense to even me! | 
July 20th, 2005, 09:39 AM
| | | Re: XSLT Dynamic Table Column Headers and Rows
David wrote:[color=blue]
>
> <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>
>[/color]
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 | 
July 20th, 2005, 09:39 AM
| | | Re: XSLT Dynamic Table Column Headers and Rows
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 |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|