Connecting Tech Pros Worldwide Help | Site Map

XSLT: Dynamically create select

Newbie
 
Join Date: Dec 2008
Posts: 5
#1: Dec 17 '08
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 of the xml:

Expand|Select|Wrap|Line Numbers
  1. <Stats>
  2.   <Structure>
  3.     <Column DisplayName="GP" FieldName="GamesPlayed" Priority="1" />
  4.     <Column DisplayName="G" FieldName="Goals" Priority="2" />
  5.     <Column DisplayName="A" FieldName="Assists" Priority="3" />
  6.     <Column DisplayName="PTS" FieldName="Points" Priority="4" />
  7.   </Structure>
  8.   <Players>
  9.     <Player Row="1" Rank="1" Name="Jobs, Steve" GamesPlayed="33" Goals="8" Assists="3" Points="11" />
  10.     <Player Row="2" Rank="1" Name="Gate, Bill" GamesPlayed="33" Goals="8" Assists="9" Points="17" />
  11.     <Player Row="3" Rank="1" Name="Ballmer, Steve" GamesPlayed="33" Goals="5" Assists="12" Points="17" />
  12.     <Player Row="4" Rank="1" Name="Jordan, Mike" GamesPlayed="33" Goals="3" Assists="12" Points="15" />
  13.     <Player Row="5" Rank="1" Name="Cent, 50" GamesPlayed="33" Goals="0" Assists="6" Points="6" />
  14.   </Players>
  15. </Stats>
SInce everything in that block is dynamic, the HTML table I am attempting to render from this XML has to just as dynamic.

I am using the "Structure" element to build the column headers of the table.

This is where I am coming up with the problem...

To make the data cells, I thought the best thing to do would be to loop through the "Structure" elem for each Player in the Players element. The problem is how to dynamically call the attributes of the current Player element.

Here is the XSLT:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
  4. >
  5.     <xsl:output method="xml" indent="yes"/>
  6.  
  7.     <xsl:template match="/">
  8.         <xsl:apply-templates select="Stats"></xsl:apply-templates>
  9.     </xsl:template>
  10.  
  11.     <xsl:template match="Stats">
  12.         <table>
  13.             <xsl:apply-templates select="Structure"></xsl:apply-templates>
  14.             <xsl:apply-templates select="Players"></xsl:apply-templates>
  15.         </table>
  16.     </xsl:template>
  17.  
  18.     <xsl:template match="Structure">
  19.         <tr>
  20.             <th>Row</th>
  21.             <th>Rank</th>
  22.             <th>Name</th>
  23.             <xsl:apply-templates select="Column"></xsl:apply-templates>
  24.         </tr>
  25.     </xsl:template>
  26.  
  27.     <xsl:template match="Column">
  28.         <th>
  29.             <xsl:value-of select="@DisplayName" />
  30.         </th>
  31.     </xsl:template>
  32.  
  33.     <xsl:template match="Players">
  34.         <xsl:apply-templates select="Player"></xsl:apply-templates>
  35.     </xsl:template>
  36.  
  37.     <xsl:template match="Player">
  38.         <xsl:variable name="player" select="."/>
  39.         <tr>
  40.             <th>
  41.                 <xsl:value-of select="@Row" />
  42.             </th>
  43.             <th>
  44.                 <xsl:value-of select="@Rank" />
  45.             </th>
  46.             <th>
  47.                 <xsl:value-of select="@Name" />
  48.             </th>
  49.             <xsl:for-each select="//Stats/Structure/Column">
  50.                 <xsl:variable name="column" select="@FieldName"/>
  51.                 <td>
  52.                     <!--
  53.                         HERE IS THE PROBLEM
  54.                         I have tried a bunch of different things... but this is the last thing i tried
  55.                     -->
  56.                     <xsl:value-of select="concat($player, '/@", @FieldName />                    
  57.                 </td>
  58.             </xsl:for-each>
  59.         </tr>
  60.     </xsl:template>
  61.  
  62. </xsl:stylesheet>
I am doing this server side using C#. Anyone have any ideas on how I can get this to work without re-writing the structure of the XML. If not, what changes in the XML and XSL would you make to get it to work.

Thanks in advance!!!
Andrew
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,621
#2: Dec 18 '08

re: XSLT: Dynamically create select


took me a while to understand what you wanted...... the crucial part of the solution is to get all player attributes an look if the name is matching the column.
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="Player">
  2.         <xsl:variable name="player" select="."/>
  3.         <tr>
  4.             <td>
  5.                 <xsl:value-of select="@Row" />
  6.             </td>
  7.             <td>
  8.                 <xsl:value-of select="@Rank" />
  9.             </td>
  10.             <td>
  11.                 <xsl:value-of select="@Name" />
  12.             </td>
  13. <xsl:for-each select="//Column">
  14.             <td>
  15.                 <xsl:value-of select="$player/@*[name(.) = current()/@FieldName]" />                                     
  16.             </td>
  17. </xsl:for-each>
  18.         </tr>
  19.     </xsl:template>
PS: the xml gods strike again *g*
Newbie
 
Join Date: Dec 2008
Posts: 5
#3: Dec 23 '08

re: XSLT: Dynamically create select


Thanks, it is most appreciated :)
Reply

Tags
xml, xsl, xslt