sort content based on the date created meta data

matthew.wren's Avatar

matthew.wren

05 Jan, 2015 02:31 PM

I created an XML output of a folder of links to articles and each one contains a <created-on> attribute.

http://www.scranton.edu/news/royalnews/assets/in-the-news/royals-making-headlines1.xml

We currently use a folder to display the 5 most recent "in the news" article links in our news website but we are trying to add this to a mobile application and we'd like to sort it. Each article has this meta data so I was wondering if there's a way to use this information to sort these links.

  1. 1 Posted by Ryan Griffith on 05 Jan, 2015 02:49 PM

    Ryan Griffith's Avatar

    Hi Matthew,

    If you are using an XSLT Format, you can sort by using <xsl:sort> and selecting the created-on element as the node to sort on. More information on sorting can be found here.

    For example, your Format could have something like the following:

    <xsl:apply-templates select="system-symlink">
        <xsl:sort select="created-on" order="ascending" />
    </xsl:apply-templates>
    

    If you are using Velocity, you can sort by XML elements using the Sort Tool. So your Format could have something like the following:

    #set($links = $_XPathTool.selectNodes($contentRoot, '//system-symlink'))  
    $_SortTool.addSortCriterion("created-on", "", "number", "ascending", "upper-first")  
    $_SortTool.sort($links)
    

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by matthew.wren on 05 Jan, 2015 02:56 PM

    matthew.wren's Avatar

    I currently do not have a format in place so if I wanted to use either one of these could I just created a format and paste one of these in or would I have to do something else to utilize the sort function?

  3. 3 Posted by Ryan Griffith on 05 Jan, 2015 03:26 PM

    Ryan Griffith's Avatar

    Hi Matthew,

    Both examples should work out of the box, but you will still need to loop over the sorted links and output what you need.

    Additionally, for the XSLT sample, you would need to add an <xsl:template> that matches the appropriate elements.

    Please let me know if you have any questions.

    Thanks!

  4. 4 Posted by matthew.wren on 05 Jan, 2015 03:32 PM

    matthew.wren's Avatar

    I went with the Velocity because I like how succinct it is but the output it generates (as is) is just blank xml

    <xml></xml>

    Another question I have is what kind of variable is that within the <created-on> data? It seems like a string of random numbers but is it some sort of time/date/year numbering system?

  5. 5 Posted by Ryan Griffith on 05 Jan, 2015 08:24 PM

    Ryan Griffith's Avatar

    Hi Matthew,

    When you have a moment, please attach your Velocity Format so we can take a closer look at were you are at.

    Another question I have is what kind of variable is that within the data? It seems like a string of random numbers but is it some sort of time/date/year numbering system?

    The integer values for metadata such as created-on, last-published, etc are unix timestamps. These timestamps contain milliseconds, which is why there are 13 digits as opposed to the 10 you may encounter elsewhere like in PHP.

    Please let me know if you have any questions.

    Thanks!

  6. 6 Posted by matthew.wren on 05 Jan, 2015 08:43 PM

    matthew.wren's Avatar

    my format is just what you provided to me earlier so it's

    #set($links = $_XPathTool.selectNodes($contentRoot, '//system-symlink'))  
    $_SortTool.addSortCriterion("created-on", "", "number", "ascending", "upper-first")  
    $_SortTool.sort($links)
    

    Oh okay great, that's good to know!

  7. 7 Posted by Ryan Griffith on 05 Jan, 2015 08:47 PM

    Ryan Griffith's Avatar

    Hi Matthew,

    As I mentioned previously, my sample only does the sorting and you will still need to loop over the sorted links and output what you need. For example:

    #set($links = $_XPathTool.selectNodes($contentRoot, '//system-symlink'))  
    $_SortTool.addSortCriterion("created-on", "", "number", "ascending", "upper-first")  
    $_SortTool.sort($links)
    
    #if ($links.size() > 0)
        #foreach ($link in $links)
            ### ... Do something with the `<system-symlink>` Element ...
        #end
    #end
    

    Please let me know if you have any questions.

    Thanks!

  8. Ryan Griffith closed this discussion on 09 Jan, 2015 05:04 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac