XSL Copy Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ajc308
    New Member
    • Jul 2007
    • 11

    XSL Copy Problem

    I'm attempting to sort the <file>s within each <directory> in my XML according to their file extension, then write out the resulting sorted data back to XML format. I had it working before, and when I opened up the file the next day, the XSL behaved completely different. I don't know if I accidentally changed something, but I can't see any problems with my code.

    My Sample XML:

    Code:
    <root name="PlanRepository">
      <directory name="connoraj">
        <directory name="single_run.dat.07-23-2007.10-59-51">
          <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.est_pv.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>
            <file>single_run.tar_inertial.dat</file>
            <file>single_run.true_pv.dat</file>
          </directory>
        </directory>
      </directory>
    </root>
    My XSL File:

    Code:
    <xsl:template match="root">
        <xsl:copy>
          <xsl:attribute name="name">PlanRepository</xsl:attribute>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="directory">
        <xsl:copy>
          <xsl:copy-of select="@name"/>
          <xsl:choose>
            <xsl:when test="@name = 'SAFE_Output'">
              <xsl:for-each select="file">
                <xsl:sort select="substring-after(substring-after(.,'.'),'.')" />
                <xsl:copy-of select="."/>
              </xsl:for-each>
            </xsl:when>
            <xsl:otherwise>
              <xsl:for-each select="file">
                <xsl:sort select="substring-after(.,'.')" />
                <xsl:copy-of select="."/>
              </xsl:for-each>
            </xsl:otherwise>
          </xsl:choose>
          <xsl:apply-templates select="directory"/>
        </xsl:copy>
      </xsl:template>
    My resulting output:
    Code:
    insidebox.txtoutsidebox.txtsingle_run.datleapseconds.txtcontrol_earth.txtleo_sc_prop2.txtsingle_run.control_filessingle_run.aer.datsingle_run.dv.datsingle_run.est_pv.datsingle_run.host_inertial.datsingle_run.residuals.datsingle_run.tar_inertial.datsingle_run.true_pv.datsingle_run.host_ephem.esingle_run.tar_ephem.e
    My desired output XML (<file>s are sorted by file extension):

    Code:
    <root name="PlanRepository">
      <directory name="connoraj">
        <directory name="single_run.dat.07-23-2007.10-59-51">
          <file>insidebox.txt</file>
          <file>outsidebox.txt</file>
          <directory name="SAFE_Input">
            <file>single_run.dat</file>
            <file>leapseconds.txt</file>
            <directory name="LeoInputs">
              <file>control_earth.txt</file>
              <file>leo_sc_prop2.txt</file>
            </directory>
          </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.est_pv.dat</file>        
            <file>single_run.host_inertial.dat</file>
            <file>single_run.residuals.dat</file>
            <file>single_run.tar_inertial.dat</file>
            <file>single_run.true_pv.dat</file>
            <file>single_run.host_ephem.e</file>
            <file>single_run.tar_ephem.e</file>        
          </directory>
        </directory>
      </directory>
    </root>
    Can anyone help me out with this problem?

    Thanks,
    Andrew
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    For some reason or another, you're not hitting your root template.
    Check to make sure that you don't have any xmlns: attribute in your source xml, and that root is spelled with the same capitalization in both your source xml and your xslt.

    Comment

    Working...