Solved

Sort XML Elements

Posted on 2013-01-02
6
437 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now