Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sort XML Elements

Posted on 2013-01-02
6
Medium Priority
?
447 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 60

Accepted Solution

by:
Geert Bormans earned 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

609 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