RE: [xsl] Selecting all repeated elements

Subject: RE: [xsl] Selecting all repeated elements
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 7 Jul 2006 19:52:11 +0100
> I've been hunting for an elegant way to obtain a sequence 
> containing those elements that share a common attribute (in 
> this case <course> elements with the same "code" attribute. 

That doesn't sound like a sequence to me, it sounds like a set of sequences,
one for each attribute value.

Anyway, this sounds like a standard description of the grouping problem, and
the first thing you should do is look at xsl:for-each-group if you are using
XSLT 2.0, or Muenchian grouping in the case of 1.0 (see
http://www.jenitennison.com/xslt/grouping)

Michael Kay
http://www.saxonica.com/

 
> I think that I've got a kludged solution using <xsl:for-each> 
> as follows:
> 
> <xsl:for-each select="child::course">
>    <xsl:variable name="targetCode" select="attribute::code"/>
>    <xsl:variable name="duplicates">
>      <if test="count(/descendant::course[attribute::code =
> $targetCode]) > 1">
>        <xsl:value-of select="concat(attribute::code,ancestor::term/
> attribute::name, ' ')"/>
>      </xsl:if>
>    </xsl:variable>
> </xsl:for-each>
> 
> This is ugly, and the result isn't a nodeset, so in general 
> I'm not impressed.  The thing I haven't been able to figure 
> out is whether XPath has something equivalent to a RegExp 
> backreference, which would allow something like:
> 
> <xsl:variable
>    name="duplicates"
>    select="child::course[count(/descendant::course
> [attribute::code=???]) &gt; 1]"
> />
> 
> The trick is what should replace ???
> 
> Any assistance would be greatly appreciated.  For what's it's 
> worth, I'm using XSLT to generate a linear program that 
> represents the courses our students can take in order to find 
> out what's the  
> smallest number of hours that they'll experience over their 
> program.   
> Sometimes they can choose courses in multiple locations 
> during their program.
> 
> Thanks!
> 
> Jason Foster

Current Thread