Sorting in XML

I have the following xml fragment:
<DATA>
<DATA_ITEM>REG_UP_PCT</DATA_ITEM>
<INTERVAL_NUM>1</INTERVAL_NUM>
</DATA>
<DATA>
<DATA_ITEM>REG_UP_PCT</DATA_ITEM>
<INTERVAL_NUM>2</INTERVAL_NUM>
</DATA>
<DATA>
<DATA_ITEM>REG_DOWN_PCT</DATA_ITEM>
<INTERVAL_NUM>1</INTERVAL_NUM>
</DATA>
<DATA>
<DATA_ITEM>REG_DOWN_PCT</DATA_ITEM>
<INTERVAL_NUM>2</INTERVAL_NUM>
</DATA>
I want to sort this by interval_num so that the result is as follows:
<DATA>
<DATA_ITEM>REG_UP_PCT</DATA_ITEM>
<INTERVAL_NUM>1</INTERVAL_NUM>
</DATA>
<DATA>
<DATA_ITEM>REG_DOWN_PCT</DATA_ITEM>
<INTERVAL_NUM>1</INTERVAL_NUM>
</DATA>
<DATA>
<DATA_ITEM>REG_UP_PCT</DATA_ITEM>
<INTERVAL_NUM>2</INTERVAL_NUM>
</DATA>
<DATA>
<DATA_ITEM>REG_DOWN_PCT</DATA_ITEM>
<INTERVAL_NUM>2</INTERVAL_NUM>
</DATA>
Currently I am using a stylesheet with order by clause as follows:
<xsl:template match="/">
<xsl:apply-templates select="//DATA" order-by="+INTERVAL_NUM"/>
</xsl:template>
This xsl code sorts by interval_num considering interval_num as character instead of number.How can I sort numberwise.
Help will be appreciated?
null

Hi !
If you want to show that you want to sort values numerically, add the optional data-type="number" attribute to your <xsl:sort> element.
for eg.
<xsl:sort select=interval_num" data-type="number"/>
Please try this. I hope this will solve your problem.
....Madhuri
<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by Pankaj Khanna ([email protected]):
I have the following xml fragment:
<DATA>
<DATA_ITEM>REG_UP_PCT</DATA_ITEM>
<INTERVAL_NUM>1</INTERVAL_NUM>
</DATA>
<DATA>
<DATA_ITEM>REG_UP_PCT</DATA_ITEM>
<INTERVAL_NUM>2</INTERVAL_NUM>
</DATA>
<DATA>
<DATA_ITEM>REG_DOWN_PCT</DATA_ITEM>
<INTERVAL_NUM>1</INTERVAL_NUM>
</DATA>
<DATA>
<DATA_ITEM>REG_DOWN_PCT</DATA_ITEM>
<INTERVAL_NUM>2</INTERVAL_NUM>
</DATA>
I want to sort this by interval_num so that the result is as follows:
<DATA>
<DATA_ITEM>REG_UP_PCT</DATA_ITEM>
<INTERVAL_NUM>1</INTERVAL_NUM>
</DATA>
<DATA>
<DATA_ITEM>REG_DOWN_PCT</DATA_ITEM>
<INTERVAL_NUM>1</INTERVAL_NUM>
</DATA>
<DATA>
<DATA_ITEM>REG_UP_PCT</DATA_ITEM>
<INTERVAL_NUM>2</INTERVAL_NUM>
</DATA>
<DATA>
<DATA_ITEM>REG_DOWN_PCT</DATA_ITEM>
<INTERVAL_NUM>2</INTERVAL_NUM>
</DATA>
Currently I am using a stylesheet with order by clause as follows:
<xsl:template match="/">
<br/><xsl:apply-templates select="//DATA" order-by="+INTERVAL_NUM"/>
</xsl:template>
This xsl code sorts by interval_num considering interval_num as character instead of number.How can I sort numberwise.
Help will be appreciated?<HR></BLOCKQUOTE>
null

Similar Messages

  • How to use XmlModify to sort the XML Data?

    Hello,
    I saw some examples explain how to use XmlModify in BDB XML package. I want to sort the XML data by several elements but my Java program could not work correctly.
    <CustomerData>
    <Transaction>
    <DLSFIELDS>
    <ADR_PST_CD>12345</ADR_PST_CD>
    <CLT_IRD_NBR>002</CLT_IRD_NBR>
    </DLSFIELDS>
    </Transaction>
    <Transaction>
    <DLSFIELDS>
    <ADR_PST_CD>12345</ADR_PST_CD>
    <CLT_IRD_NBR>102</CLT_IRD_NBR>
    </DLSFIELDS>
    </Transaction>
    // many nodes like transaction ...
    </CustomerData>
    My XQuery script was executed successfully in the shell. The script looks as follows:
    "for $i in collection('sample.dbxml')/CustomerData/Transaction order by xs:decimal($i//ADR_PST_CD), xs:decimal($i//CLT_IRD_NBR) return $i".
    The Java code :
    // create XmlManager
    XmlManager manager = // ...;
    // open XmlContainer
    XmlContainer container = // ...;
    XmlQueryContext context = manager.createQueryContext(XmlQueryContext.LiveValues, XmlQueryContext.Eager);
    XmlQueryExpression expression = manager.prepare("for $i in collection('sample.dbxml')/CustomerData/Transaction order by xs:decimal($i//ADR_PST_CD),xs:decimal($i//CLT_IRD_NBR) return $i", context);
    XmlModify modify = manager.createModify();
    XmlUpdateContext uc = manager.createUpdateContext();
    XmlDocument xmldoc = container.getDocument("sample.xml");
    XmlValue value = new XmlValue(xmldoc);
    long numMod = modify.execute(value, context, uc);
    System.out.println("Peformed " + numMod     + " modification operations");
    Could you point out the errors above or offer some suggestion?
    Thanks.

    I have other question of the sorting issue. Here are a large XML need to sort so I have to split it to multiple small XML files. After importing these files, I will use the XmlModify and XQuery to sort them. I'm not clear on the multiple XML files processing.
    1. Can the BDB XML ensure that all these XML files were sorted or how to update all documents with same logic.
    2. If I want export all these sorted documents, how can I ensure these files processed in sequence? Which document needs process first?
    The export method:
    public void export(String outputfile)throws Exception{
    final int BLOCK_SIZE = 5 * 1024 * 1024; // 5Mb
    try{
    File theFile = new File(outputfile);
    FileOutputStream fos = new FileOutputStream(theFile);
    byte[] buff= new byte[BLOCK_SIZE];                         
    XmlResults rs = container.getAllDocuments(new XmlDocumentConfig());               
    while(rs.hasNext()){
         XmlDocument xmlDoc = rs.next().asDocument();
         XmlInputStream inputStream = xmlDoc.getContentAsXmlInputStream();                    
         long read=0;
         while(true){
         read = inputStream.readBytes(buff, BLOCK_SIZE);
    fos.write(buff,0,(int)read);                    
         if(read < BLOCK_SIZE) break;
    inputStream.delete();
    xmlDoc.delete();
    rs.delete();
    //MUST CLOSE!
    fos.close();               
    catch(Exception e){
    System.err.println("Error exporting file from container " + container);
    System.err.println(" Message: " + e.getMessage());
    Thanks.

  • Sort generic XML document hierarchically ?

    Hi
    I need to sort entire xml alphabetically by node names.
    I.e. alphabetic sort by node names on first level, then alphabetic sort
    each node's children (second level), etc.
    The result will be same tree, but sorted alphabetically.
    I do not know names and structure beforehand, this is abstract XML document.
    Sorting can be applied either to text form , or DOM form (Document).
    Any Ideas ?
    Thanks !

    My idea would be to use the xsl:sort element ofXSLT.
    Thanks , but I can't figure out how to define such
    generic xsl:sort rule.
    All examples of this rule refers to tags by their
    name. And in my case tag name itself should be
    subject for sorting.Wouldn't that just be <xsl:sort select="name()">?
    In addition, this sorting should
    be performed on all levels of document , and depth is
    not known beforehand.So what? Put that in a template that's applied to every element. And make sure you call <xsl:apply-templates> in that template so it gets applied at all levels.

  • URGENT:Sorting in XML Publisher

    Hi,
    i am trying to get the section data into a variable using the below.
    <?variable@incontect:hg;xdoxslt:distinct_values(/SAS_DATA/LIST_WEEKLY/WEEKLY/TYPE)?> , can i do something here so that i get the TYPE values in the SORT ascending.below is the sample data.
    <SAS_DATA>
    <LIST_WEEKLY>
    <WEEKLY>
    <BUS_GRP>FBC</BUS_GRP>
    <BUS_GRP_SO>1</BUS_GRP_SO>
    <MNGD_FLAG>Fidelity Managed</MNGD_FLAG>
    <DISC_DSC>Long-Term</DISC_DSC>
    <TYPE>Bond</TYPE>
    <SORT_ORDER>2</SORT_ORDER>
    <FLOWS>44.83</FLOWS>
    </WEEKLY>
    </LIST_WEEKLY>
    </SAS_DATA>
    Thanks in advance

    You can't upload the template here; you can post a part of the relevant XML code and template code here.
    or send me a sample XML and RTF template to [email protected] and I will take a look.

  • Sort incoming XML message

    Hi
    Is there a standard way in PI to sort the incoming XML message within the message mapping before doing the mapping itself?
    The scenario would be an incoming Flat file with header and item data records, and we will be mapping it in PI to create IDOC messages based on the same Header segment. So for the same Header Data I would like to create a single IDOC. But the incoming file would be unsorted, so I would need to sort it first based on the header segment some way.
    Ideally, we would like to do all of the mapping in the graphical mapping tool and not use a UDF.
    Thanks
    Edited by: MxG on Nov 20, 2008 5:04 PM

    I already tried Sort, but it doesnt work the way I want.
    Say I have inbound XML as
    <hdr1>
    <hdr2>
    <itm1>
    <itm2>
    <itm3>
    it needs to be mapped to an IDOC as
    <hdrsegment>
    <itmsegment>
    I mapped XML header data to IDOC after sorting, and that works fine. The issue comes on assigning the inbound XML Item data to IDOC Item segment. It is creating the correct Number of segments, but when assiging the fields to IDOC fields, it is using unsorted data ie the data as received, not the sorted one which I did for IDOC Node, and for Header fields.
    What I really want is to sort the data once, and then use that in the mapping.

  • Sorting by XML tag

    I am using Oracle 8i for storing XML files in a CLOB column and that column is intermedia text indexed. Is there any way to sort a query output based on an XML tag within that CLOB column. for eg., <price>10.22</price> is an XML tag, can i sort on price

    I don't think you'll be able to use interMedia text directly to sort the query output, but you should be able to use dbms_lob.instr and dbms_lob.substr to look for a specific tag and then sort on the contents of the tag. It may not be pretty, but you could do something like:
    select fields
    from xmltable
    where <where clause>
    order by
    nvl(dbms_lob.substr(
    clobcolumn,
    dbms_lob.instr(clobcolumn, '</price>') - dbms_lob.instr(clobcolumn, '<price>'),
    dbms_lob.instr(clobcolumn, '<price>')), 0) asc/desc
    You'd have to test it out, but the above should return the peice between the <price> and the </price> tags, or 0 if the tags don't exist.

  • 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.

  • How to sort an xml Based on "property name or node"

    Input
    '<Email>
      <Type>Personal</Type>
      <Code>1</Code>
      <Description>[email protected]</Description>
      <LocalId>000000000000000000000000868000000001</LocalId>  
    </Email>'
    OUPUT
    '<Email>  
      <Code>1</Code>
      <Description>[email protected]</Description>
      <LocalId>000000000000000000000000868000000001</LocalId>  
      <Type>Personal</Type>
    </Email>'
    how do this using sql ? 

    DECLARE @xml as xml
    SET @xml ='<Email>
      <Type>Personal</Type>
      <Code>1</Code>
      <Description>[email protected]</Description>
      <LocalId>000000000000000000000000868000000001</LocalId>  
        <Infeliz>
    <Za>
    Fuck
    </Za>
    <Maria>
    Fuck
    </Maria>
      </Infeliz>
    </Email>'
    SELECT '<Email>' 
    + CONVERT(varchar(1000), @xml.query('for $x in (/Email/*) 
      order by local-name($x) 
                       return $x '))  
           + '</Email>'
    OUPUT - NOT OK 
    <?xml version="1.0" encoding="UTF-8"?>
    <Email>
    <Code>1</Code>
    <Description>[email protected]</Description>
    <Infeliz>
    <Za>Fuck</Za>
    <Maria>Fuck</Maria>
    </Infeliz>
    <LocalId>000000000000000000000000868000000001</LocalId>
    <Type>Personal</Type>
    </Email>
    I want so
    <?xml version="1.0" encoding="UTF-8"?>
    <Email>
    <Code>1</Code>
    <Description>[email protected]</Description>
    <Infeliz>
    <Maria>Fuck</Maria>
    <Za>Fuck</Za>
    </Infeliz>
    <LocalId>000000000000000000000000868000000001</LocalId>
    <Type>Personal</Type>
    </Email>

  • 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
    Bye

    Typically 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
                          )

  • Sorting XML using DOM4J

    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,
    Raju

    Write your own code that manipulates the DOM.

  • Sorting xml document with xsl

    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 advance

    Hi,
    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>

  • Sort Xml list

    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;

  • Sorting Order of XML Deposit Advices and Checks

    Does anyone know how we can customize the sorting of XML Deposit Advices and Checks ? At present it default sorts on Organization,Person. we would like to sort on Location,Person.
    Any help will be appreceated.
    Regards,

    1. Try to do it in db itself.
    2. If that is not possible, then specify the sort order in RTF like
    <?for-each:GROUP?>
    <?sort:SEGMENT1;'ascending';data-type='number'?> -- Not sure if its Varchar or Number. Change the data type accordingly
    <?sort:LAST_NAME;'ascending';data-type='text'?>
    <?sort:FIRST_NAME;'ascending';data-type='text'?>
    Do check if the performance is not affected by this.

  • XML: sorting XML nodes

    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

  • Regarding sales order not created in xml file format

    Hi Friends,
                       I have the xml file,I sort the xml file alphabetically on material master codes, WHen program try to create the sales order by using this xml file through call transaction method, the line item numbers does not matches with the line items of xml file.
    I want same sequence.Please suggest me..
    Best Regards,
    SHialpa.

    HI Divya,
    Have u uninstalled Acrobt Reader from your system???
    Regards
    Manoj S

Maybe you are looking for

  • HP Officejet Pro 8000 Wireless - won't print an iTunes Playlist.

    No matter what I do, my new OJ Pro 8000 Wireless will not print an iTunes Playlist. I googled this and found that there is at least one other HP model with the same problem and no known solution. That really blows, considering how ubiquitous iTunes i

  • Pages converts hyperlinks to local source in .pdf

    We use Pages '09 v4.0.5 in page layout mode to do a monthly newsletter for our MUG. Something I am doing, periodically and not always, causes a hyperlink to become referenced or linked erroneously to my local machine. Even if everything in the docume

  • How to Maintain Translation of texts of Exception Class

    Hi Experts, I have an exception class ZCX_CHECK . I have maintained a text in the text tab of the exception class like No components  Exists in  Package  &PACKAGE_NAME& PACKAGE_NAME is the Attribute . How do I maintain Translation of the TEXT. When I

  • Field OBJNRZ in Tables FMIFIIT and FMIOI

    Hi all, I need fill the field OBJNRZ in tables Tables FMIFIIT and FMIOI but I dont know if the User exit EXIT_SAPLFMRI_001 and EXIT_SAPLFMRI_001 need abap code for to be filled (OBJNRZ) or only is needed activate this user exit. Regards, Alberto

  • I can´t download ios 5.0.1 in my iphone 4, it takes a lot of time

    I can´t download IOS 5.0.1 in my iphone 4, it takes a lot of time. I´ve tried using itunes 10.5 in my pc