This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Somewhat Comlex XSL matching context node to an ancestor node
- From: Derek Doerr <ddoerr at yahoo dot com>
- To: XSL-List at lists dot mulberrytech dot com
- Date: Sun, 28 Apr 2002 15:13:27 -0700 (PDT)
- Subject: [xsl] Somewhat Comlex XSL matching context node to an ancestor node
- Reply-to: xsl-list at lists dot mulberrytech dot com
I'm working with an XML file that is structured as
follows:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl"
href="class_teams1.xsl"?>
<root
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="class.xsd">
<teams>
<team id="1">
<student_id>1</student_id>
<student_id>3</student_id>
</team>
<team id="2">
<student_id>2</student_id>
</team>
</teams>
<students>
<student id="1">
<name>
<first_name>john</first_name>
<surname>doe</surname>
</name>
<email>jdoe@yahoo.com</email>
</student>
<student id="2">
<name>
<first_name>john</first_name>
<surname>public</surname>
</name>
<email>jpublic@yahoo.com</email>
</student>
<student id="3">
<name>
<first_name>jane</first_name>
<surname>austin</surname>
</name>
<email>jaustin@yahoo.com</email>
</student>
</students>
</root>
What I want to do is to use XSL to create a table that
has one row per class "team". In the first cell, show
the team number (the team 'id'). In the second cell,
show the names of the students in the team (e.g. "john
doe").
Here is the XSL that I have so far:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<xsl:for-each select="root">
<table border="1" cellpadding="1" cellspacing="1"
width="100%">
<tr>
<td>Team #</td>
<td>Students</td>
</tr>
<xsl:for-each select="teams//team">
<tr>
<td>
<xsl:value-of select="@id"/>
</td>
<td>
<!-- this node in the processing is where I
start running into problems.... From here I need to
match the student_id value to the id attribute
of a student element -->
<xsl:for-each
select="/descendant::student[@id=self::student_id']/child::name">
<xsl:apply-templates>
<xsl:value-of select="surname"/>
</xsl:apply-templates>
<br/>
</xsl:for-each> <!-- student_id -->
</td>
</tr>
</xsl:for-each> <!-- each team within teams-->
</table>
</xsl:for-each> <!-- root -->
</xsl:template>
</xsl:stylesheet>
The structure of the XML doc basically lets me avoid
having to "hard code" <student> elements into <team>
elements, making maintenance of the XML easier.
Problem is that the XSL is much more difficult!
Thanks!
- Derek
__________________________________________________
Do You Yahoo!?
Yahoo! Health - your guide to health and wellness
http://health.yahoo.com
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list