XSLT 从 XML 文件创建 table。在 table 的单元格中放入多个值

XSLT to create a table from XML file. Put multiple values in table's cell

这是学校的一个项目,我大部分时间都在学校。我需要重新创建此 PDF 文档的左侧 table:http://www.canton.edu/courses/Course_Registration_Form.pdf

我遇到的问题是在一个单元格中放置多个替代项。它只从我的 XML 文件中获取第一个备用元素。这是我的代码:

xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" version="4.0" />
  <xsl:template match="/">
    <html>
      <head>
    <title>Course Registration Form</title>
      </head>
      <body>
    <table border="1">
      <tr>
        <th>CRN#</th>
        <th>Course Number (ENGS 100)</th>
            <th>Section (0W1)</th>
        <th>Title</th>
        <th>Credits</th>
            <th>Alternates</th>
      </tr>
      <xsl:for-each select="/schedule/class">
         <xsl:sort select="crn" />
           <tr> 
        <td><xsl:value-of select="crn" /></td>
        <td><xsl:value-of select="coursenumber" /></td>
        <td><xsl:value-of select="section" /></td>
        <td><xsl:value-of select="title" /></td>
        <td><xsl:value-of select="credits" /></td>
        <td><xsl:value-of select="alternates" /></td>
          </tr>
         </xsl:for-each>
        <tr>
              <td></td>
          <td></td>
          <td></td>
          <td><b>Total Credits</b></td>
          <td><xsl:value-of select="sum(/schedule/class/credits)"/></td>
          <td></td>
        </tr>
        </table>
       </body>
      </html>
      </xsl:template>
 </xsl:stylesheet>

xml:

<!DOCTYPE library SYSTEM "schedule.dtd">
<?xml-stylesheet type="text/xsl" href="schedule.xsl" ?>
<schedule>
    <class>
        <crn>10000</crn>
        <coursenumber>ENGS 100</coursenumber>
        <section>0W1</section>
        <title>Intro to English</title>
        <credits>3</credits>
    </class>
    <class>
        <crn>10001</crn>
        <coursenumber>HIST 100</coursenumber>
        <section>0W2</section>
        <title>Intro to History</title>
        <credits>3</credits>
        <alternates>10011</alternates>
    </class>
    <class>
        <crn>10002</crn>
        <coursenumber>MATH 100</coursenumber>
        <section>0W3</section>
        <title>Intro to Algebra</title>
        <credits>4</credits>
        <alternates>10012</alternates>
        <alternates>10022</alternates>
    </class>
    <class>
        <crn>10003</crn>
        <coursenumber>BSAD 100</coursenumber>
        <section>0W4</section>
        <title>Intro to Business</title>
        <credits>3</credits>
        <alternates>10013</alternates>
        <alternates>10023</alternates>
        <alternates>10033</alternates>
    </class>
</schedule>

DTD:

<!ELEMENT schedule (class+)>
<!ELEMENT class (crn, coursenumber, section, title, credits, alternates?)>
<!ELEMENT crn (#PCDATA)>
<!ELEMENT coursenumber (#PCDATA)>
<!ELEMENT section (#PCDATA)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT credits (#PCDATA)>
<!ELEMENT alternates (#PCDATA)>

我想要它做的是创建 table,如果 XML 文件中有多个备用元素,它将把它放在与其他备用元素相同的单元格中。任何建议或想法都会很棒。

那是因为您正在使用 xsl:value-of。在 XSLT 1.0 中,这只是 returns 第一次出现。

尝试使用 xsl:apply-templates 并为 alternates 添加模板。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" version="4.0" />
    <xsl:template match="/">
        <html>
            <head>
                <title>Course Registration Form</title>
            </head>
            <body>
                <table border="1">
                    <tr>
                        <th>CRN#</th>
                        <th>Course Number (ENGS 100)</th>
                        <th>Section (0W1)</th>
                        <th>Title</th>
                        <th>Credits</th>
                        <th>Alternates</th>
                    </tr>
                    <xsl:for-each select="/schedule/class">
                        <xsl:sort select="crn" />
                        <tr> 
                            <td><xsl:value-of select="crn" /></td>
                            <td><xsl:value-of select="coursenumber" /></td>
                            <td><xsl:value-of select="section" /></td>
                            <td><xsl:value-of select="title" /></td>
                            <td><xsl:value-of select="credits" /></td>
                            <td><xsl:apply-templates select="alternates" /></td>
                        </tr>
                    </xsl:for-each>
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><b>Total Credits</b></td>
                        <td><xsl:value-of select="sum(/schedule/class/credits)"/></td>
                        <td></td>
                    </tr>
                </table>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="alternates">
        <xsl:if test="not(position()=1)"><br/></xsl:if>
        <xsl:value-of select="."/>
    </xsl:template>
</xsl:stylesheet>

使用 xsl:apply-templates 并用其他模板覆盖默认模板(推送方法)将更易于管理和扩展。如果您的样式表要复杂得多,您可能需要考虑更新样式表的其余部分。