|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hey,
I am having some problems converting this xml to xsl. I want the xsl to work for any XML files that are instance of the studentlist.dtd >> for example different course name, assessment items etc.). Any help will be appreciated -------------------------------------------------------- DTD below: <!ATTLIST course code CDATA #REQUIRED> <!ATTLIST item name CDATA #REQUIRED> <!ELEMENT studentlist (course, student+)> <!ELEMENT course (#PCDATA)> <!ELEMENT student (snum, surname, firstname, results)> <!ELEMENT results (item, total)> <!ELEMENT snum (#PCDATA)> <!ELEMENT surname (#PCDATA)> <!ELEMENT firstname (#PCDATA)> <!ELEMENT item (#PCDATA)> <!ELEMENT total (#PCDATA)> -------------------------------------------------------- XML below <?xml version="1.0"?> <?DOCTYPE markentry SYSTEM "studentlist.dtd"?> <?xml-stylesheet type="text/xsl" href="results.xsl"?> <studentList> <course code="4545bus">Business Ethics <student> <snum>s11111</snum> <surname>shi</surname> <firstname>tan</firstname> <results> <item name="Assignment1">20</item> <item name="Assignment2">20</item> <item name="Exam">40</item> <total>80</total> </results> </student> <student> .... .... .... </student> </course> </studentList> ------------------------------------------------------- XSL below <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>4545bus Business Ethics</h2> <table border="1"> <tr bgcolor="#9acd32"> <th align="left">snum</th> <th align="left">Assignment1</th> <th align="left">Assignment2</th> <th align="left">Exam</th> <th align="left">total</th> </tr> <xsl:for-each select="studentlist/course/student"> <xsl:sort select="snum"/> <tr> <td><xsl:value-of select="snum"/></td> <td><xsl:value-of select="(results/item[@name='Assignment1'])" /></td> <td><xsl:value-of select="(results/item[@name='Assignment2'])" /></td> <td><xsl:value-of select="(results/item[@name='Exam'])" /></td> <td><xsl:value-of select="(results/total)" /></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> |
|
#2
|
|||
|
|||
|
Well, you seem to have some idea what you're doing, iterating through the different students, so I'm not sure what actual difficulty you're having... I some some possible catch-points though:
Task 0: in the code you posted here, the xml file has a root node of studentList, while the xsl refers to studentlist. Since it appears twice in the xml and only once in the xsl, I'm assuming the capital should be there. Task 1. Show the course code and course title based on data in the XML rather than explicit in the XSL. Your <course> tag contains a lot of stuff other than the course title, so you'll have to either make the title an attribute or a seperate child tag. for the sake of this example let's say you make it an attribute. So then the line in your XSL which outputs this info would be: Code:
<h2> <xsl:value-of select="/studentList/course/@code"/> <xsl:text> </xsl:text> <!-- because xsl doesn't recognize it's own whitespace --> <xsl:value-of select="/studentList/course/@title"/> </h2> Task 2: Iterate through results items names to get values for ths picking the first instance of student as representative. If it might not be, you'll have figure something else out. Code:
<tr bgcolor="#9acd32"> <th align="left">snum</th> <xsl:for-each select="/studentList/course/student[1]/results/item"> <th align="left"><xsl:value-of select="@name"/></th> </xsl:for-each> <th align="left">total</th> </tr> Task 3: Iterate through results items values to get results for tds Code:
<tr> <td><xsl:value-of select="snum"/></td> <xsl:for-each select="results/item"> <td><xsl:value-of select="current()" /></td> </xsl:for-each> <td><xsl:value-of select="results/total"/></td> </tr> Did I miss the point of your question? not address anything? Full, modified xsl: Code:
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2><xsl:value-of select="/studentList/course/@code"/> <xsl:text> </xsl:text> <xsl:value-of select="/studentList/course/@title"/></h2> <table border="1"> <tr bgcolor="#9acd32"> <th align="left">snum</th> <xsl:for-each select="/studentList/course/student[1]/results/item"> <th align="left"><xsl:value-of select="@name"/></th> </xsl:for-each> <th align="left">total</th> </tr> <xsl:for-each select="studentList/course/student"> <xsl:sort select="snum"/> <tr> <td><xsl:value-of select="snum"/></td> <xsl:for-each select="results/item"> <td><xsl:value-of select="current()" /></td> </xsl:for-each> <td><xsl:value-of select="results/total"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> Chris crcdesign.net |
|
#3
|
|||
|
|||
|
Now I understand
Thanks alot for the help Chris, that was very helpful
Sam |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > XML to XSL problems!! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|