Solved

Sort XML Elements

Posted on 2013-01-02
6
445 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

688 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