XSL样式表获得捐赠者和捐赠的表格

时间:2011-07-30 20:55:11

标签: xml xslt

我正在努力解决这个问题。我有两个XML表,我想显示两者的信息。一张XML表格有捐款,另一张有捐款人及其信息。我想创建一个表格,其中包含他们的联系信息以及他们捐赠的金额。 一张表格,其中包含捐赠者的姓名和与该公司旁边的另一个单元格的联系信息,其中包括他们捐赠的金额。

<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>

这是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>

以下是捐助者

<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>

1 个答案:

答案 0 :(得分:1)

此转化

<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>

应用于任何XML文档(忽略/未使用)时,会生成所需的正确结果

<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>