www.webdeveloper.com
Results 1 to 2 of 2

Thread: XML/XSL Sort

Hybrid View

  1. #1
    Join Date
    Jun 2005
    Posts
    3

    XML/XSL Sort

    I have XML code that looks like the following:

    Code:
    <root name="PlanRepository">
      <directory name="connoraj">
        <directory name="singlerun">
          <file>insidebox.txt</file>
          <file>outsidebox.txt</file>
          <directory name="SAFE_Input">
            <file>leapseconds.txt</file>
            <file>single_run.dat</file>
            <directory name="LeoInputs">
              <file>control_earth.txt</file>
              <file>leo_sc_prop2.txt</file>
            </directory>
          </directory>
          <directory name="SAFE_Output">
            <file>single_run.aer.dat</file>
            <file>single_run.control_files</file>
            <file>single_run.dv.dat</file>
            <file>single_run.host_ephem.e</file>
            <file>single_run.host_inertial.dat</file>
            <file>single_run.residuals.dat</file>
            <file>single_run.tar_ephem.e</file>
          </directory>
        </directory>
      </directory>
    </root>
    And my goal is, that in each <directory> to sort its respective <file>s by extension, then write it back in XML format. I can get this all to work, except for getting the <directory> tags to close correctly.

    This is the XSL I am using:

    Code:
    <xsl:template match="/">
        &lt;root name="PlanRepository"&gt;
        <br/>
      <xsl:for-each select="//directory">
        &lt;directory name="<xsl:value-of select="@name"/>"&gt;
        <br/>
        <xsl:choose>
          <xsl:when test="@name = 'SAFE_Output'">
            <xsl:for-each select="file">
              <xsl:sort select="substring-after(substring-after(.,'.'),'.')" />
              &lt;file&gt;<xsl:value-of select="."/>&lt;/file&gt; 
              <br/>
            </xsl:for-each>
          </xsl:when>
          <xsl:otherwise>
            <xsl:for-each select="file">
              <xsl:sort select="substring-after(.,'.')" />
              &lt;file&gt;<xsl:value-of select="."/>&lt;/file&gt;
              <br/>
            </xsl:for-each>
          </xsl:otherwise>
        </xsl:choose>
        &lt;/directory&gt;
        <br/>
      </xsl:for-each>
        &lt;/root&gt;
    </xsl:template>
    This XSL yields the following XML output:

    Code:
    <root name="PlanRepository">
      <directory name="connoraj">
      </directory>
      <directory name="singlerun">
        <file>insidebox.txt</file>
        <file>outsidebox.txt</file>
      </directory>
      <directory name="SAFE_Input">
        <file>single_run.dat</file>
        <file>leapseconds.txt</file>
      </directory>
      <directory name="LeoInputs">
        <file>control_earth.txt</file>
        <file>leo_sc_prop2.txt</file>
      </directory>
      <directory name="SAFE_Output">
        <file>single_run.control_files</file>
        <file>single_run.aer.dat</file>
        <file>single_run.dv.dat</file>
        <file>single_run.host_inertial.dat</file>
        <file>single_run.residuals.dat</file>
        <file>single_run.host_ephem.e</file>
        <file>single_run.tar_ephem.e</file>
      </directory>
    </root>
    So as you can see, the files are correctly sorted by extension, but the XML formatting is no longer the same.

    Any ideas? Please help!

    Thanks,
    Andrew

  2. #2
    Join Date
    Jun 2005
    Posts
    3

    Problem Solved

    This is the XSL code I used that correctly sorted the files:

    Code:
      <xsl:template match="root">
        &lt;root name="PlanRepository"&gt;
        <br/>
        <xsl:apply-templates />
        &lt;/root&gt;
      </xsl:template>
    
      <xsl:template match="directory">
        <xsl:param name="directoryName">
          <xsl:value-of select="@name" />
        </xsl:param>
        &lt;directory name="<xsl:value-of select="@name"/>"&gt;
        <br/>
        <xsl:choose>
          <xsl:when test="$directoryName = 'SAFE_Output'">
            <xsl:for-each select="file">
              <xsl:sort select="substring-after(substring-after(.,'.'),'.')" />
              &lt;file&gt;<xsl:value-of select="."/>&lt;/file&gt;
              <br/>
            </xsl:for-each>
          </xsl:when>
          <xsl:otherwise>
            <xsl:for-each select="file">
              <xsl:sort select="substring-after(.,'.')" />
              &lt;file&gt;<xsl:value-of select="."/>&lt;/file&gt;
              <br/>
            </xsl:for-each>
          </xsl:otherwise>
        </xsl:choose>
        <xsl:apply-templates/>
        &lt;/directory&gt;
        <br/>
      </xsl:template>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles