我希望Donations按降序排列,当我尝试运行它时会出现此错误
Unexpected element
xsl:sort
代码
<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:\Users\Randy\Documents\xml\projects\persons.xml'"/>
<xsl:param name="ppathDonations" select="'file:///C:\Users\Randy\Documents\XML\projects\money.xml'"/>
<xsl:variable name="vPersons" select="document($ppathPersons)"/>
<xsl:variable name="vDonations" select="document($ppathDonations)"/>
<xsl:template match="/">
<html>
<head>
<link href="money.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h2>The Lighthouse Donation List</h2>
<p><img src="logo.jpg"/></p>
<table>
<tr>
<th>Donor</th>
<th>Donation</th>
</tr>
<xsl:apply-templates select="$vPersons/*/person"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="person">
<tr>
<td colspan="1">
<xsl:value-of select="concat(firstName, ' ', lastName)"/>
<xsl:value-of select="concat(' ',street,' ' , city,' ', state, ' ', zip, ' ', phone)"/>
</td>
<td colspan="1">
<xsl:value-of select="$vDonations/*/*[@pin = current()/@pid]/@amount"/>
<xsl:for-each select="$vDonations">
<xsl:sort select="$ppathDonations" data-type="number" order="descending" case- order="upper-first"/>
</xsl:for-each>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:1)
如果您希望同一个人的捐款按递减顺序显示在一个td
内,请使用:
<xsl:template match="person">
<tr>
<td colspan="1">
<xsl:value-of select="concat(firstName, ' ', lastName)"/>
<xsl:value-of select="concat(' ',street,' ' , city,' ', state, ' ', zip, ' ', phone)"/>
</td>
<td colspan="1">
<xsl:for-each select="$vDonations/*/*[@pin = current()/@pid]">
<xsl:sort select="@amount"
data-type="number" order="descending"
case-order="upper-first"/>
<xsl:if test="position() >1">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:value-of select="@amount"/>
</xsl:for-each>
</td>
</tr>
如果您想按递减顺序显示所有捐款,请将转换修改为此:
<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="ppathDonations" select=
"'file:///c:/temp/delete/donations.xml'"/>
<xsl:param name="ppathPersons" select=
"'file:///c:/temp/delete/persons.xml'"/>
<xsl:variable name="vPersons" select=
"document($ppathPersons)"/>
<xsl:variable name="vDonations" select=
"document($ppathDonations)"/>
<xsl:template match="/">
<html>
<head>
<link href="money.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h2>The Lighthouse Donation List</h2>
<p>
<img src="logo.jpg"/>
</p>
<table>
<tr>
<th>Donor</th>
<th>Donation</th>
</tr>
<xsl:apply-templates select=
"$vDonations/*/donation">
<xsl:sort select="@amount" data-type="number"
order="descending"/>
</xsl:apply-templates>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="donation">
<xsl:variable name="vPerson" select=
"$vPersons/*/person[@pid = current()/@pin]"/>
<tr>
<td colspan="1">
<xsl:value-of select=
"concat($vPerson/firstName, ' ', $vPerson/lastName)"/>
<xsl:value-of select=
"concat(' ', $vPerson/street,' ' ,
$vPerson/city,' ',
$vPerson/state, ' ',
$vPerson/zip, ' ',
$vPerson/phone)"/>
</td>
<td colspan="1">
<xsl:value-of select="@amount"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
使用您在第一个问题中提供的两个XML文件执行此转换时,会生成所需的正确结果:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="money.css" rel="stylesheet" type="text/css">
</head>
<body>
<h2>The Lighthouse Donation List</h2>
<p><img src="logo.jpg"></p>
<table>
<tr>
<th>Donor</th>
<th>Donation</th>
</tr>
<tr>
<td colspan="1">David Olson 5133 Oak Street Delphi KY 89011 (532) 555-8981</td>
<td colspan="1">10000</td>
</tr>
<tr>
<td colspan="1">Jane Whitney 87 Hilltop Drive Jasper KY 89381 (534) 555-7493</td>
<td colspan="1">10000</td>
</tr>
<tr>
<td colspan="1">Lee Thomas 451 Unwin Court Jasper KY 89381 (534) 555-9082</td>
<td colspan="1">100</td>
</tr>
<tr>
<td colspan="1">Cindy Wu 31 Alice Avenue Delphi KY 89011 (532) 555-7212</td>
<td colspan="1">50</td>
</tr>
</table>
</body>
</html>