someone11 Posted September 19, 2008 Share Posted September 19, 2008 After the generous help of aalbetski via muenchian grouping I now have a new challenge. From my xml file I would like to create a two column html layout, a left navigation with named anchor links and a formatted output in the right column. After reading the templates chapter in Jeni Tennison'a Beginning XSLT 2.0 book I found that I can process the same node in different ways using the mode attribute inside the <xsl:template> and <xsl:apply-templates> tags, however I'm not getting the desired output which I want, and I'm not certain how to apply the mode attribute properly.I've attached four files to this thread.[1] the xml file[2] the left column navigation xsl file[3] the right column formatting xsl file[4] my sorry attempt at combining [2] and [3]As with my previous threads, my restriction is that I have to use client-side transformations.Thank you for your help!sping.xml: <?xml version="1.0" encoding="UTF-8"?><spring> <course category="Business & Management" id="BUSS470"> <link>../html/courses/business.html</link> <title>Business Negotiations</title> <desc>N/A</desc> <textbook>N/A</textbook> <inst>N/A</inst> <bio>N/A</bio> <number>BUS S470</number> <start_date> <month>06</month>/<day>23</day>/<year>08</year> </start_date> <end_date> <month>08</month>/<day>18</day>/<year>08</year> </end_date> <reglink>N/A</reglink> <time>N/A</time> <location>N/A</location> <meetings>N/A</meetings> <fee>N/A</fee> <units>N/A</units> <note>N/A</note> </course> <course category="Languages" id="DEV1E867"> <link>../html/courses/languages.html</link> <title>Conversational Spanish I</title> <desc>N/A</desc> <textbook>N/A</textbook> <inst>N/A</inst> <bio>N/A</bio> <number>DEV1 E867</number> <start_date> <month>06</month>/<day>24</day>/<year>08</year> </start_date> <end_date> <month>08</month>/<day>26</day>/<year>08</year> </end_date> <reglink>N/A</reglink> <time>N/A</time> <location>TBA</location> <meetings>N/A</meetings> <fee>N/A</fee> <units>N/A</units> <note>N/A</note> </course> <course category="Languages" id="DEV1E880"> <link>../html/courses/languages.html</link> <title>Conversational Spanish II</title> <desc>N/A</desc> <textbook>N/A</textbook> <inst>N/A</inst> <bio>N/A</bio> <number>DEV1 E880</number> <start_date> <month>10</month>/<day>02</day>/<year>08</year> </start_date> <end_date> <month>12</month>/<day>11</day>/<year>08</year> </end_date> <reglink>N/A</reglink> <time>N/A</time> <location>N/A</location> <meetings>N/A</meetings> <fee>N/A</fee> <units>N/A</units> <note></note> </course></spring> nav.xsl <?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" encoding="UTF-8" indent="yes" /> <xsl:key name="courses" match="course" use="@category" /> <xsl:template match="/"> <ul> <xsl:for-each select="//course[count(. | key('courses', @category)[1]) = 1]"> <xsl:variable name="cat" select="@category" /> <li>Category: <xsl:value-of select="$cat"/></li> <xsl:apply-templates select="//course[@category = $cat]" /> </xsl:for-each> </ul> </xsl:template> <xsl:template match="course"> <ul> <li><xsl:value-of select="title"/></li> </ul> </xsl:template></xsl:stylesheet> courses.xsl <?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> <!ENTITY mdash "—"> <!ENTITY ndash "–">]><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:output method="html" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <xsl:apply-templates select="spring/course"/> </xsl:template> <xsl:template match="course"> <h2><xsl:value-of select="title"/></h2> <p><xsl:value-of select="desc"/></p> <p><xsl:text>Textbook: </xsl:text><xsl:value-of select="textbook"/></p> <p><strong><em><xsl:value-of select="inst"/></em></strong><em> <xsl:value-of select="bio"/></em></p> <p><strong><xsl:value-of select="number"/></strong><br/> <xsl:value-of select="start_date"/><xsl:text>–</xsl:text><xsl:value-of select="end_date"/><br/> <a href="{reglink}">Register online for <em><xsl:value-of select="title"/></em></a><br/> <xsl:value-of select="time"/><br/> <xsl:text>Location: </xsl:text><xsl:value-of select="location"/><br/> <xsl:value-of select="meetings"/><xsl:text> meeting(s)</xsl:text><br/> <xsl:text>Fee: </xsl:text><xsl:value-of select="fee"/><br/> <xsl:value-of select="units"/><br/> <xsl:value-of select="note"/> </p> <p align="right"><a href="#wrapper">Return to Top</a></p> <hr size="1"/> </xsl:template> </xsl:stylesheet> combined.xsl ***DOESN'T WORK <?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> <!ENTITY mdash "—"> <!ENTITY ndash "–">]><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:output method="html" encoding="UTF-8" indent="yes"/> <xsl:template match="/" mode="nav"> <ul> <xsl:for-each select="//course[count(. | key('courses', @category)[1]) = 1]"> <xsl:variable name="cat" select="@category" /> <li>Category: <xsl:value-of select="$cat"/></li> <xsl:apply-templates select="//course[@category = $cat]" mode="nav"/> </xsl:for-each> </ul> </xsl:template> <xsl:template match="course" mode="nav"> <ul> <li><xsl:value-of select="title"/></li> </ul> </xsl:template> <xsl:template match="/" mode="formattting"> <xsl:apply-templates select="spring/course" mode="formatting"/> </xsl:template> <xsl:template match="course" mode="formatting"> <h2><xsl:value-of select="title"/></h2> <p><xsl:value-of select="desc"/></p> <p><xsl:text>Textbook: </xsl:text><xsl:value-of select="textbook"/></p> <p><strong><em><xsl:value-of select="inst"/></em></strong><em> <xsl:value-of select="bio"/></em></p> <p><strong><xsl:value-of select="number"/></strong><br/> <xsl:value-of select="start_date"/><xsl:text>–</xsl:text><xsl:value-of select="end_date"/><br/> <a href="{reglink}">Register online for <em><xsl:value-of select="title"/></em></a><br/> <xsl:value-of select="time"/><br/> <xsl:text>Location: </xsl:text><xsl:value-of select="location"/><br/> <xsl:value-of select="meetings"/><xsl:text> meeting(s)</xsl:text><br/> <xsl:text>Fee: </xsl:text><xsl:value-of select="fee"/><br/> <xsl:value-of select="units"/><br/> <xsl:value-of select="note"/> </p> <p align="right"><a href="#wrapper">Return to Top</a></p> <hr size="1"/> </xsl:template> </xsl:stylesheet> Link to comment Share on other sites More sharing options...
boen_robot Posted September 20, 2008 Share Posted September 20, 2008 In SAXON (an XSLT 2.0 processor), you can choose an initial mode with which to run the stylesheet. XSLT 1.0 APIs in browsers don't offer this AFAIK. You must create a starting template, and choose to move on in a different mode based on something (say, the value of a node in the XML document, or an XSLT parameter).Once a node is matched, only one of its modes is executed. In particular, only the template that has the current mode is executed. I'm not sure if templates with no mode are taken if there's no template in the current mode, but the point is that when you have: <xsl:template match="/" mode="nav"> <ul> <xsl:for-each select="//course[count(. | key('courses', @category)[1]) = 1]"> <xsl:variable name="cat" select="@category" /> <li>Category: <xsl:value-of select="$cat"/></li> <xsl:apply-templates select="//course[@category = $cat]" mode="nav"/> </xsl:for-each> </ul> </xsl:template>... <xsl:template match="/" mode="formattting"> <xsl:apply-templates select="spring/course" mode="formatting"/> </xsl:template> Only one of those templates gets executed and thus you get just one of the results you previously had.Basically, what this means is that you should have just one "/" template with no mode, and arrange the rest of the calls in it. Like for example: <xsl:template match="/"> <ul> <xsl:for-each select="//course[count(. | key('courses', @category)[1]) = 1]"> <xsl:variable name="cat" select="@category" /> <li>Category: <xsl:value-of select="$cat"/></li> <xsl:apply-templates select="//course[@category = $cat]" mode="nav"/> </xsl:for-each> </ul> <xsl:apply-templates select="spring/course" mode="formatting"/> </xsl:template> Link to comment Share on other sites More sharing options...
someone11 Posted September 20, 2008 Author Share Posted September 20, 2008 I'm so excited that it's working!!! Thank you so much for your help! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.