473,395 Members | 1,869 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,395 software developers and data experts.

XSLT: Dynamically create select

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
Dec 17 '08 #1
2 4340
Dormilich
8,658 Expert Mod 8TB
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*
Dec 18 '08 #2
Thanks, it is most appreciated :)
Dec 23 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Sergio del Amo | last post by:
Hi, I use the xslt functions provided by php. I am running in my computer the package xampp(www.apachefriends.org) which includes php/apache/mysql .. In this package the php includes the sablotron...
0
by: alex | last post by:
I'm new to xslt, and I am attempting to use it to produce a comma-separated-value file from a large, dynamically-generated data file formatted in xml (examples of the xml file and my xslt style...
4
by: Stephen | last post by:
I have the following that outputs an xml file to a div using ajax: <script type="text/javascript"> function ajaxXML(url,control_id){ if (document.getElementById) { var x =...
7
by: pintihar | last post by:
Hi, As a follow on from an earlier post I have another question about xslt. Is it possible to create the stylsheet programatically? Is this sensible? In the first phase I needed to map element...
4
by: Joe Gass | last post by:
After a bit of research I think what I'm trying to do with xslt can't be done, but someone may have some pointers. I have 2 sections in my xml - 1 with properties the 2nd with image path data...
2
by: Michael Hamm | last post by:
I have the following XML file (simplified from the actual): <r> <o><n>1</n><si>s</si><v1>1</v1><v2>2</v2><v3>3</v3></o> <o><n>2</n><si>i</si><v1>4</v1><v2>5</v2><v3>6</v3></o>...
4
by: David S. Alexander | last post by:
How can I do simple subtraction in an XSLT. I want to read a few attribute values from an XML document, calculate their difference, and transform that value to an attribute in the XML output...
4
by: mark4asp | last post by:
I want to write a xslt template to create a xhtml 1.0 (transitional) file which will be sent in as email. Here is a typical xml data file: <BatchEmail> <Domain>www.myDomain.com</Domain>...
3
by: joelkeepup | last post by:
Hi, im trying to create a text email message using xslt template , the transforms work great, but the newlines and whitespace in the xslt doc are removed. Is there a setting somewhere I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.