Sorting xml
hi,
How can I sorting xml file before parsing it.
for example:
parser.parse("test.xml");
Document doc = parser.getDocument();
Thanks.
you can sort the data using xsl. Here's what i'm doing
empdata.xml file
========
<?xml version="1.0"?>
<?xml-stylesheet href="empdata.xsl" type="text/xsl"?>
<employees>
<employee>
<name>
<got>4</got>
<given>James</given>
<family>Cook</family>
</name>
</employee>
<employee>
<name>
<got>5</got>
<given>Aames</given>
<family>Dlark</family>
</name>
</employee>
<employee>
<name>
<got>1</got>
<given>Bames</given>
<family>Dlark</family>
</name>
</employee>
<employee>
<name>
<got>3</got>
<given>marshesi</given>
<family>park</family>
</name>
</employee>
<employee>
<name>
<got>7</got>
<given>Lames</given>
<family>lamb</family>
</name>
</employee>
</employees>
empdata.xsl
===========
<?xml version="1.0"?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method="xml" indent='yes'/>
<xsl:template match="employees">
<ul>
<xsl:apply-templates select="employee">
<xsl:sort select="name/got"/>
<xsl:sort select="name/family"/>
<xsl:sort select="name/given"/>
</xsl:apply-templates>
</ul>
</xsl:template>
<xsl:template match="employee">
<li>
<xsl:value-of select="name/got"/>
<xsl:text> </xsl:text>
<xsl:value-of select="name/given"/>
<xsl:text> </xsl:text>
<xsl:value-of select="name/family"/>
</li>
</xsl:template>
</xsl:stylesheet>
I hope this will help you.
sachin
Similar Messages
-
Query on sorting XML using XSLT and getting the same XML as output !
Hi,
Looking for one information regarding sorting XML using XSLT , with the sorted XML as output. For eg. my XML is :
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="sort1.xsl"?>
<levelone>
<child ID="1" sort="5">
<name>Paul</name>
</child>
<child ID="2" sort="1">
<name>Adam</name>
</child>
<child ID="3" sort="2">
<name>Will</name>
</child>
</levelone>
and XSL :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/levelone">
<xsl:copy>
<xsl:apply-templates>
<xsl:sort select="@sort"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="child">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
This does the sort based on Name. But I want to get the same xml as output with the name sorted. Eg.
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="sort1.xsl"?>
<levelone>
<child ID="2" sort="1">
<name>Adam</name>
</child>
<child ID="1" sort="5">
<name>Paul</name>
</child>
<child ID="3" sort="2">
<name>Will</name>
</child>
</levelone>
Any pointers will be highly appreciated.
- ThanksDon't you want <xsl:sort select="name"/> rather than <xsl:sort select="@sort"/>?
-
Hello,
Is it possible to sort XML in any way in Flash?
For example in MYSQL I can SELECT a column and ORDER BY...
In Flash I need to SELECT an XML child object and sort all
parent items
accordingly.
Thanks,
DavidIt would be great if there was a way in Flash 8 to sort XML
based on a
certain element without duplicating work making an array?
See a simple XML example below:
<?xml version='1.0' encoding='utf-8'?>
<data>
<item>
<title>Apple</title>
<price>7</ price>
</item>
<item>
<title>Carrot</title>
<price>2</ price>
</item>
<item>
<title>Banana</title>
<price>6</ price>
</item>
</data>
If I could sort this by cheapest price, this would give
results -
1. Carrot
2. Banana
3. Apple
Or by Alphabetical order:
1. Apple
2. Banana
3. Carrot
Any suggestions - is this possible with the Flash XML object?
On 15/2/07 16:41, in article
er22gb$rpp$[email protected], "albee"
<[email protected]> wrote:
> Why not organize the XML data into an Array, and then
use Array.sort() or
> Array.sortOn()?
>
> Albee -
I have a java code that reads various xml files, filters cetain elements and writes the results of these files to one large xml file. Problem is, when the files are read into the large xml, all the nodes are sorted alphabetically. I tried looking for an API that's responsible for this but I cant find one. Can anyone please help?? Here's the portion of my code that writes the file:
private static void writeFile(Document doc, ArrayList docIdList,
BufferedWriter output) {
try {
Element loElmt = null;
NodeList loDocumentList = XMLUtil.getNodeList(doc,
"/XML_EXPORT_FILE/DOCUMENT");
for (int i = 0; i < loDocumentList.getLength(); i++) {
Element loDocument = (Element) loDocumentList.item(i);
String lsDocCode = loDocument.getAttribute("DOC");
String lsDocId = loDocument.getAttribute("DOCS");
//System.out.println(lsDocCode + "\t\t" + lsDocId);
// doc identifer is in list
if (isDocInList(docIdList, lsDocCode, lsDocId)) {
String document = XMLUtil.transformToString(loDocument);
System.out.println("Writing " + lsDocCode + "\t\t"
+ lsDocId + " to output file");
output.write(document);
} catch (Exception e) {
e.printStackTrace();
}I have a java code that reads various xml files, filters cetain elements and writes the results of these files to one large xml file. Problem is, when the files are read into the large xml, all the nodes are sorted alphabetically. I tried looking for an API that's responsible for this but I cant find one. Can anyone please help?? Here's the portion of my code that writes the file:
private static void writeFile(Document doc, ArrayList docIdList,
BufferedWriter output) {
try {
Element loElmt = null;
NodeList loDocumentList = XMLUtil.getNodeList(doc,
"/XML_EXPORT_FILE/DOCUMENT");
for (int i = 0; i < loDocumentList.getLength(); i++) {
Element loDocument = (Element) loDocumentList.item(i);
String lsDocCode = loDocument.getAttribute("DOC");
String lsDocId = loDocument.getAttribute("DOCS");
//System.out.println(lsDocCode + "\t\t" + lsDocId);
// doc identifer is in list
if (isDocInList(docIdList, lsDocCode, lsDocId)) {
String document = XMLUtil.transformToString(loDocument);
System.out.println("Writing " + lsDocCode + "\t\t"
+ lsDocId + " to output file");
output.write(document);
} catch (Exception e) {
e.printStackTrace();
} -
Sorting xml content based on decimal numbers
hi,
I would like to proceed a sorting an xml content. So, when the sorting elements are numbers, I must add the dt:dt="number" attribute in the concerned tag.
But when this number are decimal numbers, it seems that the sorting occurs only when the decimal separator is the '.', but it does not work when the decimal separator is the ','.
So is there a solution for sorting decimal numbr with the decimal separator ',' ?
Thanks in advance
ByeTypically the best way is if you know the name of the default table that contains your document. You do the update on the default table as follows
update yourTable t
set object_value = updateXML(...)
where ref(t) = (
select extractValue(res,'/Resource/XMLRef')
from resource_view
where equals_path(res,'/some/path/mydocument.xml') = 1
) -
Hi All,
Below is a sample XML which needs to be sorted based on the "id" attribute of the "key" element.
<?xml version='1.0'?>
<main>
<key id='16066'>
<key id='16061'>
<key id='11'>
<key id='1'>
<key id='2'>
<key id='1001'>
</main>
I am trying to sort the XML using the below function using DOM4J
List xmlList = document.selectNodes("//main/key","@id")
But the List is not sorted based on the number.
I found that this does not happen even through XSLT because
When sorting in default mode, the sorting is performed using alphabetic string comparisons. If numbers are to be sorted according to their numeric value, data-type must be set to "number".
for example : "<xsl:sort data-type="number"/>"
But, how do we do this using DOM4J.
Please advice how to sort the numbers in an XML without using XSLT
Thanks in advance,
RajuWrite your own code that manipulates the DOM.
-
Hi, I didn't want to bury this in the previous thread. The
Doc examples seem to show this process by creating an array in the
code. How do I?:
• Convert the data from an XML file pulled using
HTTPService into an arrayCollection? (I think that's what I'm
supposed to do)
• Sort the data based on chosen field like
• Display the sorted data (or a way to call it)
That's it. ! I think there should be more of this covered in
the docs and definitely an article/quick start ;) Cheers
Macradobeif you don't want to convert the xml dom to your own datastructures, you could use xslt and the sort element:
http://www.w3schools.com/xsl/xsl_sort.asp -
How can I sort below xml on empNo using xsl, I want an output to be in xml format.
<employeeList>
<employee empNo=1000>
<Info >
<fname></fname>
<lname></lname>
</Info>
</employee>
<employee empNo=1001>
<Info >
<fname></fname>
<lname></lname>
</Info>
</employee>
</employeeList>
Can any one tell me the xsl for above.......
Thanks in advanceHi,
Your xml is not well-formed. Attributes have to be enclosed in double quotes.
You can use the following stylesheet.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="xml"></xsl:output>
<xsl:template match="/employeeList">
<employeeList>
<xsl:apply-templates select="employee">
<xsl:sort select="@empNo"/>
</xsl:apply-templates>
</employeeList>
</xsl:template>
<xsl:template match="employee">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet> -
Dynamic Sorting - XML Publsiher
I have read Klaus Fabian blog on this topic.
http://blogs.oracle.com/xmlpublisher/2006/05/26
As per the manual on page 2-81 Oracle applications does not support passing parameters into the templates. Is there any alternative to achieve this same task without using <xsl: parameters ?
eg. I have one column "SORT_COLUMN" it has value as SAL.
I have employee data where I have to sort the data based on the value which is stored in "SORT_COLUMN" field.
so I can populate "ENAME" to "SORT_COLUMN" field and data will be sorted in ename field. ( I can populate SORT_COLUMN value in the query)
Thanks,Hi,
Can you sort the data at XML generation time? Not sure if you're using Reports or Data Definition or PL/SQL ... doesn't matter anyway.
What manual are you referring to? You can pass in a parameter to data definition and template...
Regards,
Gareth
Blog: http://garethroberts.blogspot.com/ -
Hello,
I am trying to learn actionscript 3 for work and have a very
specific project I need it for. I have been searching every
available tutorial have bought a few books to read but I cannot
find out how to do a few simple things.
The main thing I need to do right now is sort an xml list by
the date of an event before I start picking which nodes to display.
(<dt_event_start>) I need the function to be able to sort by
date or by alpha for a different xml list I will be sorting later,
not both at the same time, just one or the other. My code is sparse
right now, all it does is pick events in a certain city and write
them all in one big list to a text box on screen. Later I will have
to separate out each part to make a header for each event and
choose which nodes to display, so i need to sort the xml list
before I start displaying the nodes in separate text boxes within a
larger container repeated with a loop through the list.
included is one xml entry and the as3 code i have so far
Any help would be greatly appreciated and then I can go
looking for the other code I need.This thread was "imported" from the old forums -- most, if not all, code seems to have disappeared. But here are the sort functions I posted:
function sortXML(source:XML, elementName:Object, fieldName:Object,
options:Object = null):XML
// list of elements we're going to sort
var list:XMLList=source.elements("*").(name()==elementName);
// list of elements not included in the sort -
// we place these back into the source node at the end
var excludeList:XMLList=source.elements("*").(name()!=elementName);
list= sortXMLList(list,fieldName,options);
list += excludeList;
source.setChildren(list);
return source;
function sortXMLList(list:XMLList, fieldName:Object, options:Object =
null):XMLList
var arr:Array = new Array();
var ch:XML;
for each(ch in list)
arr.push(ch);
var resultArr:Array = fieldName==null ?
options==null ? arr.sort() :arr.sort(options)
: arr.sortOn(fieldName, options);
var result:XMLList = new XMLList();
for(var i:int=0; i<resultArr.length; i++)
result += resultArr[i];
return result; -
Hi experts,
Consider this XML...
<root>
<aa>111</aa>
<aa>777</aa>
<aa>555</aa>
<aa>832</aa>
<aa>222</aa>
<aa>999</aa>
<aa>200</aa>
<aa>888</aa>
<aa>632</aa>
<aa>444</aa>
<aa>666</aa>
<aa>333</aa>
</root>
how can I get the n-th smallest value of <aa> tags ?
Suppose, how can we get 6th smallest value? (Here it is 632)
Do we have any functions that can sort or may be anything similiar ??
Thanks,
NavinHi Navin,
Use the count rec to get the no of records and Get data to find the value, and write a WHILE loop to find the value.
A= counterc
b= getdata
while( countrec<=1)
b="xyz"
counterecc= countrec-1
end
so this will go to each and every tag and will find the value of XYZ. Try this logic. It will help you Out.
Regards,
Raju -
How would I sort a bunch of XML nodes? I don't necessarily
need to change the existing structure, but I do need to iterate
through in a specific order. In AS2 I would use the childNodes
array and use Array.sort.And here's a attribute sort function from
http://freerpad.blogspot.com/2007/07/more-hierarchical-sorting-e4x-xml-for.html.
var xml:XML =
<root>
<node id="2">alpha</node>
<node id="3">delta</node>
<node id="5">bravo</node>
<node id="0">foxtrot</node>
<node id="1">echo</node>
<node id="4">charlie</node>
</root>
sortXmlAttribute(xml,"id",true, Array.CASEINSENSITIVE);
trace(xml)
function sortXmlAttribute
( avXml :XML,
avAttributeName :String,
avPutEmptiesAtBottom :Boolean,
avArraySortArgument0 :* = 0,
avArraySortArgument1 :* = 0 )
:void
var lvChildrenCount:int
= avXml.children().length();
if( lvChildrenCount == 0 )
return;
if( lvChildrenCount > 1 )
var lvAttributeValue :String;
var lvXml :XML;
var lvSortOptions:int
= avArraySortArgument0 is Function
? avArraySortArgument1 : avArraySortArgument0;
var lvSortCaseInsensitive:Boolean
= ( lvSortOptions & Array.CASEINSENSITIVE )
== Array.CASEINSENSITIVE;
var lvArray:Array = new Array();
for each( lvXml in avXml.children() )
lvAttributeValue
= lvXml.attribute( avAttributeName );
if( lvSortCaseInsensitive )
lvAttributeValue
= lvAttributeValue.toUpperCase();
if( lvArray.indexOf( lvAttributeValue ) == -1 )
lvArray.push( lvAttributeValue );
} // for each
if( lvArray.length > 1 )
lvArray.sort
avArraySortArgument0,
avArraySortArgument1
if( avPutEmptiesAtBottom )
if( lvArray[0] == "" )
lvArray.push( lvArray.shift() );
} // if
} // if
var lvXmlList:XMLList = new XMLList();
for each( lvAttributeValue in lvArray )
for each( lvXml in avXml.children() )
var lvXmlAttributeValue:String
= lvXml.attribute( avAttributeName );
if( lvSortCaseInsensitive )
lvXmlAttributeValue
= lvXmlAttributeValue.toUpperCase();
if( lvXmlAttributeValue == lvAttributeValue )
lvXmlList += lvXml;
} // for each
} // for each
avXml.setChildren( lvXmlList );
} // if
for each( var lvXmlChild:XML in avXml.children() )
sortXmlAttribute
lvXmlChild,
avAttributeName,
avPutEmptiesAtBottom,
avArraySortArgument0,
avArraySortArgument1
} // for each
} // sortXmlAttribute -
Sorting xml with the dom parser
Hello,
Does the DOM parser have any inbuilt soring methods?I'm not sure what you are looking for. XSL has sort keys on xsl:apply-templates or xsl:for-each
Paul
www.codetelligence.com -
I want to find the XML entry with the highest votes. then
append that <song> to another file. Every 2 Minutes.
my xml is
<?xml version="1.0" encoding="utf-8"?>
<files>
<song>
<songID>1</songID>
<name>Is This It</name>
<artist>The Strokes</artist>
<album>This is It</album>
<genre>Unknown</genre>
<bitrate>224 </bitrate>
<length>2:35 </length>
<filename>01 Is This It.mp3</filename>
<votes>0</votes>
<played>0</played>
</song>
obviously this needs to be done with a timer in
actionscript.I want to find the XML entry with the highest votes. then
append that <song> to another file. Every 2 Minutes.
my xml is
<?xml version="1.0" encoding="utf-8"?>
<files>
<song>
<songID>1</songID>
<name>Is This It</name>
<artist>The Strokes</artist>
<album>This is It</album>
<genre>Unknown</genre>
<bitrate>224 </bitrate>
<length>2:35 </length>
<filename>01 Is This It.mp3</filename>
<votes>0</votes>
<played>0</played>
</song>
obviously this needs to be done with a timer in
actionscript. -
What is the best way to sort an XML document in WLI 8.1?
I have the schema for the XML and I want to sort the data. What is the best way to do this?
I'm not sure if you want the result of the sorting to be a sorted XML document or a sorted object tree but anyway both XSLT and XQuery support sorting so you could use a Transformation control to execute an XQuery or XSLT transformation to sort the data. Another option (if you are not comfortable writing XQuery or XSLT) would be to use XmlBeans and programmatically sort the data. A word of warning though, these processes are quite memory intensive and if the XML document you are sorting is large (say over 10 MB) your server might run out of memory. In this case you might be better of just parsing the document with SAX and then sorting the result objects.
Maybe you are looking for
-
Print a custom Error message in the XML bursting program's log file...
Hi, I having this requirement, where i need to print a custom error message in the xml bursting program's log file. Actually i am having a report where i create invoices and then those invoices are emailed to the respective customers, now say if a cu
-
Error/Scheduled messages in idx5
Hello, I have idoc to mdm scenari, thru XI. The idoc gets successfully posted to XI. In XI, when i monitor sxmb_moni, I see 3 successful messages (one sender, 3 receiver). this is OK. when i monitor, idx5 in XI, I see the 3 messages successful. and 3
-
hi! i dont know what is jsf and where use it what different from JSTL thanks...
-
Hi All I have more than one product group produced on one production line and we want do and see the capacity analysis for all the product groups at the same time to get the overview capacity analysis for the line. How to see it in one transaction
-
Several newspaper websites now download without the usual graphics and just text and also without some pictures, other sites are ok, and all sites are fine in Explorer. I have downloaded an update to Firefox but this has not solved the problem. I did