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 June 9th, 2008, 05:41 AM
Registered User
 
Join Date: Jun 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT Joins

Hi All,

I am VERY new to XSLT and I have hit a stumbling block. I have an xml file in the following format (simplified):

Code:
<listing>
  <venueList>
    <venue venueID="x100clu">

    </venue>
  </venueList>
  <titleList>
    <title titleID="ybrakes" titleName="Brakes">
      <category categoryName="mus" subCategory="rock"></category>

    </title>
   </titleList>
  <eventList>
    <event eventID="z4331565" titleID="ybrakes" venueID="x100clu" />
  <eventList>
</listing>
What I basically want to achieve is a transformed XML file that shows a list of venues that will with a 'description' node. These venues will not be distinct but a venue will be unique for each category i.e.

Code:
<venues>
  <venue id="venue1">
    <category>Cat1</category>
    <description>
     <!-- 
     all "titleList/title[@titleName] for events that are in this 
     venue AND this category 
     -->
    </description>
  </venue>
  <venue id="venue1">
    <category>Cat2</category>
    <description>
     <!-- 
     all "titleList/title[@titleName] for events that are in this 
     venue AND this category 
     -->
    </description>
  </venue>
  <venue id="venue2">
    <category>Cat2</category>
    <description>
     <!-- 
     all "titleList/title[@titleName] for events that are in this 
     venue AND this category 
     -->
    </description>
  </venue>
</venues>
Where do I begin?



 
Old June 9th, 2008, 06:07 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I think this is a combination of joining and grouping. You basically want something like

<xsl:for-each select="listing/venueList/venue">
  <xsl:variable name="events" select="listing/eventList/event[@venueId=current()/@venueId]"/>
  <xsl:variable name="titles" select="/listing/titleList/title[@titleId=$events/@titleId"/>
<xsl:for-each-group select="$titles" group-by="category/categoryName">
  <venue id="@venueId">
    <category><xsl:value-of select="current-grouping-key()"/></category>
      <xsl:apply-templates select="current-group()"/>
    </category>
  </venue>

xsl:for-each-group is XSLT 2.0; if you have to use 1.0 the syntax is much clumsier, search for "Muenchian grouping".

Note also that the join can be speeded up using xsl:key if your data volumes are large.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 9th, 2008, 08:49 AM
Registered User
 
Join Date: Jun 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I get that for-each-group cannot be a child of for-each?

 
Old June 9th, 2008, 08:53 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

xsl:for-each-group is XSLT 2.0, if you get an error message using that instruction then it is likely because you use an XSLT 1.0 processor.
Saxon, Gestalt, and AltovaXML are the only XSLT 2.0 processors so far.

--
  Martin Honnen
  Microsoft MVP - XML
 
Old June 9th, 2008, 10:34 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>I get that for-each-group cannot be a child of for-each?

That probably means you're using an XSLT 1.0 processor. As Martin said, there are a lot of them still around.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
What are joins? Bhalchandra SQL Server 2000 2 July 2nd, 2007 11:29 PM
Joins nalla Oracle 0 December 14th, 2005 05:54 AM
Joins r_ganesh76 SQL Server 2000 2 February 10th, 2005 12:21 AM
Multiple Inner Joins msmagied Classic ASP Databases 0 October 6th, 2004 05:11 PM
Joins marthaj SQL Server 2000 7 June 26th, 2003 09:02 AM





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