bharathinc has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sort xml based on attribute
by marto (Cardinal) on Feb 18, 2010 at 12:26 UTC | |
See Re: XML::Twig -- sorting by attribute, super search is your friend. | [reply] |
|
Re: Sort xml based on attribute
by Corion (Patriarch) on Feb 18, 2010 at 12:27 UTC | |
While you have shown us some of your data, you haven't shown us any of your code and how it fails to produce the results you want. We cannot help you without knowing what you've done already and where your exact problem is. Personally, I would approach the problem by either restructuring/restringifying the XML fragments to be sorted so that they sort ASCIIbetically or by using XPath queries to extract the attributes after which the fragments are to be sorted and then output the new document by stringifying their nodes. For both approaches, XML::Twig or XML::LibXML would work, and also Jendas XML::Rules I suppose. | [reply] |
|
Re: Sort xml based on attribute
by Jenda (Abbot) on Feb 18, 2010 at 22:27 UTC | |
Basicaly ... whenever an <itemid> tag is fully parsed (including content and end tag), the code checks whether the idtype eq "PUI" and if it does it remembers the content in the tag's parent's parent's parent's parent (i.e. the <item> tag ... attributes starting by a colon are never exported to the resulting XML) and then it add the tag's data into the parent's content. Then the <item> tags are removed from the parent tag's content and stored in an array stored in the parent tag's hash of attributes under key "item". Then once the XML is fully parsed, the array of items is sorted, some whitespace gets inserted between the items and the resulting array becomes the contents of the root tag. And the tag with the attributes and content (including child tags) gets printed. The code assumes the <itemid> will always be at the same level below <item> and that there will only <item> tags in bibdataset! Jenda | [reply] [d/l] |
by Anonymous Monk on Aug 12, 2010 at 02:35 UTC | |
Note: It's actually a much much larger file (9510 lines) | [reply] [d/l] |
by Jenda (Abbot) on Aug 12, 2010 at 10:20 UTC | |
The code will be a bit simpler, but whether it will be any easier to understand I don't know. What language(s) do you have experience with?
Update: Please see Re^9: Sort xml based on attribute for a fixed version. Jenda | [reply] [d/l] |
by Anonymous Monk on Aug 12, 2010 at 11:48 UTC | |
by Jenda (Abbot) on Aug 12, 2010 at 12:53 UTC | |
| |
by Anonymous Monk on Aug 12, 2010 at 11:53 UTC | |
|
Re: Sort xml based on attribute
by mirod (Canon) on Feb 20, 2010 at 08:43 UTC | |
That's something I've had to do in the past, so as usual it ended up in XML::Twig. The sort_children method gets called on the parent, and gets passed a function, which will be called on each child in turn. That function will return the sort criteria. The method also takes options to specify the type of sort (numeric or alpha) and the order.
This leads to the code below:
Note that this code relies on a couple of assumptions:
Does this help? | [reply] [d/l] |
|
Re: Sort xml based on attribute
by runrig (Abbot) on Aug 12, 2010 at 15:18 UTC | |
| [reply] |
|
Re: Sort xml based on attribute
by choroba (Cardinal) on Aug 12, 2010 at 17:39 UTC | |
| [reply] [d/l] |
| A reply falls below the community's threshold of quality. You may see it by logging in. | |