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="/">
<root name="PlanRepository">
<br/>
<xsl:for-each select="//directory">
<directory name="<xsl:value-of select="@name"/>">
<br/>
<xsl:choose>
<xsl:when test="@name = 'SAFE_Output'">
<xsl:for-each select="file">
<xsl:sort select="substring-after(substring-after(.,'.'),'.')" />
<file><xsl:value-of select="."/></file>
<br/>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="file">
<xsl:sort select="substring-after(.,'.')" />
<file><xsl:value-of select="."/></file>
<br/>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</directory>
<br/>
</xsl:for-each>
</root>
</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
<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="/">
<root name="PlanRepository">
<br/>
<xsl:for-each select="//directory">
<directory name="<xsl:value-of select="@name"/>">
<br/>
<xsl:choose>
<xsl:when test="@name = 'SAFE_Output'">
<xsl:for-each select="file">
<xsl:sort select="substring-after(substring-after(.,'.'),'.')" />
<file><xsl:value-of select="."/></file>
<br/>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="file">
<xsl:sort select="substring-after(.,'.')" />
<file><xsl:value-of select="."/></file>
<br/>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</directory>
<br/>
</xsl:for-each>
</root>
</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