Results 1 to 5 of 5
  1. #1
    keioGirl is offline Member
    Join Date
    Jul 2008
    Posts
    7
    Rep Power
    0

    Post Sorting xml file

    I have an xml file like this:

    <rec>
    <name>Smith</name>
    <loc>USA</loc>
    </rec>
    <rec>
    <name>Bob</name>
    <loc>USA</loc>
    </rec>
    <rec>
    <name>John</name>
    <loc>UK</loc>
    </rec>
    <rec>
    <name>Smith</name>
    <loc>SG</loc>
    </rec>
    <rec>
    <name>Amy</name>
    <loc>AUC</loc>
    </rec>

    And I would like to sort those records in ascending order by <name> tag. And you can see that Smith has two records. But the location is not same.,. i.e. they are different Smith. So for that case, I would like to be like this:
    <rec>
    <name>Smith</name>
    <loc>USA</loc>
    <loc>SG</loc>
    </rec>

    So, my ouput file will be like this:

    <rec>
    <name>Amy</name>
    <loc>AUC</loc>
    </rec>
    <rec>
    <name>Bob</name>
    <loc>USA</loc>
    </rec>
    <rec>
    <name>John</name>
    <loc>UK</loc>
    </rec>
    <rec>
    <name>Smith</name>
    <loc>USA</loc>
    <loc>SG</loc>
    </rec>

    I will do my implementation in Java. So, I would like to know how can I do this? And if there any reference, please let me know.
    Thanks in advance!!

  2. #2
    w3user is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    define your transformation logic (sorting) in an xsl file
    use java api for xslt to transform your input xml into a sorted xml

    look into the "Transforming XML" section in the below url
    java.sun.com/developer/technicalArticles/xml/WebAppDev2/#code13

    other refs
    ---------
    xml.com/pub/a/2002/07/03/transform.html
    xml.com/lpt/a/991
    w3schools.com/xsl/el_sort.asp
    w3schools.com/XSL/xsl_sort.asp

  3. #3
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    The upthread link is the answer, but I have another question:

    why do you want to do such a thing?

    XML represents a tree structure, so any sorting should/must respect the tree.

  4. #4
    keioGirl is offline Member
    Join Date
    Jul 2008
    Posts
    7
    Rep Power
    0

    Default

    Hi thanks. I try to do it coz I just want to sort and group according to name. I want to use that xml file for indexing.

    Actually, I am just a newbie and trying to do something out.
    I am trying to do it with xslt. But it can't sort my xml file. Here is my xslt.

    <xsl:template match="entry">
    <xsl:for-each select="kw">
    <xsl:sort select="kw" order="ascending"/>
    <xsl:copy-of select="."/>
    </xsl:for-each>
    </xsl:template>

    And the output is like this:
    Smith
    USA
    Bob
    USA
    John
    UK
    Smith
    SG
    Amy
    AUC

    I don't know where is wrong. And how can I do it to show within <rec> <name> and <loc> tag.
    Thanks.

  5. #5
    w3user is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    Try this out:
    Note:
    My submissions are not allowed to have hyperlink
    you need to update w3.org/1999/XSL/Transform by appending "http" ":" "//" "www" to make it a hyperlink


    [records.xml]
    <?xml-stylesheet href="transformer.xsl" type="text/xsl"?>
    <recs>
    <rec>
    <name>Smith</name>
    <loc>USA</loc>
    </rec>
    <rec>
    <name>Bob</name>
    <loc>USA</loc>
    </rec>
    <rec>
    <name>John</name>
    <loc>UK</loc>
    </rec>
    <rec>
    <name>Smith</name>
    <loc>SG</loc>
    </rec>
    <rec>
    <name>Amy</name>
    <loc>AUC</loc>
    </rec>
    </recs>



    [transformer.xsl]
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="w3.org/1999/XSL/Transform">

    <xsl:template match="/">
    <recs>
    <xsl:for-each select="recs/rec">
    <xsl:sort select="name"/>
    <rec>
    <name><xsl:value-of select="name"/></name>
    <loc><xsl:value-of select="loc"/></loc>
    </rec>
    </xsl:for-each>
    </recs>
    </xsl:template>

    </xsl:stylesheet>

Similar Threads

  1. Data Sorting in a .data file using java
    By stutiger99 in forum New To Java
    Replies: 2
    Last Post: 10-08-2008, 02:52 AM
  2. sorting
    By jot321 in forum New To Java
    Replies: 18
    Last Post: 10-02-2008, 10:30 AM
  3. Sorting CachedRowset
    By Sayed in forum Advanced Java
    Replies: 0
    Last Post: 07-18-2008, 12:14 PM
  4. sorting problem...
    By mark-mlt in forum New To Java
    Replies: 4
    Last Post: 04-17-2008, 02:15 PM
  5. Heap Sorting
    By kesav2005 in forum New To Java
    Replies: 1
    Last Post: 11-13-2007, 04:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •