我一直致力于此分配XSL样式表,该样式表显示员工信息并按部门对其进行分组并按降序排序。我正在使用for-each元素,该元素使用Muenchian分组通过使用此位置路径对每个唯一部门进行排序。赋值告诉我,每次通过for-each循环,它都会写出“employeeList”表的代码。 “部门”是部门元素的价值。
它还告诉我插入一个“匹配”employee元素的新模板。该模板应该写一个包含所选员工信息的表行。这是代码:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="departments" match="employee" use="department" />
<xsl:output method="html"
doctype-system="about:legacy-compat"
encoding="UTF-8"
indent="yes" />
<xsl:template match="/">
<html>
<head>
<title>Employee Report</title>
<link href="hbstyles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrap">
<header>
<img src="hblogo.png" alt="Harris and Barnes" />
</header>
<h1>Employee Report</h1>
<xsl:for-each select="//employee[generate-id()=generate-id(key('departments', department)[1])]">
<xsl:sort select="department" />
</xsl:for-each>
<table class="employeeList">
<caption><xsl:value-of select="department" /></caption>
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Salary</th>
<th>Phone</th>
<th>Gender</th>
<th>Marital Status</th>
<th>Work Status</th>
</tr>
</thead>
<tbody>
<xsl:apply-templates select="key('departments', department)">
<xsl:sort select="salary" order="descending" />
</xsl:apply-templates>
</tbody>
</table>
</div>
</body>
</html>
</xsl:template>
<xsl:template match="employee">
<tr>
<td><xsl:value-of select="name" /></td>
<td><xsl:value-of select="position" /></td>
<td><xsl:value-of select="format-number(salary, '$#,##0')" /></td>
<td><xsl:value-of select="phone" /></td>
<td><xsl:value-of select="gender" /></td>
<td><xsl:value-of select="maritalStatus" /></td>
<td>><xsl:value-of select="workingStatus" /></td>
</tr>
</xsl:template>
</xsl:stylesheet>
XML:
<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="hbemployees.xsl" ?>
<employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<employee empID="4">
<name>Heffner, Marian</name>
<position>Chief Operating Officer</position>
<phone>x10962</phone>
<email>mheffner50@example.com/harrisbarnes</email>
<department>Management</department>
<salary>262000</salary>
<gender>female</gender>
<maritalStatus>married</maritalStatus>
<workStatus>Full Time</workStatus>
</employee>
<employee empID="192">
<name>Murff, Nicolle</name>
<position>Mgr Software Client Supp</position>
<phone>x32524</phone>
<email>nmurff63@example.com/harrisbarnes</email>
<department>Sales</department>
<salary>137000</salary>
<gender>female</gender>
<maritalStatus>married</maritalStatus>
<workStatus>Full Time</workStatus>
</employee>
<employee empID="295">
<name>Vecchio, Art</name>
<position>Line Worker</position>
<phone>x12125</phone>
<email>avecchio55@example.com/harrisbarnes</email>
<department>Management</department>
<salary>83000</salary>
<gender>male</gender>
<maritalStatus>married</maritalStatus>
<workStatus>Part Time</workStatus>
</employee>
<employee empID="294">
<name>Lewis, Richard</name>
<position>Met Read/Coll</position>
<phone>x22131</phone>
<email>rlewis19@example.com/harrisbarnes</email>
<department>Production</department>
<salary>74500</salary>
<gender>male</gender>
<maritalStatus>married</maritalStatus>
<workStatus>Full Time</workStatus>
</employee>
<employee empID="293">
<name>White, William</name>
<position>Env Asst</position>
<phone>x03194</phone>
<email>wwhite61@example.com/harrisbarnes</email>
<department>Marketing</department>
<salary>53500</salary>
<gender>male</gender>
<maritalStatus>single</maritalStatus>
<workStatus>Contract</workStatus>
</employee>
<employee empID="292">
<name>Williams, John</name>
<position>Line Wrker A</position>
<phone>x06056</phone>
<email>jwilliams31@example.com/harrisbarnes</email>
<department>Research</department>
<salary>70500</salary>
<gender>male</gender>
<maritalStatus>single</maritalStatus>
<workStatus>Full Time</workStatus>
</employee>
<employee empID="291">
<name>Clark, David</name>
<position>Sr Engineer</position>
<phone>x03551</phone>
<email>dclark16@example.com/harrisbarnes</email>
<department>Production</department>
<salary>81000</salary>
<gender>male</gender>
<maritalStatus>married</maritalStatus>
<workStatus>Contract</workStatus>
</employee>
<employee empID="290">
<name>Sand, Robyn</name>
<position>Dsl Sys Rep</position>
<phone>x12823</phone>
<email>rsand25@example.com/harrisbarnes</email>
<department>Research</department>
<salary>89000</salary>
<gender>female</gender>
<maritalStatus>married</maritalStatus>
<workStatus>Contract</workStatus>
</employee>
<employee empID="191">
<name>Michell, Eloise</name>
<position>Mgr Cap Rptg Dist</position>
<phone>x19826</phone>
<email>emichell34@example.com/harrisbarnes</email>
<department>Sales</department>
<salary>188000</salary>
<gender>female</gender>
<maritalStatus>single</maritalStatus>
<workStatus>Full Time</workStatus>
</employee>
<employee empID="289">
<name>Blackshear, Homer</name>
<position>Line Wrker A</position>
<phone>x14845</phone>
<email>hblackshear74@example.com/harrisbarnes</email>
<department>Sales</department>
<salary>83000</salary>
<gender>male</gender>
<maritalStatus>married</maritalStatus>
<workStatus>Full Time</workStatus>
</employee>
<employee empID="288">
<name>Trumbull, Monroe</name>
<position>Prog/Analyst</position>
<phone>x27132</phone>
<email>mtrumbull49@example.com/harrisbarnes</email>
<department>Sales</department>
<salary>51500</salary>
<gender>male</gender>
<maritalStatus>single</maritalStatus>
<workStatus>Full Time</workStatus>
</employee>
BTW它验证但页面呈现为文本和数字。谢谢。
答案 0 :(得分:1)
如果 - 看起来 - 你想要一个每个部门的表,你必须将表放在 xsl:for-each
指令中。
IOW,移动结束标记</xsl:for-each>
,使其在</table>
之后立即显示。
另请注意:
<td>><xsl:value-of select="workingStatus" /></td>
返回一个只包含>
个字符的单元格,因为(1)>
之后你有一个额外的<td>
和(2)中没有workingStatus
元素输入。
另外,请确保按薪水排序时,xsl:sort
元素具有data-type="number"
属性。否则,您将在$ 262,000之前看到$ 83,000(按字母顺序排列)。