BeataG Posted April 25, 2009 Share Posted April 25, 2009 Hi,I'm very beginner user of XML and XSL, and therefore I'd like to ask you for little help with conditional formatting. I have the following XML code: <?xml version="1.0" encoding ="windows-1250"?><?xml-stylesheet type="text/xsl" href="details.xsl"?><semester><day_1><hour>8.00 - 9.30</hour><subject>Gymnastics</subject><form>lecture</form><room>145C</room></day_1><day_1><hour>9.45 - 11.15</hour><subject>Object programming</subject><form>lab</form><room>427</room></day_1><day_1><hour>11.45 - 13.15</hour><subject>Team sports</subject><form>classes</form><room>court</room></day_1><day_2><hour>8.00 - 9.30</hour><subject>Databases</subject><form>lecture</form><room>458D</room></day_2><day_2><hour>10.00 - 11.30</hour><subject>Team sports</subject><form>classes</form><room>court</room></day_2><day_2><hour>11.45 - 13.15</hour><subject>Object programming</subject><form>classes</form><room>78C</room></day_2></semester> and XSL code: <?xml version="1.0" encoding="windows-1250"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><html><body scroll="no" bgcolor="lightgreen"><font size="1pt"><center><br></br><br></br><p><b><u><font size="2">TIMETABLE</font></u></b></p><p>Studies: <b>full-time</b></p><p>Academic year: <b>2008/2009</b></p><p>Semester: <b>summer</b></p><br></br><table border="2" width="60%" height="25%" bgcolor="lightgrey"><tr><th><font size="1pt"><center>Monday</center></font></th><th><font size="1pt"><center>Subject</center></font></th><th><font size="1pt"><center>Form</center></font></th><th><font size="1pt"><center>Room</center></font></th></tr><xsl:for-each select="semester/day_1"><tr><td width="20%"><font size="1pt"><center><xsl:value-of select="hour"/></center></font></td><xsl:choose><xsl:when test="subject='Gymnastics'"><td width="35%" bgcolor="lightyellow"><font size="1pt"><xsl:value-of select="subject"/></font></td></xsl:when><xsl:when test="subject='Object programming'"><td width="35%" bgcolor="lightblue"><font size="1pt"><xsl:value-of select="subject"/></font></td></xsl:when><xsl:otherwise><td width="35%" bgcolor="lightgreen"><font size="1pt"><xsl:value-of select="subject"/></font></td></xsl:otherwise></xsl:choose><xsl:choose><xsl:when test="form='lecture'"><td width="15%" style="font-weight:bold"><font size="1pt"><center><xsl:value-of select="form"/></center></font></td></xsl:when><xsl:when test="form='lab'"><td width="15%" style="font-style:italic"><font size="1pt"><center><xsl:value-of select="form"/></center></font></td></xsl:when><xsl:otherwise><td width="15%" style="text-decoration:underline"><font size="1pt"><center><xsl:value-of select="form"/></center></font></td></xsl:otherwise></xsl:choose><td width="15%"><font size="1pt"><center><xsl:value-of select="room"/></center></font></td></tr></xsl:for-each></table><br></br><br></br><table border="2" width="60%" height="25%" bgcolor="lightgrey"><tr><th><font size="1pt"><center>Tuesday</center></font></th><th><font size="1pt"><center>Subject</center></font></th><th><font size="1pt"><center>Form</center></font></th><th><font size="1pt"><center>Room</center></font></th></tr><xsl:for-each select="semester/day_2"><tr><td width="20%"><font size="1pt"><center><xsl:value-of select="hour"/></center></font></td><td width="35%"><font size="1pt"><xsl:value-of select="subject"/></font></td><td width="15%"><font size="1pt"><center><xsl:value-of select="form"/></center></font></td><td width="15%"><font size="1pt"><center><xsl:value-of select="room"/></center></font></td></tr></xsl:for-each></table></center></font></body></html></xsl:template></xsl:stylesheet> My goal is to format these tables when the following conditions are met:1) The same subject have the same background colors (let's say: Object programming - lightblue, Gymnastics - lightyellow, Team Sports -lightgreen). 2) The same form of classes have the same font styles (e.g. lecture - bold, classes - underline, lab - italics).I've tried to use xsl:choose to format adequate table ROWS, however, I only managed to format table DATA.Is there any possibility to modify my xsl code to obtain table rows formatted, not only table data? Could anybody help me in modyfing XSL code to get desired results?Tahnk you in advance for any helpRegards.I've noticed that I write the post in the wrong section and sorry for this, nevertheless I hope somebody help me. Link to comment Share on other sites More sharing options...
boen_robot Posted April 25, 2009 Share Posted April 25, 2009 The easiest way is to simply give an attribute to <tr> with xsl:attribute, like so: <xsl:when test="subject='Gymnastics'"><xsl:attribute name="bgcolor">lightyellow</xsl:attribute><td width="35%"><font size="1pt"><xsl:value-of select="subject"/></font></td></xsl:when> (repeated for all xsl:when elements)But I wouldn't call it the best way... far from it. I would suggest you learn CSS, and move your formatting there. Then with XSLT, just add the proper class-es to the proper elements. Link to comment Share on other sites More sharing options...
BeataG Posted April 25, 2009 Author Share Posted April 25, 2009 Thank you for your suggestions. I've tried to correct it the way you said, but I've failed.Anyway, I'm afraid I still don't know where exactly I should put xsl:attributes.If you (or anyone else) could look at the code below and give me some advice, I'd be grateful. <?xml version="1.0" encoding="windows-1250"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><html><body scroll="no" bgcolor="lightgreen"><font size="1pt"><center><br></br><br></br><p><b><u><font size="2">TIMETABLE</font></u></b></p><p>Studies: <b>full-time</b></p><p>Academic year: <b>2008/2009</b></p><p>Semester: <b>summer</b></p><br></br><table border="2" width="60%" height="25%" bgcolor="lightgrey"><tr><th><font size="1pt"><center>Monday</center></font></th><th><font size="1pt"><center>Subject</center></font></th><th><font size="1pt"><center>Form</center></font></th><th><font size="1pt"><center>Room</center></font></th></tr><xsl:for-each select="semester/day_1"><tr><td width="20%"><font size="1pt"><center><xsl:value-of select="hour"/></center></font></td><xsl:choose><xsl:when test="subject='Gymnastics'"><xsl:attribute name="bgcolor">lightyellow</xsl:attribute><td width="35%"><font size="1pt"><xsl:value-of select="subject"/></font></td></xsl:when><xsl:when test="subject='Object programming'"><xsl:attribute name="bgcolor">lightblue</xsl:attribute><td width="35%"><font size="1pt"><xsl:value-of select="subject"/></font></td></xsl:when><xsl:otherwise><xsl:attribute name="bgcolor">lightgreen</xsl:attribute><td width="35%"><font size="1pt"><xsl:value-of select="subject"/></font></td></xsl:otherwise></xsl:choose><xsl:choose><xsl:when test="form='lecture'"><xsl:attribute name="font-weight">bold</xsl:attribute><td width="15%"><font size="1pt"><center><xsl:value-of select="form"/></center></font></td></xsl:when><xsl:when test="form='lab'"><xsl:attribute name="font-style">italic</xsl:attribute><td width="15%"><font size="1pt"><center><xsl:value-of select="form"/></center></font></td></xsl:when><xsl:otherwise><xsl:attribute name="text-decoration">underline</xsl:attribute><td width="15%"><font size="1pt"><center><xsl:value-of select="form"/></center></font></td></xsl:otherwise></xsl:choose><td width="15%"><font size="1pt"><center><xsl:value-of select="room"/></center></font></td></tr></xsl:for-each></table><br></br><br></br><table border="2" width="60%" height="25%" bgcolor="lightgrey"><tr><th><font size="1pt"><center>Tuesday</center></font></th><th><font size="1pt"><center>Subject</center></font></th><th><font size="1pt"><center>Form</center></font></th><th><font size="1pt"><center>Room</center></font></th></tr><xsl:for-each select="semester/day_2"><tr><td width="20%"><font size="1pt"><center><xsl:value-of select="hour"/></center></font></td><td width="35%"><font size="1pt"><xsl:value-of select="subject"/></font></td><td width="15%"><font size="1pt"><center><xsl:value-of select="form"/></center></font></td><td width="15%"><font size="1pt"><center><xsl:value-of select="room"/></center></font></td></tr></xsl:for-each></table></center></font></body></html></xsl:template></xsl:stylesheet> Thank you Link to comment Share on other sites More sharing options...
BeataG Posted April 26, 2009 Author Share Posted April 26, 2009 I've almost achieved my goal, almost beacuse the first conditional formatting works fine, the second NOT.Maybe the problem is WHERE exactly or HOW I should write the second block of code with "xsl:attribute".I put the fragment that seems to be incorrect and I'd like to ask for help in suggesting how it should look like. <?xml version="1.0" encoding="windows-1250"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><html><body scroll="no" bgcolor="lightgreen"><font size="1pt"><center><br></br><br></br><p><b><u><font size="2">TIMETABLE</font></u></b></p><p>Studies: <b>full-time</b></p><p>Academic year: <b>2008/2009</b></p><p>Semester: <b>summer</b></p><br></br><table border="2" width="60%" height="25%" bgcolor="lightgrey"><tr><th><font size="1pt"><center>Monday</center></font></th><th><font size="1pt"><center>Subject</center></font></th><th><font size="1pt"><center>Form</center></font></th><th><font size="1pt"><center>Room</center></font></th></tr><xsl:for-each select="semester/day_1"><tr><xsl:choose> <xsl:when test="@form='lecture'"> <xsl:attribute name="style">font-weight:bold; </xsl:attribute> </xsl:when> <xsl:when test="@form='lab'"> <xsl:attribute name="style">font-style:italic; </xsl:attribute> </xsl:when> <xsl:otherwise> <xsl:attribute name="style">text-decoration:underline; </xsl:attribute> </xsl:otherwise></xsl:choose><xsl:choose> <xsl:when test="@subject='Gymnastics'"> <xsl:attribute name="background-color">lightyellow </xsl:attribute> </xsl:when> <xsl:when test="@subject='Object programming'"> <xsl:attribute name="background-color">lightblue </xsl:attribute> </xsl:when> <xsl:otherwise> <xsl:attribute name="background-color">lightgreen </xsl:attribute> </xsl:otherwise></xsl:choose><td width="20%"><font size="1pt"><center><xsl:value-of select="hour"/></center></font></td><td width="35%"><font size="1pt"><xsl:value-of select="@subject"/></font></td><td width="15%" ><font size="1pt"><center><xsl:value-of select="@form"/></center></font></td><td width="15%"><font size="1pt"><center><xsl:value-of select="room"/></center></font></td></tr></xsl:for-each></table><br></br><br></br> And my DTD: <?xml version="1.0" encoding="ISO-8859-2"?><!ELEMENT semester (day_1,day_2)><!ELEMENT day_1 (hour, subject, form, room)><!ELEMENT day_2 (hour, subject, form, room)><!ATTLIST day_1 form (lecture | lab |classes) #REQUIRED subject (Gymnastics|Object_programming|Team_sports) #REQUIRED><!ELEMENT hour (#PCDATA)><!ELEMENT room (#PCDATA)> I'd appreciate if anybody could help.Thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.