Click to See Complete Forum and Search --> : XML/XSL Sort


Drewsuredaddy
07-17-2007, 12:53 PM
I have XML code that looks like the following:


<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:


<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:


<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

Drewsuredaddy
07-18-2007, 06:32 AM
This is the XSL code I used that correctly sorted the files:


<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>