I was trying with the examples given at
http://www.yukonxml.com/articles/xquery/ by using saxon8.jar. Iam able to
sort only ONE time means i can sort by COUNTRY & not by STATE & vise versa
also a part of the xml code will be missed in the output.

Input XML File: countryinfo.xml
<?xml version="1.0" encoding="UTF-8"?>
<World>
    <Country name="Pakistan">
        <State name="Karachi" population="999"/>
        <State name="Islamabad" population="900"/>
        <State name="Rawalpandi" population="909"/>
    </Country>
    <Country name="India">
        <State name="Bangalore" population="100"/>
        <State name="AP" population="200"/>
        <State name="Madras" population="300"/>
        <State name="Delhi" population="600"/>
    </Country>
</World>

Query to sort by Country:
Query: for $country in doc("countryinfo.xml")/World/Country 
          order by $country/@name ascending
          return $country

OutPut:
<?xml version="1.0" encoding="UTF-8"?>
<Country name="India">
        <State name="Bangalore" population="100"/>
        <State name="AP" population="200"/>
        <State name="Madras" population="300"/>
        <State name="Delhi" population="600"/>
    </Country>
<Country name="Pakistan">
        <State name="Karachi" population="999"/>
        <State name="Islamabad" population="900"/>
        <State name="Rawalpandi" population="909"/>
    </Country>

Query to sort by State:
Query: for $country in doc("countryinfo.xml")/World/Country/State 
          order by $country/@name ascending
          return $country

OutPut:
<?xml version="1.0" encoding="UTF-8"?>
<State name="AP" population="200"/>
<State name="Bangalore" population="100"/>
<State name="Delhi" population="600"/>
<State name="Islamabad" population="900"/>
<State name="Karachi" population="999"/>
<State name="Madras" population="300"/>
<State name="Rawalpandi" population="909"/>

Java Program:
import net.sf.saxon.Query;
public class SortXMLData {
   public static void main(String[] args) {
       Query query = new Query();
       String arg[] = {"C:/countryinfo.xq"};  // contains query
       try{
        query.main(arg);
       } catch(Exception ex){
        System.out.println("Exception: "+ex);
      }
   }
}

- Khan
-- 
View this message in context: 
http://www.nabble.com/sorting-xml-data-in-alphabetical-order-tp19920246p21027601.html
Sent from the Xml Beans - Dev mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@xmlbeans.apache.org
For additional commands, e-mail: dev-h...@xmlbeans.apache.org

Reply via email to