1

I have the following XML

<fields>
  <field position="4" tablename="Students" headername="First Name" fieldreference="FirstName" orderbydirection="ASC" />
  <field position="2" tablename="Students" headername="Last Name" fieldreference="LastName" orderbydirection="ASC" />
  <field position="3" tablename="Students" headername="Race" fieldreference="Race" orderbydirection="ASC" />
  <field position="1" tablename="Students" headername="Sex" fieldreference="Sex" orderbydirection="ASC" />
  <field position="5" tablename="Students" headername="State" fieldreference="State" orderbydirection="ASC" />
</fields>

I want to sort it based on the "position"

How can it be done?

Thanks in advance!

5
  • Using what? XSLT? a bash script? magic wand? Commented Jan 30, 2009 at 17:45
  • @Chris - Good edit. This way the syntax highlighting completely breaks. Commented Jan 30, 2009 at 17:46
  • @Sean Bright- Your right... Sorry, I should roll it back and leave it completely unformatted.... GREAT IDEA.... Commented Jan 30, 2009 at 17:49
  • I am storing the XML in the DB table. I am then retrieving it and reaching this node using m_nodelist = m_xmld.SelectNodes("report/query/select/fields/field") now i want to sort it in the code behind itself and save it back to the Daabase table Commented Jan 30, 2009 at 19:11
  • @Chris - I think you meant "you're right" instead of "your right." Sean 2, Chris 0 :P Commented Jan 31, 2009 at 1:14

2 Answers 2

1

See sorting with XSLT http://www.xml.com/pub/a/2002/07/03/transform.html

Sign up to request clarification or add additional context in comments.

Comments

1

This XSLT 1.0 transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:strip-space elements="*"/>

    <xsl:template match="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="fields">
      <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates>
          <xsl:sort select="@position" data-type="number"/>
        </xsl:apply-templates>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<fields>
    <field position="4" tablename="Students" headername="First Name" fieldreference="FirstName" orderbydirection="ASC" />
    <field position="2" tablename="Students" headername="Last Name" fieldreference="LastName" orderbydirection="ASC" />
    <field position="3" tablename="Students" headername="Race" fieldreference="Race" orderbydirection="ASC" />
    <field position="1" tablename="Students" headername="Sex" fieldreference="Sex" orderbydirection="ASC" />
    <field position="5" tablename="Students" headername="State" fieldreference="State" orderbydirection="ASC" />
</fields>

produces the wanted result:

<fields>
  <field position="1" tablename="Students" headername="Sex" fieldreference="Sex" orderbydirection="ASC" />
  <field position="2" tablename="Students" headername="Last Name" fieldreference="LastName" orderbydirection="ASC" />
  <field position="3" tablename="Students" headername="Race" fieldreference="Race" orderbydirection="ASC" />
  <field position="4" tablename="Students" headername="First Name" fieldreference="FirstName" orderbydirection="ASC" />
  <field position="5" tablename="Students" headername="State" fieldreference="State" orderbydirection="ASC" />
</fields>

3 Comments

I was able to do this using LINQ in 2 lines of code. Thakns everyone
Dim sFieldList = From field In SortedFields.Descendants("field") Order By Integer.Parse(field.@position)
Actually here is the full code Dim feedXML As XDocument = XDocument.Parse(sXMLDefinition.ToString()) Dim SortedFields = From field In feedXML.Descendants("fields") Dim sFieldList = From field In SortedFields.Descendants("field") Order By Integer.Parse(field.@position)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.