[xsl] Selecting all repeated elements
Subject: [xsl] Selecting all repeated elements
From: Jason Foster <jason.foster@xxxxxxxxxxx>
Date: Fri, 7 Jul 2006 13:41:20 -0400
|
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. 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=???]) > 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