xslt- comparing and selecting different elements

Here should go questions about transforming XML with XSLT and FOP.
lock
Posts: 6
Joined: Tue Aug 05, 2008 11:25 am

xslt- comparing and selecting different elements

Post by lock »

Hi guys. I am just asking for some help with my xslt.

I have an XML file which with a structure like below (havnt included the whole file)

Code: Select all

<units xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="studentschema.xsd">
<unit>
<unitCode>FIT2028</unitCode>
<unitLeader>Janet Fraser</unitLeader>
<class tutorialNo="t1">
<tutor>Janet Fraser</tutor>
<tutor>Christine Clemence</tutor>
<timetable>
<day>Wednesday</day>
<time>14:00:00</time>
<room>G3.18</room>
</timetable>
<studentMarks>
<student id="s12233441">

Now their are multiple unit elements and each has the same structure. Now i need to create a xslt that will list all student elements who are part of both the unit shown above (with unitCode FIT2028) and another unit (with a value of FIT3043).

Now I really dont know how to do this.
I know how to compare values but i dont know how to compare them in this context.

Can somebody please help me? Am desperate!

Many thanks in advance
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Re: xslt- comparing and selecting different elements

Post by sorin_ristache »

Hello,

If the unit elements have the same structure you find the students with the same id attribute that are common to units FIT2028 and FIT3043 with the following expression:

Code: Select all

/units/unit/class/studentMarks/student[ancestor::unit/unitCode = 'FIT2028'][@id= /units/unit/class/studentMarks/student[ancestor::unit/unitCode = 'FIT3043']/@id]
For example you process the set of all the common students with:

Code: Select all

<xsl:apply-templates select="/units/unit/class/studentMarks/student[ancestor::unit/unitCode = 'FIT2028'][@id= /units/unit/class/studentMarks/student[ancestor::unit/unitCode = 'FIT3043']/@id]"/>
Regards,
Sorin
lock
Posts: 6
Joined: Tue Aug 05, 2008 11:25 am

Re: xslt- comparing and selecting different elements

Post by lock »

Oh my god.. thank you so much for that. Worked an absolute treat! Unfortuantly, i dont fully understand the statement, but thanks so much for that.
Last edited by lock on Tue Aug 05, 2008 12:51 pm, edited 1 time in total.
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Re: xslt- comparing and selecting different elements

Post by sorin_ristache »

Do you mean formatting the expression that finds the student elements in the template where you will use it? You can use the expression in any template you want. The context where you use the expression does not matter because the expression uses absolute paths (starting with '/').


Regards,
Sorin
lock
Posts: 6
Joined: Tue Aug 05, 2008 11:25 am

Re: xslt- comparing and selecting different elements

Post by lock »

sorin wrote:Do you mean formatting the expression that finds the student elements in the template where you will use it? You can use the expression in any template you want. The context where you use the expression does not matter because the expression uses absolute paths (starting with '/').


Regards,
Sorin
Thanks for that mate.. worked it out. You were so helpful! Cheers
lock
Posts: 6
Joined: Tue Aug 05, 2008 11:25 am

Re: xslt- comparing and selecting different elements

Post by lock »

Hey mate. Sorry to be a pain but just need another quick bit of help. Im actually trying to learn this top off campus and the resources available to us are limited.

I need to format the data so that i have 3 columns, one with the student name (which i am fine at doing) and the other two colums have the average for each unit (eg. average for FIT2028 and FIT3043). Now I know how to calculate the average, as I do below (their might be a much simpler method of calculating average but i dont know it, i probably could use a lot less variables which i might fix up later :( ).... but i dont konw how to calculate it for each unit we are doing..

Code: Select all

<xsl:variable name="ass_sum" select="sum(assignments/assignment)"/>
<xsl:variable name="unit_sum" select="sum(assignments/unitTest)"/>
<xsl:variable name="ass_count" select="count(assignments/assignment)"/>
<xsl:variable name="unit_count" select="count(sassignments/unitTest)"/>
<xsl:variable name="tot_sum" select="$ass_sum + $unit_sum"/>
<xsl:variable name="count_sum" select="$ass_count + $unit_count"/>
<xsl:variable name="average" select="$tot_sum div $count_sum"/>
<xsl:value-of select="$average"/>
A nudge in the right direction would be awesome.

Thanks in advance
lock
Posts: 6
Joined: Tue Aug 05, 2008 11:25 am

Re: xslt- comparing and selecting different elements

Post by lock »

please can someone send me some help?? cheers!
Post Reply