首页 > 解决方案 > 我按照说明操作,但我的 XML 和 XSL 文件无法正常工作

问题描述

1.在编辑器中转到 hbemployees.xsl 文件。在打开样式表元素之后,直接创建部门键,匹配员工元素并使用部门元素作为键索引。此键的目的是按部门对员工进行分组。

  1. 在 h1 Employee Report 标题之后直接插入一个 for-each 元素,该元素使用 Muenchian 分组通过使用位置路径选择每个唯一部门:“//employee[generate-id()=generate-id(key('departments' , department)[1])]”,然后按部门对结果进行排序。

  2. 每次通过 for-each 循环,将以下 HTML 代码写入结果文档:

    <table class="employeeList"> <caption>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>
    </tbody>
    </table>
    

其中部门是部门元素的值。

  1. 在您刚刚编写的 HTML 代码的 tbody 元素中,使用带有部门元素当前值的部门键应用模板,以便显示当前显示部门中每个员工的信息。按salary 元素的降序对应用的模板进行排序。

  2. 在根模板之后直接插入一个与员工元素匹配的新模板。此模板的目的是编写一个包含所选员工信息的表格行。让模板将以下 HTML 代码写入结果文档:

    <tr>
    <td>name</td> 
    <td>position</td> 
    <td>salary</td> 
    <td>phone</td>
    <td>gender</td>
    <td>marital status</td>
    <td>working status</td>
    </tr> 
    

    其中姓名、职位、工资、电话、性别、婚姻状况和工作状态是姓名、职位、工资、电话、性别、军事状态和工作状态元素的值。格式化薪水,使其显示为货币。

我不确定为什么我的文件不起作用。

<?xml version="1.0" encoding="UTF-8" ?>
<!--
   New Perspectives on XML, 3rd Edition
   Tutorial 7
   Case Problem 1

   Harris and Barnes Style Sheet

   Filename:         hbemployees.xsl
   Supporting Files: hbemployees.xml
-->


<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:key name="departments" match="employee" use="department" />

   <xsl:variable name="hbemployessDoc" select="document('hbemployess.xml')" />

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

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

               </xsl:for-each>

             </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="193">
      <name>Fleming, Angela</name>
      <position>Inventory Ck</position>
      <phone>x31751</phone>
      <email>afleming30@example.com/harrisbarnes</email>
      <department>Sales</department>
      <salary>99000</salary>
      <gender>female</gender>
      <maritalStatus>married</maritalStatus>
      <workStatus>Full Time</workStatus>
   </employee>
</employees>

标签: xmlvalidationxslt

解决方案


推荐阅读