XSL Stylesheet getting a table with donors and donations
I am struggling with this one. I have two XML sheets that I want to display information from both. One XML sheet has the donations, and the other has the Donors and their information. I would like to create a table that has their contact information and the ammount they donated. A table with the Donor's name and contact information with another cell next to that one tha开发者_运维问答t includes the amount that they donated.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="donation" match="donation" use="."/>
<xsl:variable name="people" select="document('people.xml')"/>
<xsl:variable name="money" select="document('money.xml')"/>
<xsl:key name="personid" use="@pid" match="person"/>
<xsl:template match="/">
<html>
<head>
<title>The Light House</title>
</head>
<body>
<h2>The Lighthouse Donation List</h2>
</body>
</html>
<table>
<tr>
<th colspan="5">Donor</th>
<th colspan="5">Donation</th>
</tr>
<tr>
<td colspan="5">
</td>
<td colspan="5">
</td>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>
Here is the money.xml
<?xml-stylesheet type="text/xsl" href="money.xsl" ?>
<donations>
<donation pin="p1" amount="10000" />
<donation pin="p2" amount="50" />
<donation pin="p3" amount="100" />
<donation pin="p4" amount="10000" />
</donations>
Here are the donors
<persons>
<person pid="p1">
<firstName>David</firstName>
<lastName>Olson</lastName>
<street>5133 Oak Street</street>
<city>Delphi</city>
<state>KY</state>
<zip>89011</zip>
<phone>(532) 555-8981</phone>
</person>
<person pid="p2">
<firstName>Cindy</firstName>
<lastName>Wu</lastName>
<street>31 Alice Avenue</street>
<city>Delphi</city>
<state>KY</state>
<zip>89011</zip>
<phone>(532) 555-7212</phone>
</person>
<person pid="p3">
<firstName>Lee</firstName>
<lastName>Thomas</lastName>
<street>451 Unwin Court</street>
<city>Jasper</city>
<state>KY</state>
<zip>89381</zip>
<phone>(534) 555-9082</phone>
</person>
<person pid="p4">
<firstName>Jane</firstName>
<lastName>Whitney</lastName>
<street>87 Hilltop Drive</street>
<city>Jasper</city>
<state>KY</state>
<zip>89381</zip>
<phone>(534) 555-7493</phone>
</person>
</person>
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="ppathPersons" select=
"'file:///c:/temp/delete/persons.xml'"/>
<xsl:param name="ppathDonations" select=
"'file:///c:/temp/delete/donations.xml'"/>
<xsl:variable name="vPersons"
select="document($ppathPersons)"/>
<xsl:variable name="vDonations"
select="document($ppathDonations)"/>
<xsl:template match="/">
<html>
<table>
<xsl:apply-templates select="$vPersons/*/person"/>
</table>
</html>
</xsl:template>
<xsl:template match="person">
<tr>
<td colspan="5">
<xsl:value-of select="concat(firstName, ' ', lastName)"/>
</td>
<td colspan="5">
<xsl:value-of select=
"$vDonations/*/*[@pin = current()/@pid]/@amount"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
when applied on any XML document (ignored/not-used), produces the wanted, correct result:
<html>
<table>
<tr>
<td colspan="5">David Olson</td>
<td colspan="5">10000</td>
</tr>
<tr>
<td colspan="5">Cindy Wu</td>
<td colspan="5">50</td>
</tr>
<tr>
<td colspan="5">Lee Thomas</td>
<td colspan="5">100</td>
</tr>
<tr>
<td colspan="5">Jane Whitney</td>
<td colspan="5">10000</td>
</tr>
</table>
</html>
精彩评论