473,569 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XSLT: Dynamically create select

5 New Member
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 4354
Dormilich
8,658 Recognized Expert Moderator Expert
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
djnokturnal
5 New Member
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
2687
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 extension responsible for the xslt functions. The problem i have is that the obtained transformation is not the waited one. I try to proccess the...
0
1590
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 sheet follow). It works pretty well, but xsltproc takes an extremely long time to process the data file when it grows fairly large (>1MB). I've read...
4
5230
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 = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); } if (x) {
7
5196
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 name from inbound xml to my internal elements to standardize disparate input. Now I could just create an xslt stylesheet for each possible inbound...
4
2434
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 each property can have more than 1 set of image path nodes What I'm trying to do is transform the property data into the format I require at the...
2
1846
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> <o><n>3</n><si>s</si><v1>7</v1><v2>8</v2><v3>9</v3></o> <o><n>5</n><si>i</si><v1>10</v1><v2>11</v2><v3>12</v3></o>...
4
8934
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 document. My original XML is, <Dates> <Birth Year="1966" />
4
2753
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> <Destination> <Salutation>Hi Mark</Salutation> <UniqueID>4120</UniqueID>
3
4517
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 missed: My template is: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"...
0
7609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6278
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.