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 July 29th, 2003, 07:15 AM
Registered User
 
Join Date: Jul 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default xslt 1.0 distinct grouping

This is the first time I have posted on a forum before, so please bear with me. If what I have included is too much or not enough, please advise.

I am having a problem with xtra bullets coming through on my html page afer I have tried to render only the distinct Month/Year and the articles for each. Please see my xml/xsl below and provide any help possible. Thanks!

----------------------------------------------------------------------
<?xml:stylesheet type='text/xsl' href='xml_object.xsl'?>
<root>
<metadata_object mdo_id="3" mdo_type_cd="WPG" mdo_type_nm="Web Page" mdo_nm="Home">
<metadata md_type="mdi" mdi_type_cd="DESC" mdi_type_nm="Description" mdi_txt="Decision Support Technical Services Intranet Tools Portal Home Page"/>
<metadata md_type="mdi" mdi_type_cd="UPD" mdi_type_nm="Update" mdi_txt="This document was last updated on July 8, 2003."/>
<metadata md_type="mdi" mdi_type_cd="URL" mdi_type_nm="URL" mdi_txt="xml_object.asp?mdo_sp=mdo_id_sp&amp;mdo_i d=3&amp;mdo_rltn_type_cd=PCAT,PWPG&amp;mdi_type_cd =DESC,URL&amp;mdo_xsl=xml_object.xsl"/>
<metadata md_type="mdi" mdi_type_cd="WT" mdi_type_nm="Welcome to" mdi_txt="Welcome to the &lt;b&gt;Decision Support Technical Services (DSTS)&lt;/b&gt; intranet &lt;font color=&quot;#ff9900&quot;&gt;&lt;b&gt;Tools Portal&lt;/b&gt;&lt;/font&gt;, your main intranet source for Anthem&apos;s Decision Support related information.
&lt;br/&gt;"/>
<metadata md_type="mdo" mdo_id="182" whats_new_id="1" mdo_type_cd="NEW" mdo_type_nm="What&apos;s New" mdo_nm="What&apos;s Test Article 1">
<metadata_detail mdi_type_cd="URL" mdi_type_nm="URL" mdi_txt="under_construction.html" mdi_efctv_dt="2003-05-31T00:00:00" mdi_efctv_dt_month="May" mdi_efctv_dt_year="2003"/>
</metadata>
<metadata md_type="mdo" mdo_id="183" whats_new_id="0" mdo_type_cd="NEW" mdo_type_nm="What&apos;s New" mdo_nm="What&apos;s New Article 2">
<metadata_detail mdi_type_cd="URL" mdi_type_nm="URL" mdi_txt="under_construction.html" mdi_efctv_dt="2003-06-01T00:00:00" mdi_efctv_dt_month="June" mdi_efctv_dt_year="2003"/>
</metadata>
<metadata md_type="mdo" mdo_id="185" whats_new_id="0" mdo_type_cd="NEW" mdo_type_nm="What&apos;s New" mdo_nm="What&apos;s New Article 4">
<metadata_detail mdi_type_cd="URL" mdi_type_nm="URL" mdi_txt="under_construction.html" mdi_efctv_dt="2003-06-30T00:00:00" mdi_efctv_dt_month="June" mdi_efctv_dt_year="2003"/>
</metadata>
<metadata md_type="mdo" mdo_id="186" whats_new_id="1" mdo_type_cd="NEW" mdo_type_nm="What&apos;s New" mdo_nm="What&apos;s New Article 5">
<metadata_detail mdi_type_cd="URL" mdi_type_nm="URL" mdi_txt="under_construction.html" mdi_efctv_dt="2003-05-01T00:00:00" mdi_efctv_dt_month="May" mdi_efctv_dt_year="2003"/>
</metadata>
<metadata md_type="mdo" mdo_id="189" whats_new_id="3" mdo_type_cd="NEW" mdo_type_nm="What&apos;s New" mdo_nm="What&apos;s New Article 8">
<metadata_detail mdi_type_cd="URL" mdi_type_nm="URL" mdi_txt="under_construction.html" mdi_efctv_dt="2003-03-31T00:00:00" mdi_efctv_dt_month="March" mdi_efctv_dt_year="2003"/>
</metadata>
<metadata md_type="mdo" mdo_id="190" whats_new_id="2" mdo_type_cd="NEW" mdo_type_nm="What&apos;s New" mdo_nm="What&apos;s New Article 9">
<metadata_detail mdi_type_cd="URL" mdi_type_nm="URL" mdi_txt="under_construction.html" mdi_efctv_dt="2003-04-10T00:00:00" mdi_efctv_dt_month="April" mdi_efctv_dt_year="2003"/>
</metadata>
<metadata md_type="mdo" mdo_id="191" whats_new_id="1" mdo_type_cd="NEW" mdo_type_nm="What&apos;s New" mdo_nm="What&apos;s New Article 10">
<metadata_detail mdi_type_cd="URL" mdi_type_nm="URL" mdi_txt="under_construction.html" mdi_efctv_dt="2003-05-24T00:00:00" mdi_efctv_dt_month="May" mdi_efctv_dt_year="2003"/>
</metadata>
</metadata_object>
</root>

---------------------------------------------------------------------

<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "#160;"> ]>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">

        <head>
            <link rel="stylesheet" href="includes/whitespace_style.css" type="text/css" media="screen"/>
        </head>
        <body topmargin="0" bottommargin="0" rightmargin="0" marginwidth="0" marginheight="0">
            <table cellpadding="0" cellspacing="0" align="center" border="0" bgcolor="#ffffff" width="100%">
                <tr>
                    <td><img src="images/spacer.gif" width="2" height="6"></img></td>
                </tr>
            </table>

            <xsl:apply-templates select="//metadata[@mdi_type_cd='WT']"/>

            <xsl:call-template name="md_objects"/>

            <xsl:call-template name="whats_new"/>

            <xsl:call-template name="whats_new_article"/>

        </body>
    </xsl:template>

    <xsl:template match="//metadata[@mdi_type_cd='WT']">
        <div class="mdi">
            <xsl:value-of select="@mdi_txt" disable-output-escaping="yes"/>
        </div>

    </xsl:template>

    <xsl:template name="whats_new">
        <xsl:choose>
            <xsl:when test="count(//metadata[@mdo_type_cd='NEW']) > 0">
                <div class="new">
                    What's New...
                        <xsl:text disable-output-escaping="yes">
                            <![CDATA[
                            <a href="xml_object.asp?mdo_sp=cassel_ben.whats_new_a rchive_sp&mdo_id=194&mdo_rltn_type_cd=PWPG,ONEW&md i_type_cd=DESC,URL&mdo_xsl=xml_archive.xsl">
                                [archive]
                            </a>
                            ]]>
                        </xsl:text>
                </div>
            </xsl:when>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="md_objects">
        <xsl:for-each select="//metadata[@md_type='mdo'][@mdo_type_cd!='NEW']">
            <xsl:sort select="@mdo_sort_sqnc_nbr"/>
                <div class="mdo">
                    <a target="_self">
                        <xsl:attribute name="href">
                            <xsl:for-each select="metadata_detail[@mdi_type_cd='URL']">
                                <xsl:value-of select="@mdi_txt"/>
                            </xsl:for-each>
                        </xsl:attribute>
                        <xsl:value-of select="@mdo_nm"/>
                    </a>
                </div>
            <xsl:for-each select="metadata_detail[@mdi_type_cd='DESC']">
                <div class="mdi">
                    <xsl:value-of select="@mdi_txt" disable-output-escaping="yes"/>
                </div>
            </xsl:for-each>
            <br/>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="whats_new_article">
        <xsl:for-each select="//metadata[not(@whats_new_id=preceding-sibling::metadata/@whats_new_id)]">
            <xsl:sort select="@whats_new_id"/>
                    <table cellpadding="0" cellspacing="0" align="center" border="0" bgcolor="#ffffff" width="100%">
                        <tr class="mdo">
                            <td colspan="2">
                                <xsl:for-each select="metadata_detail[@mdi_type_cd='URL']">
                                    <xsl:value-of select="@mdi_efctv_dt_month"/>
                                        <xsl:text> </xsl:text>
                                    <xsl:value-of select="@mdi_efctv_dt_year"/>
                                </xsl:for-each>
                                <br/>
                            </td>
                        </tr>
                        <tr>
                            <td width="25">&nbsp;</td>
                            <td class="new">
                                <xsl:for-each select=".|following-sibling::metadata[@whats_new_id=current()/@whats_new_id]">
                                    <xsl:sort select="@mdi_effctv_dt"/>
                                        <li>
                                            <a target="_self">
                                                <xsl:attribute name="href">
                                                    <xsl:for-each select="metadata_detail[@mdi_type_cd='URL']">
                                                        <xsl:value-of select="@mdi_txt"/>
                                                    </xsl:for-each>
                                                </xsl:attribute>
                                                    <xsl:value-of select="@mdo_nm"/>
                                            </a>
                                        </li>
                                    </xsl:for-each>
                                </td>
                            </tr>
                    </table>
            </xsl:for-each>
        </xsl:template>

</xsl:stylesheet>


 
Old July 29th, 2003, 07:51 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Hi,
the information and the code you supplied is quite sufficient to fix the problem :)

The problem is in the XPath expression:
Code:
...
<xsl:for-each select="//metadata[not(@whats_new_id=preceding-sibling::metadata/@whats_new_id)]">
...
This expression selects all "metadata" elements having distinct "whats_new_id" attribute values and also all "metadata" elements which have no attribute "whats_new_id" ; so you just need to add
one predicate before the existing one. XPath expression will be:
Code:
//metadata[@whats_new_id][not(@whats_new_id=preceding-sibling::metadata/@whats_new_id)]
Regards,
Armen
 
Old July 29th, 2003, 08:55 AM
Registered User
 
Join Date: Jul 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Armen,

Thank you VERY much. My code now works perfectly.

Regards,
Ben

 
Old July 30th, 2003, 07:31 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

an alternative method which avoids using the expensive preceding-sibling and following-sibling expressions is to use keys for your grouping (this method is known as the Muenchian method).

Create a key based on the whats_new_id attribute by placing this line at the top of your stylesheet
Code:
<xsl:key name="NewArticles" match="metadata" use="@whats_new_id"/>
Then you can use this key to group the articles and select all articles within a group by changing your whats_new_article template to the following (I've commented out your for-each lines which I've changed to use the keys). Note that I've also changed the expensive \\ path expression to use the actual path, since we know exactly what it is.
Code:
<xsl:template name="whats_new_article">

    <xsl:for-each select="root/metadata_object/metadata[generate-id()=generate-id(key('NewArticles', @whats_new_id)[1])]">
        <xsl:sort select="@whats_new_id"/>
        <table cellpadding="0" cellspacing="0" align="center" border="0" bgcolor="#ffffff" width="100%">
            <tr class="mdo">
                <td colspan="2">
                    <xsl:for-each select="metadata_detail[@mdi_type_cd='URL']">
                        <xsl:value-of select="@mdi_efctv_dt_month"/>
                        <xsl:text> </xsl:text>

                        <xsl:value-of select="@mdi_efctv_dt_year"/>
                    </xsl:for-each>
                    <br/>
                </td>
            </tr>
            <tr>
                <td width="25">&nbsp;</td>
                <td class="new">

                    <xsl:for-each select="key('NewArticles', @whats_new_id)">
                        <xsl:sort select="@mdi_effctv_dt"/>
                        <li>
                            <a target="_self">
                                <xsl:attribute name="href"><xsl:for-each select="metadata_detail[@mdi_type_cd='URL']"><xsl:value-of select="@mdi_txt"/></xsl:for-each></xsl:attribute>
                                <xsl:value-of select="@mdo_nm"/>
                            </a>
                        </li>
                    </xsl:for-each>
                </td>
            </tr>
        </table>
    </xsl:for-each>
</xsl:template>
rgds
Phil





Similar Threads
Thread Thread Starter Forum Replies Last Post
Grouping and Distinct items Condor76 XML 5 May 23rd, 2007 01:11 AM
Get distinct values on *second* grouping of nodes. MissHenesy XSLT 4 October 23rd, 2006 10:34 AM
XSLT Select Distinct jaa XSLT 1 April 6th, 2006 11:10 AM
XSLT 1.0 Grouping kwilliams XSLT 0 January 11th, 2006 06:30 PM
xslt distinct and countnumber of occurance imran_py XSLT 1 May 5th, 2004 02:50 PM





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