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

XSL Heartache - Newbie

I'm new to XML but an old hand at c++ lisp and vba programming. I'm trying to process the following xml data:

Expand|Select|Wrap|Line Numbers
  1.     <abilities>
  2.         <ability>
  3.             <name>Strength</name>
  4.             <abbr>STR</abbr>
  5.             <base>15</base>
  6.             <modifier>+2</modifier>
  7.         </ability>
  8.         <ability>
  9.             <name>Dexterity</name>
  10.             <abbr>DEX</abbr>
  11.             <base>14</base>
  12.             <modifier>+2</modifier>
  13.         </ability>
  14.         <ability>
  15.             <name>Constitution</name>
  16.             <abbr>CON</abbr>
  17.             <base>13</base>
  18.             <modifier>+1</modifier>
  19.         </ability>
  20.         <ability>
  21.             <name>Intelligence</name>
  22.             <abbr>INT</abbr>
  23.             <base>10</base>
  24.             <modifier>+0</modifier>
  25.         </ability>
  26.         <ability>
  27.             <name>Wisdom</name>
  28.             <abbr>WIS</abbr>
  29.             <base>12</base>
  30.             <modifier>+1</modifier>
  31.         </ability>
  32.         <ability>
  33.             <name>Charisma</name>
  34.             <abbr>CHA</abbr>
  35.             <base>8</base>
  36.             <modifier>-1</modifier>
  37.         </ability>
  38.     </abilities>
There is nothing wrong with the structure of the xml file above. The problem lies in the xsl file below. Each ability is to be printed on its own line in its own color, which I've managed to do using <xsl:choose>. What I can't do is condition the color of the printed <modifier> text. I want the modifier to print red if negative, black if zero (+0) and green if positive. The relevant fragment of the xsl file appears below.


Expand|Select|Wrap|Line Numbers
  1.             <xsl:for-each select="ability">
  2.                 <tr>
  3.                      <xsl:choose>
  4.                         <xsl:when test ="abbr = 'STR'">
  5.                             <td align="center" bgcolor="#CC0033" class="v10w">
  6.                                 <b>
  7.                                     <xsl:value-of select="abbr"/>
  8.                                 </b>
  9.                                 <br/>
  10.                             </td>
  11.                             <td align="center" style="border: 3px solid #CC0033" bgcolor="#FFAABF" 
  12.                                 class="v10">
  13.                                 <b>
  14.                                     <xsl:value-of select="base"/>
  15.                                 </b>
  16.                                 <br/>
  17.                             </td>
  18.                             <td align="center" style="border: 3px solid #CC0033" bgcolor="#FFAABF"
  19.                             class="v10g">
  20.                                   <b>
  21.                                     <xsl:value-of select="modifier"/>
  22.                                 </b>
  23.                                 <br/>                               
  24.                             </td>
  25.                              <td align="center" style="border: 3px solid #CC0033" bgcolor="#FFAABF">
  26.                                 <br/>
  27.                             </td>
  28.                             <td align="center" style="border: 3px solid #CC0033" bgcolor="#FFAABF">
  29.                                 <br/>
  30.                             </td>
  31.                         </xsl:when>
  32.                         <xsl:when test ="abbr = 'DEX'">
  33.                             <td align="center" bgcolor="#FF6600" class="v10w">
  34.                                 <b>
  35.                                     <xsl:value-of select="abbr"/>
  36.                                 </b>
  37.                                 <br/>
  38.                             </td>
  39.                             <td align="center" style="border: 3px solid #FF6600" bgcolor="#FFCCAA" 
  40.                             class="v10">
  41.                                 <b>
  42.                                     <xsl:value-of select="base"/>
  43.                                 </b>
  44.                                 <br/>
  45.                             </td>
  46.                             <td align="center" style="border: 3px solid #FF6600" bgcolor="#FFCCAA" 
  47.                             class="v10">
  48.                                 <b>
  49.                                     <xsl:value-of select="modifier"/>
  50.                                 </b>
  51.                                 <br/>
  52.                             </td>
  53.                             <td align="center" style="border: 3px solid #FF6600" bgcolor="#FFCCAA">
  54.                                 <br/>
  55.                             </td>
  56.                             <td align="center" style="border: 3px solid #FF6600" bgcolor="#FFCCAA">
  57.                                 <br/>
  58.                             </td>
  59.                         </xsl:when>
  60.                         <xsl:when test ="abbr = 'CON'">
  61.                             <td align="center" bgcolor="#FFCC00" class="v10w">
  62.                                 <b>
  63.                                     <xsl:value-of select="abbr"/>
  64.                                 </b>
  65.                                 <br/>
  66.                             </td>
  67.                             <td align="center" style="border: 3px solid #FFCC00" bgcolor="#FFEEAA" 
  68.                             class="v10">
  69.                                 <b>
  70.                                     <xsl:value-of select="base"/>
  71.                                 </b>
  72.                                 <br/>
  73.                             </td>
  74.                             <td align="center" style="border: 3px solid #FFCC00" bgcolor="#FFEEAA" 
  75.                             class="v10">
  76.                                 <b>
  77.                                     <xsl:value-of select="modifier"/>
  78.                                 </b>
  79.                                 <br/>
  80.                             </td>
  81.                             <td align="center" style="border: 3px solid #FFCC00" bgcolor="#FFEEAA">
  82.                                 <br/>
  83.                             </td>
  84.                             <td align="center" style="border: 3px solid #FFCC00" bgcolor="#FFEEAA">
  85.                                 <br/>
  86.                             </td>
  87.                         </xsl:when>
  88.                         <xsl:when test ="abbr = 'INT'">
  89.                             <td align="center" bgcolor="#339933" class="v10w">
  90.                                 <b>
  91.                                     <xsl:value-of select="abbr"/>
  92.                                 </b>
  93.                                 <br/>
  94.                             </td>
  95.                             <td align="center" style="border: 3px solid #339933" bgcolor="#BFEABF" 
  96.                             class="v10">
  97.                                 <b>
  98.                                     <xsl:value-of select="base"/>
  99.                                 </b>
  100.                                 <br/>
  101.                             </td>
  102.                             <td align="center" style="border: 3px solid #339933" bgcolor="#BFEABF" 
  103.                             class="v10">
  104.                                 <b>
  105.                                     <xsl:value-of select="modifier"/>
  106.                                 </b>
  107.                                 <br/>
  108.                             </td>
  109.                             <td align="center" style="border: 3px solid #339933" bgcolor="#BFEABF">
  110.                                 <br/>
  111.                             </td>
  112.                             <td align="center" style="border: 3px solid #339933" bgcolor="#BFEABF">
  113.                                 <br/>
  114.                             </td>
  115.                         </xsl:when>
  116.                         <xsl:when test ="abbr = 'WIS'">
  117.                             <td align="center" bgcolor="#336699" class="v10w">
  118.                                 <b>
  119.                                     <xsl:value-of select="abbr"/>
  120.                                 </b>
  121.                                 <br/>
  122.                             </td>
  123.                             <td align="center" style="border: 3px solid #336699" bgcolor="#BFD5EA" 
  124.                             class="v10">
  125.                                 <b>
  126.                                     <xsl:value-of select="base"/>
  127.                                 </b>
  128.                                 <br/>
  129.                             </td>
  130.                             <td align="center" style="border: 3px solid #336699" bgcolor="#BFD5EA" 
  131.                             class="v10">
  132.                                 <b>
  133.                                     <xsl:value-of select="modifier"/>
  134.                                 </b>
  135.                                 <br/>
  136.                             </td>
  137.                             <td align="center" style="border: 3px solid #336699" bgcolor="#BFD5EA">
  138.                                 <br/>
  139.                             </td>
  140.                             <td align="center" style="border: 3px solid #336699" bgcolor="#BFD5EA">
  141.                                 <br/>
  142.                             </td>
  143.                         </xsl:when>
  144.                         <xsl:otherwise>
  145.                             <td align="center" bgcolor="#663399" class="v10w">
  146.                                 <b>
  147.                                     <xsl:value-of select="abbr"/>
  148.                                 </b>
  149.                                 <br/>
  150.                             </td>
  151.                             <td align="center" style="border: 3px solid #663399" bgcolor="#D5BFEA" 
  152.                             class="v10">
  153.                                 <b>
  154.                                     <xsl:value-of select="base"/>
  155.                                 </b>
  156.                                 <br/>
  157.                             </td>
  158.                             <td align="center" style="border: 3px solid #663399" bgcolor="#D5BFEA" 
  159.                             class="v10">
  160.                                 <b>
  161.                                     <xsl:value-of select="modifier"/>
  162.                                 </b>
  163.                                 <br/>
  164.                             </td>
  165.                             <td align="center" style="border: 3px solid #663399" bgcolor="#D5BFEA">
  166.                                 <br/>
  167.                             </td>
  168.                             <td align="center" style="border: 3px solid #663399" bgcolor="#D5BFEA">
  169.                                 <br/>
  170.                             </td>
  171.                         </xsl:otherwise>
  172.                     </xsl:choose>
  173.                 </tr>
  174.             </xsl:for-each>
  175.         </table>
  176.     </xsl:template>
The line that controls the text color is:
<td align="center" style="border: 3px solid #663399" bgcolor="#D5BFEA" class="v10">

This is the snippet for the v10 style:

Expand|Select|Wrap|Line Numbers
  1.                         .v10 {
  2.                         font-family: Verdana;
  3.                         font-size:10pt;
  4.  
  5.     color:black;
  6.                         }
I tried to do it using a nested <xsl:choose> statement and by creating a v10r for red and v10g for green text. I think I was failing in the test= statement. I just don't know how to test the string.

Any help will be greatly appreciated.

virgil, out!
Jan 4 '08 #1
6 1449
jkmyoung
2,057 Expert 2GB
Modifier color:
Expand|Select|Wrap|Line Numbers
  1. <xsl:variable name="modClass">
  2.     <xsl:choose>
  3.         <xsl:when test="modifier &amp;gt; 0">v10g</xsl:when>
  4.         <xsl:when test="modifier &amp;lt; 0">v10r</xsl:when>
  5.         <xsl:otherwise>v10</xsl:otherwise>
  6.     </xsl:choose>
  7. </xsl:variable>
  8. ....
  9.  
  10. and then
  11. class="{$modClass}"
  12.  
In order to decrease repeated code, and make it more manageable, I suggest abstraction like so:

1. Put the following in a variable, or in your stylesheet somewhere: (I've chosen to put this at the end of the stylesheet)
Expand|Select|Wrap|Line Numbers
  1. <my:mapping xmlns:my="internal">
  2.     <ability abbr="STR" bg="#CC0033" bg2="#FFAABF"/>
  3.     <ability abbr="DEX" bg="#FF6600" bg2="#FFCCAA"/>
  4.     <ability abbr="CON" bg="#FFCC00" bg2="#FFEEAA"/>
  5.     <ability abbr="INT" bg="#339933" bg2="#BFEABF"/>
  6.     <ability abbr="WIS" bg="#336699" bg2="#BFD53A"/>
  7.     <ability abbr="CHA" bg="#663399" bg2="#D5BFEA"/>
  8. </my:mapping>
  9.  
2. Look up the node for each attribute:
Expand|Select|Wrap|Line Numbers
  1. <xsl:variable name="styleNode" select="document('')//ability[@abbr=current()/abbr]"/>
3. Use this node in a single piece of code. All put together, it looks something like:
Expand|Select|Wrap|Line Numbers
  1. <xsl:for-each select="ability">
  2.     <tr>
  3.         <xsl:variable name="styleNode" select="document('')//ability[@abbr=current()/abbr]"/>
  4.         <xsl:variable name="bgcolor" select="$styleNode/@bg"/>
  5.         <xsl:variable name="style" select="concat('border: 3px solid ', $bgcolor)"/>
  6.         <xsl:variable name="bgcolor2" select="$styleNode/@bg2"/>
  7.         <xsl:variable name="modClass">
  8.             <xsl:choose>
  9.                 <xsl:when test="modifier &amp;gt; 0">v10g</xsl:when>
  10.                 <xsl:when test="modifier &amp;lt; 0">v10r</xsl:when>
  11.                 <xsl:otherwise>v10</xsl:otherwise>
  12.             </xsl:choose>
  13.         </xsl:variable>
  14.         <td align="center" bgcolor="{$bgcolor}" class="v10w">
  15.             <b><xsl:value-of select="abbr"/></b><br/>
  16.         </td>
  17.         <td align="center" style="{$style}" bgcolor="{$bgcolor2}" class="v10">
  18.             <b><xsl:value-of select="base"/></b><br/>
  19.         </td>
  20.         <td align="center" style="{$style}" bgcolor="{$bgcolor2}" class="{$modClass}">
  21.             <b><xsl:value-of select="modifier"/></b><br/>
  22.         </td>
  23.         <td align="center" style="{$style}" bgcolor="{$bgcolor2}">
  24.             <br/>
  25.         </td>
  26.         <td align="center" style="{$style}" bgcolor="{$bgcolor2}">
  27.             <br/>
  28.         </td>
  29.     </tr>
  30. </xsl:for-each>
  31.  
No more ridiculous & unwieldly choose clauses.

--edit darn forum escapes entities even in code clauses...
Jan 4 '08 #2
Hi jkmyoung,

Thanks for your reply. I've used the code and it is working fine. I must have the worst XML book there is: Teach Yourself XML in 24 Hours. It makes no mention of my:mapping whatsoever. Or are all the books bad?

A couple more questions:

1. How does the node actually reference my:mapping? There doesn't seem to be an explicit reference.

2. I need to use colors frequently in the stylesheet so I want to do something like:

Expand|Select|Wrap|Line Numbers
  1. <!-- Schedule of Colors -->
  2.   <xsl:variable name="redborder" select=" ' #CC0033 ' "/>
  3.  
  4. <my:mapping xmlns:my="internal">
  5.     <ability abbr="STR" bg="($redborder)" bg2="#FFAABF"/>
  6.     <ability abbr="DEX" bg="#FF6600" bg2="#FFCCAA"/>
  7.     <ability abbr="CON" bg="#FFCC00" bg2="#FFEEAA"/>
  8.     <ability abbr="INT" bg="#339933" bg2="#BFEABF"/>
  9.     <ability abbr="WIS" bg="#336699" bg2="#BFD5EA"/>
  10.     <ability abbr="CHA" bg="#663399" bg2="#D5BFEA"/>
  11. </my:mapping>
This would allow me to specify one color for all "redborder"s throughout the sheet. It works but somehow the color changes!!!! Instead of a dark red (#CC0033) it prints a sky blue !!!! Do you know why this might be happening?


Thanks again for all your help.

Regards

virgil
Jan 6 '08 #3
jkmyoung
2,057 Expert 2GB
my:mapping is somewhat of a neat trick some developers discovered when using XSLT. I picked up the trick in some random forum. Really, this just allows you to put an internal reference xml inside your stylesheet without intefering with the xslt. You could put it in an external xml sheet if wanted.

How it is referenced: document function.
document('') references the xslt stylesheet as an xml document.
//ability references all the ability elements in the stylesheet.

What you're trying to do in 2, is double evaluation of an xpath. Currently, the bg colors are manually hardcoded as part of the stylesheet. You could change the actual ability node inside the mapping node.

When you say need to use colors frequently, do you mean you're changing the the colors frequently??
Jan 7 '08 #4
Hi jkmyoung,

When I say I'm using colors frequently I mean that I'm wanting to specify, say, redborder here, redbackground there, orangetext here ... and so on. Rather than doing a search and replace all the time to replace, say, #CC0033 with #CC11F3, I'd like to have a color schedule somewhere:

redbackground = " ' #CC0033 ' "
redborder = ...
redtext = ...

etc;

Then I can tinker with the colors to improve the presentation of the sheet without having to do a whole lot of search and replace ... ie: I only want to change the value of redbackground once and have it update automatically everywhere on the sheet.

Have you got any ideas?

Thanks in advance

virgil
Jan 8 '08 #5
jkmyoung
2,057 Expert 2GB
Oh ok, so you're using redborder somewhere else besides in ability.

As it is currently, the text ($redborder) is input to the border, as opposed to "#CC0033"

The only way I can see is to use css to do such a thing. Either that or a global search/replace on your document.
Jan 8 '08 #6
Thanks jkmyoung.

I'll look into it. I get what you were saying about the double eval.

No need to reply.

Thanks again.

virgil, out!
Jan 9 '08 #7

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

Similar topics

1
by: virgiloz | last post by:
Continuing on from my heartache post earlier, can I nest a <choose> element inside of: <td align="center" style="border: 3px solid #CC0033" bgcolor="#FFAABF" class="v10"> Ie: is the following...
0
by: Twayne | last post by:
Twayne wrote: That's the verification I was looking for I think. There are times that doesn't seem to be so, but perhaps I'll looking at the wrong side of the tree in the wrong forest ... I...
5
by: Banibrata Dutta | last post by:
Hi, I've gone through the list of "language differences" between 2.3 / 2.4 & 2.5 of CPython. I've spend around 2 weeks now, learning v2.5 of CPython, and I consider myself still very very...
16
by: Raxit | last post by:
Hi, i was reading/learning some hello world program in python. I think its very simillar to Java/C++/C#. What's different (except syntax) ? what can i do easily with python which is not easy...
10
by: Peter Michaux | last post by:
On May 14, 8:55 pm, Prisoner at War <prisoner_at_...@yahoo.comwrote: Get it from the library. I cannot imagine needing to own an HTML book. There are plenty of good references on the web....
2
by: r_ahimsa_m | last post by:
Could you recommend me some free JavaScript validator? I was using JSlint but it reports nonsense errors. Please help. Thanks. /RAM/
6
by: raylopez99 | last post by:
Will ASP.NET 3.0 work under Visual Studio 2005? And what is a good newbie ASP.NET book? Subject: Will ASP.NET 3.0 work under Visual Studio 2005? And what is a good newbie ASP.NET book? My...
3
Lokean
by: Lokean | last post by:
Sorry for this newbie question, this is not my realm of expertese. I have searched google, tried several applications that claim they can do this, such as Mapforce, which I found confusing, to...
5
by: Dave | last post by:
I am new to Visual Web Developer 2005 Expres. I am using absolute positioning and every time I add a button control to my web form its width extends all the way to the edge of the page. IOW I...
5
by: SharkD | last post by:
Hi! I'm a total newbie when it comes to ASP (or any type of server-side programming). I want to start a project that does the following: 1. query Wikipedia for information regarding...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.