我想要一些帮助用 xslt 解析动态 (folder.subfolders) xml,我尝试了一些东西,但我还没有。这是我拥有的xml的结构:
<FOLDERS>
<FOLDER>
<FOLDERID>2edfb864-5693-4e7f-8f98-4ef6e032d8a5</FOLDERID>
<NAME>bla</NAME>
<FOLDERSIZE>33Kb</FOLDERSIZE>
<LASTMODIFIED>2012-06-07 11:11:02</LASTMODIFIED>
<SUBFOLDERS />
<FILES>
<FILE>
<FILEID>1825</FILEID>
<NAME>IMG_15052012_142711.png</NAME>
<SIZE>33Kb</SIZE>
<EXTENSION>png</EXTENSION>
<LASTMODIFIED />
</FILE>
</FILES>
</FOLDER>
<FOLDER>
<FOLDERID>c9c5e2b2-ee93-49a2-b8be-d86e41528071</FOLDERID>
<NAME>TestFolder</NAME>
<FOLDERSIZE>0Kb</FOLDERSIZE>
<LASTMODIFIED>2012-06-05 00:00:00</LASTMODIFIED>
<SUBFOLDERS>
<FOLDER>
<FOLDERID>3b43fe58-db85-43e6-b541-77ce250afc78</FOLDERID>
<NAME>SubFolder</NAME>
<FOLDERSIZE>362Kb</FOLDERSIZE>
<LASTMODIFIED>2012-06-05 00:00:00</LASTMODIFIED>
<SUBFOLDERS>
<FOLDER>
<FOLDERID>ac1d35d0-80fd-4d97-8101-4de408f24dbd</FOLDERID>
<NAME>subsub</NAME>
<FOLDERSIZE>0Kb</FOLDERSIZE>
<LASTMODIFIED>2012-06-05 00:00:00</LASTMODIFIED>
<SUBFOLDERS />
<FILES />
</FOLDER>
<FOLDER>
<FOLDERID>7cfdc199-9d0e-4f86-904d-eda3521f3e87</FOLDERID>
<NAME>SUBSUB1</NAME>
<FOLDERSIZE>0Kb</FOLDERSIZE>
<LASTMODIFIED>2012-06-05 00:00:00</LASTMODIFIED>
<SUBFOLDERS />
<FILES />
</FOLDER>
</SUBFOLDERS>
<FILES>
<FILE>
<FILEID>1833</FILEID>
<NAME>Untitled.png</NAME>
<SIZE>121Kb</SIZE>
<EXTENSION>png</EXTENSION>
<LASTMODIFIED />
</FILE>
<FILE>
<FILEID>1834</FILEID>
<NAME>Untitled.png</NAME>
<SIZE>121Kb</SIZE>
<EXTENSION>png</EXTENSION>
<LASTMODIFIED />
</FILE>
<FILE>
<FILEID>1839</FILEID>
<NAME>Untitled.png</NAME>
<SIZE>121Kb</SIZE>
<EXTENSION>png</EXTENSION>
<LASTMODIFIED />
</FILE>
</FILES>
</FOLDER>
</SUBFOLDERS>
<FILES />
</FOLDER>
<FOLDER>
<FOLDERID>1ead58e2-a412-4318-9629-d5c70d8681bc</FOLDERID>
<NAME>TestFolder1</NAME>
<FOLDERSIZE>241Kb</FOLDERSIZE>
<LASTMODIFIED>2012-06-05 00:00:00</LASTMODIFIED>
<SUBFOLDERS />
<FILES>
<FILE>
<FILEID>1832</FILEID>
<NAME>Untitled.png</NAME>
<SIZE>121Kb</SIZE>
<EXTENSION>png</EXTENSION>
<LASTMODIFIED />
</FILE>
<FILE>
<FILEID>1837</FILEID>
<NAME>Untitled.png</NAME>
<SIZE>121Kb</SIZE>
<EXTENSION>png</EXTENSION>
<LASTMODIFIED />
</FILE>
</FILES>
</FOLDER>
</FOLDERS>
<FILES>
<FILE>
<FILEID>1836</FILEID>
<NAME>Untitled.png</NAME>
<SIZE>121Kb</SIZE>
<EXTENSION>png</EXTENSION>
<LASTMODIFIED />
</FILE>
<FILE>
<FILEID>1869</FILEID>
<NAME>WallPaper.png</NAME>
<SIZE>7.1Mb</SIZE>
<EXTENSION>png</EXTENSION>
<LASTMODIFIED />
</FILE>
</FILES>
问题是我不知道文件夹/子文件夹树的深度。我已经做了一些事情,但它仍然是静态的,它也需要是动态的,尤其是在 xslt 条件下的选择部分。
你们认为在这种情况下解析 xml 的最佳方法是什么?
这是我想要的结构:
Folders
Subfolders
Subfolders
Subfolders
Files
Files
Files
Files
这就是我现在所拥有的:
<xsl:variable name="folderRoot" select="$xml/ROOT/FOLDERS/FOLDER" />
<xsl:variable name="fileRoot" select="$xml/ROOT/FILES/FILE" />
//Loop through the root folder
<xsl:if test="count($folderRoot) > 0">
<xsl:call-template name="folder"/>
</xsl:if>
//Loop through the root files
<xsl:if test="count($fileRoot) > 0">
<xsl:call-template name="files"/>
</xsl:if>
//files template
<xsl:template name="files">
<xsl:for-each select="$fileRoot">
<tr>
<td class="defaultIcon {EXTENSION}">
</td>
<td>
<xsl:value-of select="NAME" />
</td>
</tr>
</xsl:for-each>
</xsl:template>
//Folder template
<xsl:template name="folder">
<xsl:for-each select="$folderRoot">
<tr>
<td>
<img src="/default/components/bestandsbeheer/images/folder.png" width="16" height="16" />
</td>
<td>
<xsl:value-of select="NAME"/>
</td>
<td> </td>
<td>Map</td>
<td>
<xsl:value-of select="LASTMODIFIED" />
</td>
</tr>
<!--SUBFOLDER 1 -->
<xsl:if test="count(SUBFOLDERS/FOLDER) > 0">
<xsl:for-each select="SUBFOLDERS/FOLDER">
<!--SUBFOLDER 2 -->
<xsl:if test="count($folderRoot/SUBFOLDERS/FOLDER/SUBFOLDERS/FOLDER) > 0">
<xsl:for-each select="$folderRoot/SUBFOLDERS/FOLDER/SUBFOLDERS/FOLDER">
</xsl:for-each>
</xsl:if>
<xsl:if test="count($folderRoot/SUBFOLDERS/FOLDER/FILES) > 0">
<xsl:for-each select="$folderRoot/SUBFOLDERS/FOLDER/FILES">
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:if>
//Files belong to a sub folder
<xsl:if test="count(FILES) > 0">
<xsl:for-each select="FILES/FILE">
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:template>
如果有不清楚的地方,请告诉我。