使用XSLT生成具有jQuery treetable属性的表

时间:2017-04-08 20:29:44

标签: jquery xslt treetable

使用Saxon XSLT 2.0,如果绝对必要,可以使用XSLT 3.0。

主要问题是生成<tr>属性id和父ID(data-tt-iddata-tt-parent-id),它们是由用于显示的破折号分隔的数字序列(它们必须按顺序)。有效的建议有什么建议吗?我正在考虑不断更新字符串变量,即XSLT以正确的顺序遍历(深度优先,按顺序,不受任何多线程影响)。否则,我可能必须聪明地计算每个值的祖先等,这似乎非常低效。我对XSLT有些新意;)

<table id="example-basic">
  <caption>Basic jQuery treetable Example</caption>
  <thead>
    <tr>
      <th>Tree column</th>
      <th>Additional data</th>
    </tr>
  </thead>
  <tbody>
    <tr data-tt-id="1">
      <td>Node 1: Click on the icon in front of me to expand this branch.</td>
      <td>I live in the second column.</td>
    </tr>
    <tr data-tt-id="1.1" data-tt-parent-id="1">
      <td>Node 1.1: Look, I am a table row <em>and</em> I am part of a tree!</td>
      <td>Interesting.</td>
    </tr>
    <tr data-tt-id="1.1.1" data-tt-parent-id="1.1">
      <td>Node 1.1.1: I am part of the tree too!</td>
      <td>That's it!</td>
    </tr>
    <tr data-tt-id="2">
      <td>Node 2: I am another root node, but without children</td>
      <td>Hurray!</td>
    </tr>
  </tbody>
</table>

这些示例和jQuery treetable在http://ludo.cubicphuse.nl/jquery-treetable/#usage

中有更详细的描述

我正在生成类似于他们的复杂树拖放的jQuery treetable:

<table id="example-advanced">
  <caption>
    <a href="#" onclick="jQuery('#example-advanced').treetable('expandAll'); return false;">Expand all</a>
    <a href="#" onclick="jQuery('#example-advanced').treetable('collapseAll'); return false;">Collapse all</a>
  </caption>
  <thead>
    <tr>
      <th>Name</th>
      <th>Kind</th>
      <th>Size</th>
    </tr>
  </thead>
<tbody>
<tr data-tt-id='1'><td><span class='file'>Acknowledgements.rtf</span></td><td>File</td><td>480.95 KB</td></tr>
<tr data-tt-id='2'><td><span class='folder'>CHUD</span></td><td>Folder</td><td>--</td></tr>
<tr data-tt-id='2-1' data-tt-parent-id='2'><td><span class='folder'>amber</span></td><td>Folder</td><td>--</td></tr>
<tr data-tt-id='2-1-1' data-tt-parent-id='2-1'><td><span class='file'>AmberTraceFormats.pdf</span></td><td>File</td><td>124.46 KB</td></tr>
<tr data-tt-id='2-2' data-tt-parent-id='2'><td><span class='folder'>BigTop</span></td><td>Folder</td><td>--</td></tr>
<tr data-tt-id='2-2-1' data-tt-parent-id='2-2'><td><span class='file'>BigTopUserGuide.pdf</span></td><td>File</td><td>1314.71 KB</td></tr>
<tr data-tt-id='2-3' data-tt-parent-id='2'><td><span class='folder'>Saturn</span></td><td>Folder</td><td>--</td></tr>
<tr data-tt-id='2-3-1' data-tt-parent-id='2-3'><td><span class='file'>SaturnUserGuide.pdf</span></td><td>File</td><td>694.29 KB</td></tr>
<tr data-tt-id='2-4' data-tt-parent-id='2'><td><span class='folder'>Shark</span></td><td>Folder</td><td>--</td></tr>
<tr data-tt-id='2-4-1' data-tt-parent-id='2-4'><td><span class='file'>SharkUserGuide.pdf</span></td><td>File</td><td>12902.51 KB</td></tr>
<tr data-tt-id='2-5' data-tt-parent-id='2'><td><span class='folder'>simg4</span></td><td>Folder</td><td>--</td></tr>
<tr data-tt-id='2-6' data-tt-parent-id='2'><td><span class='folder'>simg4_plus</span></td><td>Folder</td><td>--</td></tr>
<tr data-tt-id='2-7' data-tt-parent-id='2'><td><span class='folder'>simg5</span></td><td>Folder</td><td>--</td></tr>
<tr data-tt-id='3'><td><span class='folder'>DocSets</span></td><td>Folder</td><td>--</td></tr>
<tr data-tt-id='3-1' data-tt-parent-id='3'><td><span class='folder'>com.apple.ADC_Reference_Library.CoreReference.docset</span></td><td>Folder</td><td>--</td></tr>
<tr data-tt-id='3-1-1' data-tt-parent-id='3-1'><td><span class='folder'>Contents</span></td><td>Folder</td><td>--</td></tr>
<tr data-tt-id='3-1-1-1' data-tt-parent-id='3-1-1'><td><span class='file'>Info.plist</span></td><td>File</td><td>1.23 KB</td></tr>
<tr data-tt-id='3-1-1-2' data-tt-parent-id='3-1-1'><td><span class='folder'>Resources</span></td><td>Folder</td><td>--</td></tr>
<tr data-tt-id='3-1-1-2-1' data-tt-parent-id='3-1-1-2'><td><span class='file'>docSet.dsidx</span></td><td>File</td><td>41504 KB</td></tr>
<tr data-tt-id='3-1-1-2-2' data-tt-parent-id='3-1-1-2'><td><span class='file'>docSet.skidx</span></td><td>File</td><td>43072 KB</td></tr>
<tr data-tt-id='3-1-1-2-3' data-tt-parent-id='3-1-1-2'><td><span class='folder'>Documents</span></td><td>Folder</td><td>--</td></tr>
<tr data-tt-id='3-1-1-2-3-1' data-tt-parent-id='3-1-1-2-3'><td><span class='folder'>documentation</span></td><td>Folder</td><td>--</td></tr>
<tr data-tt-id='3-1-1-2-3-1-1' data-tt-parent-id='3-1-1-2-3-1'><td><span class='folder'>Accessibility</span></td><td>Folder</td><td>--</td></tr>
<tr data-tt-id='3-1-1-2-3-1-1-1' data-tt-parent-id='3-1-1-2-3-1-1'><td><span class='folder'>Reference</span></td><td>Folder</td><td>--</td></tr>
<tr data-tt-id='3-1-1-2-3-1-1-1-1' data-tt-parent-id='3-1-1-2-3-1-1-1'><td><span class='folder'>AccessibilityCarbonRef</span></td><td>Folder</td><td>--</td></tr>
<tr data-tt-id='3-1-1-2-3-1-1-1-1-1' data-tt-parent-id='3-1-1-2-3-1-1-1-1'><td><span class='folder'>CarbonAXRefRevisions</span></td><td>Folder</td><td>--</td></tr>
<tr data-tt-id='3-1-1-2-3-1-1-1-1-1-1' data-tt-parent-id='3-1-1-2-3-1-1-1-1-1'><td><span class='file'>CarbonAXRefRevisions.html</span></td><td>File</td><td>7.44 KB</td></tr>
<tr data-tt-id='3-1-1-2-3-1-1-1-1-2' data-tt-parent-id='3-1-1-2-3-1-1-1-1'><td><span class='folder'>Index</span></td><td>Folder</td><td>--</td></tr>
<tr data-tt-id='3-1-1-2-3-1-1-1-1-2-1' data-tt-parent-id='3-1-1-2-3-1-1-1-1-2'><td><span class='file'>index_of_book.html</span></td><td>File</td><td>174.1 KB</td></tr>
<tr data-tt-id='3-1-1-2-3-1-1-1-1-3' data-tt-parent-id='3-1-1-2-3-1-1-1-1'><td><span class='file'>index.html</span></td><td>File</td><td>1.1 KB</td></tr>
<tr data-tt-id='3-1-1-2-3-1-1-1-1-4' data-tt-parent-id='3-1-1-2-3-1-1-1-1'><td><span class='folder'>Reference</span></td><td>Folder</td><td>--</td></tr>
<tr data-tt-id='3-1-1-2-3-1-1-1-1-4-1' data-tt-parent-id='3-1-1-2-3-1-1-1-1-4'><td><span class='file'>reference.html</span></td><td>File</td><td>196.28 KB</td></tr>
<tr data-tt-id='3-1-1-2-3-1-1-1-1-5' data-tt-parent-id='3-1-1-2-3-1-1-1-1'><td><span class='file'>toc.html</span></td><td>File</td><td>15.92 KB</td></tr>
<tr data-tt-id='3-1-1-2-3-1-1-1-2' data-tt-parent-id='3-1-1-2-3-1-1-1'><td><span class='folder'>AccessibilityLowlevel</span></td><td>Folder</td><td>--</td></tr>
  . . .

我的XML源具有以下结构:

<root xmlns="http://example.com">
    <content>
        <folder>
            <config>
                <name>Root</name>
                <updatedDate>2017-04-05T09:48:19.619-04:00</updatedDate>
            </config>
            <folder>
                <config>
                    <name>Email</name>
                    <updatedDate>2017-04-05T09:47:58.669-04:00</updatedDate>
                </config>
                <folder>
                    <config>
                        <name>Documents</name>
                        <updatedDate>2017-04-05T09:47:58.669-04:00</updatedDate>
                    </config>
                    <file>
                        <config>
                            <name>Resume.doc</name>
                            <additionalElements>...</additionalElements>
                            <updatedDate>2017-04-05T09:40:03.022-04:00</updatedDate>
                        </config>
                        <permissions>
                            <read>yes</read>
                            <write>yes</write>
                            <additionalElements>...</additionalElements>
                        </permissions>
                    </file>
                    <file>
                        <occasionalFileOrFolderResultToIgnore/>
                        <config>
                            <name>Letter.doc</name>
                            <additionalElements>...</additionalElements>
                            <updatedDate>2017-04-05T09:47:58.669-04:00</updatedDate>
                        </config>
                        <permissions>
                            <read>yes</read>
                            <write>yes</write>
                            <additionalElements>...</additionalElements>
                        </permissions>
                    </file>
                    <permissions>
                        <read>yes</read>
                        <write>yes</write>
                    </permissions>
                </folder>
                <permissions>
                   <read>yes</read>
                   <write>yes</write>
                </permissions>
            </folder>
            <permissions>
               <read>yes</read>
               <write>no</write>
            </permissions>
       </folder>
   </root>
</content>

请帮忙! :)

1 个答案:

答案 0 :(得分:0)

我在输入输出时遇到了一些麻烦,但我认为这可以通过xsl:number level="multiple"完成。像

这样的东西
<xsl:template match="folder">
  <td>
    <xsl:attribute name="data-tt-id">
      <xsl:number level="multiple" count="folder" format="1-1"/>
    </attribute>
    <xsl:attribute name="data-tt-parent-id">
      <xsl:number select=".." level="multiple" count="folder" format="1-1"/>
    </xsl:attribute>
     ...
    <xsl:apply-templates/>
  </td>
</xsl:template>