Q: Dynamic parsing of xml with xslt I would like some help parsing a dynamic (folder.subfolders) xml with xslt, I tried a few things but I'm not there yet. Here is the structure of the xml that I have:
<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>

The thing is that I don't know how deep the folders/subfolders tree can be.
I did already something but it is still static, it needs to be dynamic also, especially the select part if the xslt condition.
What do you guys think the best approach to parse the xml in this case?
This is the structure I would like to have:
Folders
     Subfolders       
          Subfolders 
               Subfolders
               Files
          Files
     Files 
Files  

And this is what I have now:
<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) &gt; 0">
  <xsl:call-template name="folder"/>
</xsl:if>

//Loop through the root files    
<xsl:if test="count($fileRoot) &gt; 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) &gt; 0">
        <xsl:for-each select="SUBFOLDERS/FOLDER">
         <!--SUBFOLDER 2 -->
          <xsl:if test="count($folderRoot/SUBFOLDERS/FOLDER/SUBFOLDERS/FOLDER) &gt; 0">
            <xsl:for-each select="$folderRoot/SUBFOLDERS/FOLDER/SUBFOLDERS/FOLDER">
            </xsl:for-each>
          </xsl:if>
          <xsl:if test="count($folderRoot/SUBFOLDERS/FOLDER/FILES) &gt; 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) &gt; 0">
        <xsl:for-each select="FILES/FILE">

        </xsl:for-each>
      </xsl:if>

    </xsl:for-each>
  </xsl:template>

Let me know if something is not clear.

A: It's not clear what output you want, but you need to learn how to use xsl:apply-templates, which is perfectly suited to processing this kind of recursive data using a recursive descent. I would expect your stylesheet to contain template rules that do something like:
<xsl:template match="FOLDER">
  <xsl:apply-templates select="FILES"/>
  <xsl:apply-templates select="SUBFOLDERS"/>
</xsl:template>

<xsl:template match="FILES">
  <xsl:apply-templates select="FILE"/>
</xsl:template>

<xsl:template match="SUBFOLDERS">
  <xsl:apply-templates select="FOLDER"/>
</xsl:template>

Try to avoid xsl:for-each and xsl:if. They have a valid role to play, but don't use them until you have tried it first with template rules and match patterns: that's the XSLT way, and it's for this kind of structure that it really makes a difference.

A: Something like this? It's a skeleton structure but you get the idea.
http://www.xmlplayground.com/c848Vl
