Matching values of nodes to apply templates

D

david hepworth

Hi All
I will try to explain my problem.

I am wishing to apply a template to the following XML. There are
multiple event nodes within the register root node. For each of the
event nodes there are a list of students taking the Event. I wish to
apply a template (and thus get the data) for the same student across
the events. I can apply the template to student 18 for event 1, how
do i then get the data for student 18 from event 2 and then for event
3 and so on?

Many thanks in advance.

David


<register>
<event>
<code>1</code>
<date>...</date>
 
D

David Carlisle

I can apply the template to student 18 for event 1, how
do i then get the data for student 18 from event 2 and then for event
3 and so on?

<xsl:apply-templates select="event/student
Code:
"/>

will apply templates to student elements with code=18 whatever event
they are in.

David
 
D

david hepworth

David Carlisle said:
I can apply the template to student 18 for event 1, how
do i then get the data for student 18 from event 2 and then for event
3 and so on?

<xsl:apply-templates select="event/student
Code:
"/>

will apply templates to student elements with code=18 whatever event
they are in.

David[/QUOTE]

Thanks for this.

The problem that I can forsee with this is that i dont actually know
the students code.
I need to show a line of details for each student.  There will be
different numbers of students with different codes per print out.

Thanks

David
 
D

David Carlisle

David Carlisle said:
I can apply the template to student 18 for event 1, how
do i then get the data for student 18 from event 2 and then for event
3 and so on?

<xsl:apply-templates select="event/student
Code:
"/>

will apply templates to student elements with code=18 whatever event
they are in.

David[/QUOTE]

Thanks for this.

The problem that I can forsee with this is that i dont actually know
the students code.
I need to show a line of details for each student.  There will be
different numbers of students with different codes per print out.

Thanks

David[/QUOTE]


just replace the literal 18 in the code I posted with whatever code you
need to calculate the value. (Or perhaps simpler calcualte teh value
first in a variable, and use a variable reference instead of 18)
 
J

Joris Gillis

I am wishing to apply a template to the following XML. There are
multiple event nodes within the register root node. For each of the
event nodes there are a list of students taking the Event. I wish to
apply a template (and thus get the data) for the same student across
the events. I can apply the template to student 18 for event 1, how
do i then get the data for student 18 from event 2 and then for event
3 and so on?

Hi,

I think this might be a way to the solution of your problem.

<xsl:key name="students" match="student/code" use="."/>

<xsl:template match="/">
<xsl:apply-templates select="//student[generate-id(code) =
generate-id(key('students',code))]">
<xsl:sort select="name"/>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="student">
student name: <xsl:value-of select="name"/>
<xsl:apply-templates select="//event[student/code=current()/code]"/>
</xsl:template>

<xsl:template match="event">
event: <xsl:value-of select="code"/>
</xsl:template>

this code produces:

student name: Janet Bloggs
event: 1
event: 2
student name: Joe Bloggs
event: 1
event: 2

Is this useful?


Joris Gillis
 
D

david hepworth

I am wishing to apply a template to the following XML. There are
multiple event nodes within the register root node. For each of the
event nodes there are a list of students taking the Event. I wish to
apply a template (and thus get the data) for the same student across
the events. I can apply the template to student 18 for event 1, how
do i then get the data for student 18 from event 2 and then for event
3 and so on?

Hi,

I think this might be a way to the solution of your problem.

<xsl:key name="students" match="student/code" use="."/>

<xsl:template match="/">
<xsl:apply-templates select="//student[generate-id(code) =
generate-id(key('students',code))]">
<xsl:sort select="name"/>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="student">
student name: <xsl:value-of select="name"/>
<xsl:apply-templates select="//event[student/code=current()/code]"/>
</xsl:template>

<xsl:template match="event">
event: <xsl:value-of select="code"/>
</xsl:template>

this code produces:

student name: Janet Bloggs
event: 1
event: 2
student name: Joe Bloggs
event: 1
event: 2

Is this useful?


Joris Gillis

Many thanks to both of you for your help - I believe that this will work.
David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top