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 October 6th, 2008, 02:29 PM
Authorized User
 
Join Date: Oct 2008
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
Default XSL group/sort by date problem

I've been making attempts to sort/group via the Muenchian Method, but apparently I'm doing it wrong. I can't figure out what it is that I'm screwing up, and I've been working on this for days.

I'm trying to pull items out of a huge xml by their program id's, group by the document date, sort on this date, list the date, and then print all the relevant docs under that in alternating line colors.

Can anyone help me out? Thanks.


Sample part of XML


Code:
<item category="meeting">
    <program id="prog1">program 1</program>
    <docdate>20071026</docdate>
    <title>"Welcome to Historic Downtown"</title>
    <desc>"Welcome to Historic Downtown"</desc>
    <xref>ftp://our.ftp.site/anftpdoc/historic.pdf</xref>
</item>
<item category="agenda">
    <program id="prog1">program 1</program>
    <program id="prog2">program 2</program>
    <docdate>20070718</docdate>
    <title>agenda</title>
    <desc>agenda for July 18, 2007 Meeting</desc>
    <xref>ftp://our.ftp.site/anftpdoc/agenda071807.pdf</xref>
</item>
Sample XSL:

Code:
<xsl:key name="sortdate" match="item" use="docdate"/>

<xsl:template match="/" name="groups">

    <xsl:for-each select="//downloads/item[@category='meeting' or @category='agenda' or @category='summary']/program[@id='prog1'][generate-id(.)=generate-id(key('sortdate', ../docdate)[1])]">
        <xsl:sort select="../docdate" order="ascending" data-type="number" />
        <xsl:element name="h4">        
        <xsl:value-of select="../docdate" />
    </xsl:element>
        <xsl:for-each select="key('sortdate', ../docdate)">
            <xsl:call-template name="populate" /> 
        </xsl:for-each>
    </xsl:for-each>

</xsl:template>

<xsl:template name="populate">
    <xsl:if test="position()  mod 2 = 1">
        <xsl:element name="tr">
        <xsl:attribute name="style">background-color: #fff; border: 0</xsl:attribute>
            <xsl:element name="td">
                <xsl:element name="a">
                <xsl:attribute name="href"><xsl:value-of select="../xref" /></xsl:attribute>
                    <xsl:value-of select="../title" />
                </xsl:element>
            </xsl:element> 
        </xsl:element>
    </xsl:if>
    <xsl:if test="position()  mod 2 = 0">
        <xsl:element name="tr">
        <xsl:attribute name="style">background-color: #efe8df; border: 0</xsl:attribute>
            <xsl:element name="td">
                <xsl:element name="a">
<xsl:attribute name="href"><xsl:value-of select="../xref" /></xsl:attribute>
<xsl:value-of select="../title" />
                </xsl:element>
            </xsl:element> 
        </xsl:element>
    </xsl:if>
</xsl:template>
 
Old October 6th, 2008, 02:45 PM
Authorized User
 
Join Date: Nov 2004
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jkmyoung
Default

Here: generate-id(.) You're doing this while in the program node.
Change it to generate-id(..)

 
Old October 6th, 2008, 03:10 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>select="//downloads/item[....]/program[@id='prog1'][generate-id(.)=generate-id(key('sortdate', ../docdate)[1]

key('sortdate', XXXX) is always going to be an <item> element, whereas generate-id(.) is always going to be the id of a <program> element, so the two ids will never be equal.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old October 6th, 2008, 03:52 PM
Authorized User
 
Join Date: Oct 2008
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
Default

(too many effing nodes...)

Cool. Thanks...you guys are great.

I have another problem now, though. I'm basically populating a downloads section with general information, forms, meeting information (which is grouped and sorted by date), and reports. The meeting information call, which you guys just helped with is the 3rd item down in a series of 'if' statements. Now that that the meetings part is fixed, the earlier and later 'if' calls don't work. They had worked prior to this fix. Why is this section ignoring the earlier info?

Code:
<xsl:if test="//downloads/item[@category='general']/program[@id='prog1'] or //downloads/item[@category='brochure']/program[@id='prog1']">
    <xsl:element name="h3">
    <xsl:attribute name="class">sidebar</xsl:attribute>    
           General Information
    </xsl:element>            
    <xsl:element name="table">
    <xsl:attribute name="style">margin: -8px 0 10px 0; padding: 0; border: 0; font: 500 'Trebuchet MS', helvetica, sans-serif; width:98%</xsl:attribute>
        <xsl:for-each select="//downloads/item[@category='general' or @category='brochure']/program[@id='prog1']">
            <xsl:call-template name="populate" />
        </xsl:for-each>    
    </xsl:element>
</xsl:if>

(other various "IF" statments)

<xsl:if test="//downloads/item[@category='meeting' or @category='agenda' or @category='summary']/program[@id='prog1']">
       <xsl:element name="h3">
       <xsl:attribute name="class">sidebar</xsl:attribute>    
       Meeting Information
       </xsl:element>
    <xsl:element name="table">
    <xsl:attribute name="style">margin: -8px 0 10px 0; padding: 0; border: 0; font: 500 'Trebuchet MS', helvetica, sans-serif; width:98%</xsl:attribute>
        <xsl:call-template name="groups"/> **THIS CALLS THE TEMPLATE YOU JUST HELPED ME WITH**            
    </xsl:element>

</xsl:if>

(some more various "IF" statments)
 
Old October 7th, 2008, 09:35 AM
Authorized User
 
Join Date: Oct 2008
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
Default

OK. Got it all together now. Thanks everyone.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Xsl to Sort the following simple xml: Takashi321 XSLT 3 August 27th, 2007 11:54 AM
xsl:sort Question kwilliams XSLT 6 July 18th, 2005 08:39 AM
Unable to sort using xsl sort command sly_jimmy_boy XSLT 3 June 17th, 2005 05:15 AM
XSLT-- xsl:sort help... debo XSLT 2 December 3rd, 2004 04:25 PM
xsl:sort and IE 5.0 Issue babloo81 XSLT 1 March 17th, 2004 06:12 AM





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