SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XSL How To Sort? and Output As XML

    Can any body help an XML newbie

    I have the following XML File:

    <markers>
    <book ID="War of the Worlds">
    <highlights>
    <highlight>
    <start>2001</start>
    <end>2005</start>
    </highlight>
    <highlight>
    <start>2380</start>
    <end>2410</start>
    </highlight>
    <highlight>
    <start>10</start>
    <end>32</end>
    </highlight>
    </highlights>
    <book ID="Help">
    <highlights>
    <highlight>
    <start>12</start>
    <end>30</end>
    </highlight>
    <highlight>
    <start>2></start>
    <end>6</end>
    </highlight>
    </highlights>
    </highlights>
    </markers>

    Can any body show me how to sort the start element in ascending order using XSL and output as XML, all the example's I have seen output as html, when I need xml output.


    Many Thanks

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Sorting:
    HTML Code:
    <xsl:for-each select="highlight">
    <xsl:sort select="start"
    data-type="number"
    order="descending"/>
    <xsl:value-of select="start"/>
    </xsl:for-each>
    Output as XML:
    Code:
    <xsl:stylesheet...>
    <xsl:output method="xml" version="1.0"
    encoding="utf-8"/>
    <xsl:template...>
    
    </xsl:template>
    </xsl:stylesheet>

  3. #3
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried the following xsl stylesheet and it still not working

    <xsl:stylesheet version="1.0" xmlnssl="http://www.w3.org/1999/xsl/transform">
    <xslutput method="xml" version="1.0"
    encoding="utf-8"/>
    <xsl:template match="markers/book/highlights">


    <xsl:for-each select="highlight">
    <xsl:sort select="start"
    data-type="number"
    order="ascending"/>
    <xsl:value-of select="start"/>
    </xsl:for-each>


    </xsl:template>
    </xsl:stylesheet>

    Can somebody show me where I'm going wrong?

  4. #4
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For starters, your XML file isn't well formed. For example:

    Code:
    <start>2001</start>
    <end>2005</start>
    Run your XML through a feed validator first, then worry about your XSL.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  5. #5
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, that was just a quick and dirty post of xml the actual xml is here:

    <?xml-stylesheet type="xml" href="sort.xsl"?>
    <markers>
    <book ID="War of the Worlds">
    <highlights>
    <highlight>
    <start>2001</start>
    <end>2005</end>
    </highlight>
    <highlight>
    <start>2380</start>
    <end>2410</end>
    </highlight>
    <highlight>
    <start>10</start>
    <end>32</end>
    </highlight>
    </highlights>
    </book>
    <book ID="Help">
    <highlights>
    <highlight>
    <start>12</start>
    <end>30</end>
    </highlight>
    <highlight>
    <start>2</start>
    <end>6</end>
    </highlight>
    </highlights>
    </book>
    </markers>

  6. #6
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,526
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I managed to get something working last night but then I got too tired to try and figure out just what the differences were between your code and mine. However, I did note that one thing in particular seemed to make a difference. The top line of your XML file should read something like this:

    Code:
    <?xml-stylesheet type="text/xsl" href="sort.xsl"?>
    The following is what I used for the stylesheet:

    Code:
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0"
    encoding="utf-8"/>
    <xsl:template match="markers/book/highlights">
    <xsl:for-each select="highlight">
    <xsl:sort select="start" data-type="number" order="ascending"/>
    <xsl:value-of select="start"/><br />
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>
    One thing that I find odd though is that it doesn't appear to sort those numbers in ascending order, which I found very odd. Perhaps someone could explain just how this type of sort works?
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •