在XSLT中对来自两个for-each调用的数据进行排序

时间:2018-07-15 17:51:11

标签: xslt-1.0

我找到了这个question,在答案中它建议使用联合,但我不知道如何做到这一点。

在我的情况下,我正在从两个外部XML文件读取数据。第一个包含发布者,第二个包含历史记录。现在,我正在按名称排序。脚本:

public WiP_QuestionBoard() {
        initComponents();

        //Makes the table header text centered.
        ((DefaultTableCellRenderer) tblQuestions.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(SwingConstants.CENTER);

        //This will center the text in table (header not included).
        DefaultTableCellRenderer centerAlign = new DefaultTableCellRenderer();

        //center columns.
        centerAlign.setHorizontalAlignment(SwingConstants.CENTER);
        for (int i = 0; i < tblQuestions.getColumnCount(); i++) {
            tblQuestions.getColumnModel().getColumn(i).setCellRenderer(centerAlign);
        }

        //makes the table span the panel it is in.
        tblQuestions.setPreferredSize(this.panelQuestions.getPreferredSize());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

您可以看到我的表有几列。可以在其他任何列上对这些数据进行排序吗?

我以为其他问题都有答案...。

我希望我有道理,感谢您的宝贵时间。

更新

我尝试添加一些JavaScript,以允许用户点击列标题:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msa="http://www.publictalksoftware.co.uk/msa">
  <xsl:output method="html" indent="yes" version="4.01"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
    doctype-public="//W3C//DTD XHTML 1.0 Transitional//EN"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>Publishers Report</title>
        <link rel="stylesheet" type="text/css" href="Custom Publisher Report.css"/>
      </head>
      <body>
        <xsl:variable name="PubDB" select="document('MSA_PublisherDatabase.XML')"/>
        <table>
          <thead>
            <th class="cellHeading">Student</th>
            <th class="cellHeading">Last As Student</th>
            <th class="cellHeading">Last Bible Reading</th>
            <th class="cellHeading">Last As Publisher</th>
            <th class="cellHeading">Last As Talk</th>
            <th class="cellHeading">Last As Assistant</th>
          </thead>
          <tbody>
            <xsl:apply-templates select="$PubDB/msa:PublisherDatabase/msa:Publishers/msa:Publisher">
              <xsl:sort select="msa:Name" data-type="text" order="ascending"/>
            </xsl:apply-templates>
          </tbody>
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="msa:Publisher">
    <xsl:variable name="HistoryDB" select="document('AssignHistory.XML')"/>
    <xsl:variable name ="SearchName" select="msa:Name"/>
    <tr>
      <!--The first cell is the name-->
      <td>
        <xsl:value-of select="msa:Name"/>
      </td>
      <!--The next cell is the most recent Student date-->
      <!--So we need to locate all "Bible Reading" items for the name in question-->
      <!--We sort the entries in date descending order so that the first entry is the most recent-->
      <td>
        <xsl:for-each select="$HistoryDB/AssignmentHistory/*/StudentItems/Item[Description != 'Assistant' and Name = $SearchName]">
          <xsl:sort select="$HistoryDB/AssignmentHistory/*" order="descending" data-type="text"/>
          <!--We only want the first entry-->
          <xsl:if test="position()=1">
            <xsl:call-template name="DisplayItemDate">
              <xsl:with-param name="ItemDate" select="name(../..)"/>
            </xsl:call-template>
          </xsl:if>
        </xsl:for-each>
        <!--If no entries were found then it need to write out "<xsl:text> </xsl:text>" How?-->
      </td>
      <!--The next cell is the most recent Bible Reading date-->
      <!--So we need to locate all "Bible Reading" items for the name in question-->
      <!--We sort the entries in date descending order so that the first entry is the most recent-->
      <td>
        <xsl:for-each select="$HistoryDB/AssignmentHistory/*/StudentItems/Item[Description = 'Bible Reading' and Name = $SearchName]">
          <xsl:sort select="$HistoryDB/AssignmentHistory/*" order="descending" data-type="text"/>
          <!--We only want the first entry-->
          <xsl:if test="position()=1">
            <xsl:call-template name="DisplayItemDate">
              <xsl:with-param name="ItemDate" select="name(../..)"/>
            </xsl:call-template>
          </xsl:if>
        </xsl:for-each>
        <!--If no entries were found then it need to write out "<xsl:text> </xsl:text>" How?-->
      </td>
      <!--The next cell is the most recent Publisher date-->
      <!--So we need to locate all "Bible Reading" items for the name in question-->
      <!--We sort the entries in date descending order so that the first entry is the most recent-->
      <td>
        <xsl:for-each select="$HistoryDB/AssignmentHistory/*/StudentItems/Item[Description != 'Talk' and Description != 'Assistant' and Name = $SearchName]">
          <xsl:sort select="$HistoryDB/AssignmentHistory/*" order="descending" data-type="text"/>
          <!--We only want the first entry-->
          <xsl:if test="position()=1">
            <xsl:call-template name="DisplayItemDate">
              <xsl:with-param name="ItemDate" select="name(../..)"/>
            </xsl:call-template>
          </xsl:if>
        </xsl:for-each>
        <!--If no entries were found then it need to write out "<xsl:text> </xsl:text>" How?-->
      </td>
      <!--The next cell is the most recent Talk date-->
      <!--So we need to locate all "Bible Reading" items for the name in question-->
      <!--We sort the entries in date descending order so that the first entry is the most recent-->
      <td>
        <xsl:for-each select="$HistoryDB/AssignmentHistory/*/StudentItems/Item[Description = 'Talk' and Name = $SearchName]">
          <xsl:sort select="$HistoryDB/AssignmentHistory/*" order="descending" data-type="text"/>
          <!--We only want the first entry-->
          <xsl:if test="position()=1">
            <xsl:call-template name="DisplayItemDate">
              <xsl:with-param name="ItemDate" select="name(../..)"/>
            </xsl:call-template>
          </xsl:if>
        </xsl:for-each>
        <!--If no entries were found then it need to write out "<xsl:text> </xsl:text>" How?-->
      </td>
      <!--The last cell is the most recent Assistant date-->
      <!--So we need to locate all "Bible Reading" items for the name in question-->
      <!--We sort the entries in date descending order so that the first entry is the most recent-->
      <td>
        <xsl:for-each select="$HistoryDB/AssignmentHistory/*/StudentItems/Item[Type = 'Assistant' and Name = $SearchName]">
          <xsl:sort select="$HistoryDB/AssignmentHistory/*" order="descending" data-type="text"/>
          <!--We only want the first entry-->
          <xsl:if test="position()=1">
            <xsl:call-template name="DisplayItemDate">
              <xsl:with-param name="ItemDate" select="name(../..)"/>
            </xsl:call-template>
          </xsl:if>
        </xsl:for-each>
        <!--If no entries were found then it need to write out "<xsl:text> </xsl:text>" How?-->
      </td>
    </tr>
  </xsl:template>

  <xsl:template name="DisplayItemDate">
    <xsl:param name="ItemDate"/>
    <xsl:value-of select="substring($ItemDate, 8, 2)"/>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="substring($ItemDate, 6, 2)"/>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="substring($ItemDate, 2, 4)"/>
  </xsl:template>

</xsl:stylesheet>

当我使用“本地Web服务器”查看它时,它起作用了,但是在CHtmlView中却不能。因此,目前,我认为我会回到尝试使XSL本身尽可能在任何列上进行排序的方式。

0 个答案:

没有答案