Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 20th, 2016, 01:01 PM
Registered User
 
Join Date: May 2013
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML Sorting

Really new to XSLT but have managed to put together some code that combines multiple xml files into one. I now want to sort the result but am having some difficulty. I have searched several sites but not found what I need, or one I understand. My source xml file looks like below. As you see it is tv programming. I am trying to sort based on the start value putting the in chronological order starting with earliest to latest. Of course each programme element would have to move with it's respective times. I can post my xslt used to create the xml but see no reason since the sort will be made on the final put shown below and save to new file. Thanks in advance.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<tv>
  <start value="201603182100">
    <programme>
      <id>I396.20453.schedulesdirect.org</id>
      <title>Shark Tank</title>
      <description>A product that keeps beer cold longer; a new twist to the vending machine; pet-safe bug repellents; vegetable smoothies; an update on Wombi Rose and LovePop.</description>
    </programme>
    <programme>
      <id>I265.10035.schedulesdirect.org</id>
      <title>60 Days In: Time Out</title>
      <description>A young man from a rough neighborhood witnesses a violent fight between inmates; the inmates become suspicious of Robert; Zac makes a mistake.</description>
    </programme>
 <start value="201603181900">
    <programme>
      <id>I254.59337.schedulesdirect.org</id>
      <title>The Bourne Identity</title>
      <description>A woman (Franka Potente) helps an amnesiac (Matt Damon), who has a dangerous past, to dodge assassins as he tries to learn about himself.</description>
    </programme>
    <programme>
      <id>I12.11775.schedulesdirect.org</id>
      <title>2016 NCAA Basketball Tournament</title>
      <description>Coach Bob Huggins led the No. 3 seed West Virginia Mountaineers to a second-place finish in the difficult Big 12. They take on Stephen F. Austin State in the first round. The Lumberjacks earned an automatic bid by winning the Southland Conference.</description>
    </programme>
 
Old March 20th, 2016, 02:52 PM
Registered User
 
Join Date: May 2013
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default XLS To Sort

Here is what I came up with:

Code:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>

<xsl:key name="startkey" match="programme" use="@start" />

  <xsl:template match="tv">
    <xsl:copy>
      <xsl:for-each select="programme[generate-id()= generate-id(key('startkey',@start)[1])]">
        <xsl:sort select="@start"/>
        <start>
          <xsl:attribute name="value"><xsl:value-of select="substring(@start,1,12)"/></xsl:attribute>   
          <xsl:for-each select="key('startkey',@start)">
            
            <xsl:copy>
            <id><xsl:value-of select="@channel"/></id>
              <title><xsl:value-of select="title"/></title>
              <description><xsl:value-of select="desc"/></description>
             </xsl:copy>
            
          </xsl:for-each>
        </start>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>    

</xsl:transform>
 
Old March 20th, 2016, 05:18 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I'm a little confused, because you describe the XML that you have shown as the source XML, but your logic appears to be taking programme elements that each have a start attribute, and grouping those that have the same start time to create a start element for that start time - in other words it looks to me as if the XML you have shown is the output rather than the input of your transformation. In which case, congratulations, you have discovered the Muenchian grouping method which is one of the trickiest things to program in XSLT 1.0. It's much easier, however, in XSLT 2.0, which has an xsl:for-each-group instruction. There are still some environments where no XSLT 2.0 processor is available but you should be using XSLT 2.0 if you possibly can.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 21st, 2016, 07:36 AM
Registered User
 
Join Date: May 2013
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry for the confusion, you are correct this is the output . When I originally posted I was thinking of creating the sort as a third transformation, so the xml shown would have been the output for the second but the source for a third. After figuring out the sort I was able to place it in the second transformation which I posted. So, now the code posted sorts the information based on the start value and keeps the programme info grouped.
Thanks for looking.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting elements accessed via apply-templates based on the real XML tag name in XSLT azikh XSLT 2 August 29th, 2013 07:56 PM
Sorting XML data in a dropdown list on asp.net Hughesie78 XML 4 November 27th, 2007 11:50 AM
Sorting XML in a dropdown list Hughesie78 ADO.NET 1 November 23rd, 2007 08:09 AM
sorting xml (database output) Kabe XML 2 September 21st, 2005 03:17 AM
XML to HTML Table with Sorting and Distinct VictorMk XSLT 4 April 23rd, 2004 06:29 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.