Solved

Sort XML Elements

Posted on 2013-01-02
6
443 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
Technology Partners: 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

762 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