Hi all,
I'm stumped. I've read through tutorials, examples, forums, and I can't find an answer to my problem. I'm trying to figure out how select a subset of nodes to transform by limiting the selection by attribute value, where the attribute value is not hard-coded -- but is actually determined by another element's attribute value in the same file. (Think foreign keys in relational databases...)
For example, think of a school containing a roster of students, and identifying which students belong in which classrooms. Some students may belong to multiple classrooms.
Code:
<school>
<classrooms>
<classroom id="1"><student id="1"><student id="42"> ... </classroom>
<classroom id="2"><student id="4"><student id="42"> ... </classroom>
</classrooms>
<students>
<student id="1"><name>John Doe</name></student>
<student id="2"><name>Jane Smith</name></student>
...
<student id="42"><name>Joe Somebody</name></student>
</students>
</school>
Now, I want to print out all the students, but organized by classroom. I was thinking I'd need to iterate over each classroom, and then print out each student who's ID matched the student listed as being in that room:
Code:
<xsl:template match="/">
<xsl:for-each select="classrooms/classroom">
Classroom <xsl:value-of select="@id" />
<xsl:for-each select="student"><!-- classroom/student, that is -->
<xsl:apply-templates match="/school/students/student[@id=@id]" /><!-- student info from the roster -->
</xsl:for-each><!-- student in the classroom -->
</xsl:for-each><!-- classroom in the school -->
<xsl:template>
In the
bold apply-templates statement, I'm trying to say "For each student in the classroom, print the information from the roster for that student." In other words, the first
@id is "the ID of the student in the classroom" and the second
@id is "the ID of the student in the school roster".
Does this make sense? Is this even possible?
Thanks so much in advance for all your help.
Nik