スタイル シートで動的に 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<$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<$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 タグに動的に追加する方法についても教えていただけますが、その部分は把握できるはずです。
前もって感謝します。