Solved

Sort XML Elements

Posted on 2013-01-02
6
441 Views
Last Modified: 2013-02-19
Having following XML, Want to sort this based on the values in XML elements:

<Address>
      <HomeNumber />
      <HouseAddress />
</Address>
<Address>
      <HomeNumber />
      <HouseAddress />
</Address>
<Address>
      <HomeNumber />
      <HouseAddress />
</Address>
      <Address>
      <HomeNumber />
      <HouseAddress />
</Address>

<Address>
      <HomeNumber>00</HomeNumber>
      <HouseAddress>PPP Nagar</HouseAddress>
</Address>
<Address>
      <HomeNumber>01</HomeNumber>
      <HouseAddress>Street 145</HouseAddress>
</Address>
<Address>
      <HomeNumber>02</HomeNumber>
      <HouseAddress>Street 798</HouseAddress>
</Address>


Expected Output:

<Address>
      <HomeNumber>00</HomeNumber>
      <HouseAddress>PPP Nagar</HouseAddress>
</Address>
<Address>
      <HomeNumber>01</HomeNumber>
      <HouseAddress>Street 145</HouseAddress>
</Address>
<Address>
      <HomeNumber>02</HomeNumber>
      <HouseAddress>Street 798</HouseAddress>
</Address>
<Address>
      <HomeNumber />
      <HouseAddress />
</Address>
<Address>
      <HomeNumber />
      <HouseAddress />
</Address>
<Address>
      <HomeNumber />
      <HouseAddress />
</Address>
      <Address>
      <HomeNumber />
      <HouseAddress />
</Address>

Please suggets. Hopefully looking forward good replies..
0
Comment
Question by:satmisha
  • 2
  • 2
  • 2
6 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 38735963
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 38736021
0
 

Author Comment

by:satmisha
ID: 38736102
Thanks for your quick reply,
Would it be possible for you to please provide code for the same... as I am quite newbi.. though I am also trying...
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 60

Accepted Solution

by:
Geert Bormans earned 200 total points
ID: 38737173
I am puzzled
Your article says
"I have several times needed a way to sort XML, retrieved from a file or a web service, inline without invoking XSL transformations, which is the most common way to do it as I have seen. "
XSLT seems a natural fit to do stuff like this.
All you need is a simple XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
    
    <xsl:template match="root">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="Address[normalize-space(HomeNumber)]">
                <xsl:sort select="HomeNumber" data-type="number" order="ascending"/>
            </xsl:apply-templates>
            <xsl:apply-templates select="Address[not(normalize-space(HomeNumber))]"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="Address">
        <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>

Open in new window


Save the XSLT as a file.
Here is an example how you can use the XSLT with the XML

http://blog.garkbit.com/2009-12-14/transforming-xmlxslt-using-net-3-5/
0
 

Author Closing Comment

by:satmisha
ID: 38905046
Thanks
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 38905229
welcome
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Advice on Xojo as a development tool over VB. 4 57
Vb. Net application freezes 9 48
VB.Net - Project Software to help the Developer 7 36
Fulfillment API php code sample 1 41
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question