tags:

views:

76

answers:

1

I want to sort a 'free-form' XML file through multiple attributes (first by T and then by L). The XML is a bit complex and it is structured as shown below:

<?xml version="1.0" encoding="utf-8"?>
<wb xmlns:cf="http://www.macromedia.com/2004/cfform" xmlns:a="urn:dummy">
  <a:form name="chart">
    <a:fieldset FIELD="a" FIELDNAME="FieldSet1">
      <a:select1 FIELDNUMBER="01" L="1" T="2" />
      <a:input FIELDNUMBER="02" INDEX="4" L="200" T="1" />
    </a:fieldset>
    <a:fieldset FIELD="b" FIELDNAME="FieldSet1">
      <a:select1 FIELDNUMBER="03" T="3" L="1" />
      <a:input FIELDNUMBER="04" INDEX="7" T="4" L="200" />
      <a:fieldset FIELD="c" FIELDNAME="FieldSet1">
        <a:input FIELDNUMBER="05" T="10" INDEX="6" L="400" />
        <a:input FIELDNUMBER="06" T="8" INDEX="8" L="200" />
      </a:fieldset>
    </a:fieldset>
    <a:input FIELDNUMBER="08" INDEX="3" L="3" T="5" />
    <a:input FIELDNUMBER="09" INDEX="2" L="2" T="5" />
  </a:form>
</wb>

PS:

  1. The root element is wb and this is always followed by a:form
  2. The L and T are always found in elements that have a tag in the namespace a, the only exception being a:fieldset which does not have L and T
  3. a:fieldset could have multiple children of the namespace a including another a:fieldset
  4. When sorting the children that are in a fieldset these need to remain attached to their current parent.

The resulting output should be the following:

<?xml version="1.0" encoding="utf-8"?>
<wb xmlns:cf="http://www.macromedia.com/2004/cfform" xmlns:a="urn:dummy">
  <a:form name="chart">
    <a:fieldset FIELD="a" FIELDNAME="FieldSet1">
      <a:input FIELDNUMBER="02" INDEX="4" L="200" T="1" />
      <a:select1 FIELDNUMBER="01" L="1" T="2" />
    </a:fieldset>
    <a:fieldset FIELD="b" FIELDNAME="FieldSet1">
      <a:select1 FIELDNUMBER="03" T="3" L="1" />
      <a:input FIELDNUMBER="04" INDEX="7" T="4" L="200" />
      <a:fieldset FIELD="c" FIELDNAME="FieldSet1">
        <a:input FIELDNUMBER="06" T="8" INDEX="8" L="200" />
        <a:input FIELDNUMBER="05" T="10" INDEX="6" L="400" />
      </a:fieldset>
    </a:fieldset>
    <a:input FIELDNUMBER="09" INDEX="2" L="2" T="5" />
    <a:input FIELDNUMBER="08" INDEX="3" L="3" T="5" />
  </a:form>
</wb>

For better understanding, we can assume that L denotes Left and T denotes Top. So, the idea of this is that when I view the transformed XML I can immediately note which elements precede what.

What's your take on this?

A: 

try this: (make some changes if I have not understood your questions correctly)

<xsl:template match="/wb">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates mode="aaa" select="."/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*" mode="aaa">
    <xsl:if test="child::*[1][not(@T) or not(@L)]">
      <xsl:for-each select="child::*[not(@T) or not(@L)]">
        <xsl:copy>
          <xsl:copy-of select="@*"/>
          <xsl:apply-templates mode="aaa" select="."/>
        </xsl:copy>
      </xsl:for-each>
    </xsl:if>
    <xsl:variable name="sorted1">
      <xsl:for-each select="a:*[@T and @L]">
        <xsl:sort select="@T" data-type="number" order="ascending"></xsl:sort>
        <xsl:copy>
          <xsl:copy-of select="@*"/>
        </xsl:copy>
      </xsl:for-each>
    </xsl:variable>
    <xsl:for-each select="msxsl:node-set($sorted1)/*[not(@T=preceding-sibling::*/@T)]">
      <xsl:variable name="tval" select="@T"/>
      <xsl:for-each select="msxsl:node-set($sorted1)/*[@T=$tval]">
        <xsl:sort select="@L" data-type="number" order="ascending"></xsl:sort>
        <xsl:copy>
          <xsl:copy-of select="@*"/>
        </xsl:copy>
      </xsl:for-each>
    </xsl:for-each>

    <xsl:if test="not(child::*[1][not(@T) or not(@L)])">
      <xsl:for-each select="child::*[not(@T) or not(@L)]">
        <xsl:copy>
          <xsl:copy-of select="@*"/>
          <xsl:apply-templates mode="aaa" select="."/>
        </xsl:copy>
      </xsl:for-each>
    </xsl:if>

  </xsl:template>
gp