如何在XSLT中使用动态表ID构建多个html表

时间:2013-10-18 15:14:37

标签: javascript html xslt recursion html-table

我正在尝试在样式表中动态构建html表。我目前正在使用递归来构建表,但我需要某种类型的JavaScript来控制这些表的存在。这就是表ID发挥作用的地方。

这是我的样式表。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
    <a href="#" onclick="showtable('test1'); return false;">Hide Table 1</a>
    <a href="#" onclick="showtable('test2'); return false;">Hide Table 2</a>
    <a href="#" onclick="showtable('test3'); return false;">Hide Table 3</a>
    <xsl:call-template name="Recursion">
      <xsl:with-param name="Person" select="0"/>
      <xsl:with-param name="tableID" select="0"/>
    </xsl:call-template>
  </xsl:template>
  <xsl:template name="Recursion">
    <xsl:param name="Person"/>
    <xsl:param name="tableID"/>
    <xsl:variable name="Count" select="count(//Table1)"/>
    <xsl:choose>
      <xsl:when test="$Person&lt;$Count">
        <xsl:call-template name="NewTable">
          <xsl:with-param name="Person" select="$Person"/>
          <xsl:with-param name="tableID" select="$tableID+1"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>

      </xsl:otherwise>
    </xsl:choose>
    <xsl:choose>
      <xsl:when test="$Person&lt;$Count">
        <xsl:call-template name="Recursion">
          <xsl:with-param name="Person" select="$Person+5"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>

      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="NewTable">
    <xsl:param name="Person"/>
    <xsl:param name="tableID"/>
    <html>
      <head>
        <script type="text/javascript">
          function showHide(id) {

          if (document.getElementById(id).style.display == "none") {
          document.getElementById(id).style.display = '';
          }

          else {
          document.getElementById(id).style.display = "none";
          }

          }
        </script>
      </head>
      <body>
        <table id="$tableID" border="5">
          <thead>
            <xsl:for-each select="/*/*[1]/*">
              <th>
                <xsl:value-of select="translate(local-name(), '_', ' ')"/>
              </th>
            </xsl:for-each>
          </thead>
          <tbody>
            <xsl:variable name="Person1" select="$Person+1"/>
            <xsl:variable name="Person2" select="$Person+2"/>
            <xsl:variable name="Person3" select="$Person+3"/>
            <xsl:variable name="Person4" select="$Person+4"/>
            <xsl:variable name="Person5" select="$Person+5"/>
            <tr>
              <xsl:for-each select="/*/*[$Person1]/*">
                <xsl:variable name="Attribute" select="position()"/>
                <td>
                  <xsl:value-of select="/*/*[$Person1]/*[$Attribute]"/>
                </td>
              </xsl:for-each>
            </tr>
            <tr>
              <xsl:for-each select="/*/*[$Person2]/*">
                <xsl:variable name="Attribute" select="position()"/>
                <td>
                  <xsl:value-of select="/*/*[$Person2]/*[$Attribute]"/>
                </td>
              </xsl:for-each>
            </tr>
            <tr>
              <xsl:for-each select="/*/*[$Person3]/*">
                <xsl:variable name="Attribute" select="position()"/>
                <td>
                  <xsl:value-of select="/*/*[$Person3]/*[$Attribute]"/>
                </td>
              </xsl:for-each>
            </tr>
            <tr>
              <xsl:for-each select="/*/*[$Person4]/*">
                <xsl:variable name="Attribute" select="position()"/>
                <td>
                  <xsl:value-of select="/*/*[$Person4]/*[$Attribute]"/>
                </td>
              </xsl:for-each>
            </tr>
            <tr>
              <xsl:for-each select="/*/*[$Person5]/*">
                <xsl:variable name="Attribute" select="position()"/>
                <td>
                  <xsl:value-of select="/*/*[$Person5]/*[$Attribute]"/>
                </td>
              </xsl:for-each>
            </tr>
          </tbody>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

当我应用样式表时,我得到了正在构建的表的所需结果以及样式表中最多为5的人数。唯一令我迷惑的是将表ID设置为唯一。当前样式表将表ID设置为$ tableID。

我的问题是,如何将表ID设置为等于变量,或者至少使每个表ID都是唯一的,并且能够跟踪它以便可以在表格上方的JavaScript方法中使用它。

这是输入XML文件。

<?xml version="1.0" encoding="utf-8" ?>
<NewDataSet>
  <Table1>
    <Run_Date>2013-12-21</Run_Date>
    <Run_Time>9:30</Run_Time>
    <Date_Ending>2013-12-7</Date_Ending>
    <Invoice_Number>00001</Invoice_Number>
    <Invoice_Date>2013-12-1</Invoice_Date>
    <Due_Date>2013-12-31</Due_Date>
  </Table1>
  <Table1>
    <Run_Date>2013-12-21</Run_Date>
    <Run_Time>9:30</Run_Time>
    <Date_Ending>2013-12-7</Date_Ending>
    <Invoice_Number>00001</Invoice_Number>
    <Invoice_Date>2013-12-1</Invoice_Date>
    <Due_Date>2013-12-31</Due_Date>
  </Table1>
</NewDataSet>

我得到了这个输出

<Table id="$tableID" border="5">

但我想要这个输出

<!-- Obviously this is a very dumbed down version of my expected output. I just need the table IDs 
increment by one.-->
<Table id="1" border="5">

<Table id="2" border="5">

非常感谢任何输入。如果你愿意,你可以给我一个关于如何动态地将ID添加到JavaScript标签的想法,但我应该能够计算出那部分。

提前致谢。

1 个答案:

答案 0 :(得分:0)

你需要

 <table id="{$tableID}" b

使用{},以便评估属性值中的Xpath表达式。

虽然编码效率相当低,因为您“手动”递归,然后重复从树根获取项目

/*/*[$Person3]/*">

而不是让<xsl:apply-templates/>自然地适用于每个表格。