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 January 19th, 2006, 08:07 AM
Registered User
 
Join Date: Jan 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Urgent help, please !

Hi

This is kind of urgent and I would really appreciate som help.

I have a xml structure like the one below and I want to display (in html) only the latest version of every document that share the same documentid.

<documents>
    <document>
        <documentid>12ABCDF5</documentid>
        <documenttitle>Dokument X</documenttitle>
        <documentlink>http://documenturl1.vgrdok.se</documentlink>
        <ingress>Text text text text text text text text text text text text text text text text text text text text text text text text text text text text </ingress>
        <version>0</version>
    </document>
    <document>
        <documentid>12ABCDF5</documentid>
        <documenttitle>Dokument B</documenttitle>
        <documentlink>http://documenturl2.vgrdok.se</documentlink>
        <ingress>Text text text text text text text text text text text text text text text text text text text text text text text text text text text text </ingress>
        <version>11.0</version>
    </document>
    <document>
        <documentid>12ABCDF5</documentid>
        <documenttitle>Dokument C</documenttitle>
        <documentlink>http://documenturl3.vgrdok.se</documentlink>
        <ingress>Text text text text text text text text text text text text text text text text text text text text text text text text text text text text </ingress>
        <version>1.3</version>
    </document>
    <document>
        <documentid>12ABCDF5</documentid>
        <documenttitle>Dokument D</documenttitle>
        <documentlink>http://documenturl4.vgrdok.se</documentlink>
        <ingress>Text text text text text text text text text text text text text text text text text text text text text text text text text text text text </ingress>
        <version>2</version>
    </document>
    <document>
        <documentid>12ABCDF5</documentid>
        <documenttitle>Dokument E</documenttitle>
        <documentlink>http://documenturl5.vgrdok.se</documentlink>
        <ingress>Text text text text text text text text text text text text text text text text text text text text text text text text text text text text </ingress>
        <version>2.1</version>
    </document>
    <document>
        <documentid>12ABCDF5</documentid>
        <documenttitle>Dokument F</documenttitle>
        <documentlink>http://documenturl6.vgrdok.se</documentlink>
        <ingress>Text text text text text text text text text text text text text text text text text text text text text text text text text text text text </ingress>
        <version>0.4</version>
    </document>
    <document>
        <documentid>12ABCDF11</documentid>
        <documenttitle>Dokument G</documenttitle>
        <documentlink>http://documenturl7.vgrdok.se</documentlink>
        <ingress>Text text text text text text text text text text text text text text text text text text text text text text text text text text text text </ingress>
        <version>1.0</version>
    </document>
    <document>
        <documentid>12ABCDF11</documentid>
        <documenttitle>Dokument H</documenttitle>
        <documentlink>http://documenturl8.vgrdok.se</documentlink>
        <ingress>Text text text text text text text text text text text text text text text text text text text text text text text text text text text text </ingress>
        <version>1.1</version>
    </document>
    <document>
        <documentid>12ABCDF13</documentid>
        <documenttitle>Dokument I</documenttitle>
        <documentlink>http://documenturl9.vgrdok.se</documentlink>
        <ingress>Text text text text text text text text text text text text text text text text text text text text text text text text text text text text </ingress>
        <version>4</version>
    </document>
    <document>
        <documentid>12ABCDF14</documentid>
        <documenttitle>Dokument J</documenttitle>
        <documentlink>http://documenturl10.vgrdok.se</documentlink>
        <ingress>Text text text text text text text text text text text text text text text text text text text text text text text text text text text text </ingress>
        <version>4</version>
    </document>
</documents>


 
Old January 19th, 2006, 08:50 AM
Authorized User
 
Join Date: Dec 2005
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
Default

As per the submitted above file - You mean that only the "document" of "version" = 4 as to be get transformed as html.

kindly confirm.

-ROCXY

 
Old January 19th, 2006, 09:51 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This is a classic grouping problem, the only quirk being that instead of displaying all the members of a group (grouped together) you display only the last member. If you're using XSLT 1.0, use Muenchian grouping (http://www.jenitennison.com/xslt/grouping). In 2.0, do

<xsl:for-each-group select="document" group-by="documentid">
  <xsl:apply-templates select="current-group()[last()]"/>



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 19th, 2006, 10:00 AM
Registered User
 
Join Date: Jan 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael

I use 1.0 and the muenchian method helped me out, BUT how do I sort the resulting items from the grouping? My xsl:sort on documenttitle does not have any effect, why is that?


<xsl:for-each select="document[generate-id() = generate-id(key('documents-by-id', documentid)[1])]">
                            <xsl:sort data-type="text" select="documenttitle" order="ascending" />
                            <xsl:for-each select="key('documents-by-id', documentid)">
                                <xsl:sort select="version"
                                    data-type="number"
                                    order="descending" />
                                <xsl:if test="position() = 1">
                                    <xsl:apply-templates select="current()">
                                    </xsl:apply-templates>
                                </xsl:if>
                            </xsl:for-each>
                        </xsl:for-each>

Quote:
quote:Originally posted by mhkay
 This is a classic grouping problem, the only quirk being that instead of displaying all the members of a group (grouped together) you display only the last member. If you're using XSLT 1.0, use Muenchian grouping (http://www.jenitennison.com/xslt/grouping). In 2.0, do

<xsl:for-each-group select="document" group-by="documentid">
<xsl:apply-templates select="current-group()[last()]"/>



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 19th, 2006, 12:04 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Don't know, sorry. I can't seen any obvious reason why the sort shouldn't work.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 19th, 2006, 01:17 PM
Registered User
 
Join Date: Jan 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

As you see in my xsl below I have two if cases.
In the first if case where I use the Muench method my context is a document node just before i apply template.
In the second case the context just before apply template is the top level of all documents - the document node.

I think this is why a can't sort the result of the Muench method.

Does this give you any idea? (I hope...)

     <xsl:template match="documents">
        <html>
            <head/>
            <body>
                <xsl:if test="$showOnlyLatestVersion='yes'">
                        <xsl:for-each select="document[generate-id() = generate-id(key('documents-by-id', documentid)[1])]">
                            <xsl:for-each select="key('documents-by-id', documentid)">
                                <xsl:sort select="version"
                                    data-type="number"
                                    order="descending" />
                                <xsl:if test="position() = 1">
                                    <xsl:apply-templates select=".">
                                    </xsl:apply-templates>
                                </xsl:if>
                            </xsl:for-each>
                        </xsl:for-each>
                </xsl:if>
                <xsl:if test="not($showOnlyLatestVersion='yes')">
                    <xsl:apply-templates select="document">
                        <xsl:sort data-type="number" select="version" order="ascending" />
                    </xsl:apply-templates>
                </xsl:if>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="document">
    ...
    </xsl:template>


 
Old January 19th, 2006, 04:02 PM
Registered User
 
Join Date: Jan 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Problem solved.

I built a temporary tree in i variable which I processed in a sorted template.






Similar Threads
Thread Thread Starter Forum Replies Last Post
urgent deb_kareng ASP.NET 2.0 Professional 1 August 13th, 2007 07:29 AM
it's urgent deb_kareng ASP.NET 2.0 Professional 3 August 7th, 2007 07:40 AM
urgent help yash_coolbuddy_forindia BOOK: Wrox's ASP.NET 2.0 Visual Web Developer 2005 Express Edition Starter ISBN: 978-0-7645-8807-5 1 May 7th, 2007 08:40 AM
urgent???????????? nsr35 Beginning VB 6 1 October 3rd, 2005 10:57 AM
urgent ??????????????? nsr35 Pro VB Databases 0 October 3rd, 2005 04:53 AM





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