[xsl] counting elements with values that match other element values

Subject: [xsl] counting elements with values that match other element values
From: Sean Whalen <seanwhalen@xxxxxxxxxxx>
Date: Thu, 18 May 2006 07:28:17 -0400
I step away from xpath and xslt for long stretches, and when I come back, parts of the big picture are a little fuzzy. My sense is that for the problem below, I need to loop; the test can't be done in a single XSLT1.0 statement. I have a set of elements ("t") with children under them ("t/cSet/c"). I'm looking for children that occur twice. I'd like get a nice 1.0 nodeset, which is the practical reason for trying to shoehorn the task into a single statement.

The xslt and xml are below. I used command-line saxon for debugging.

The expression for the element "Find_Cs_In_Ts" is what I'd like to either get working, or get an official confirmation that the task is impossible. In the real implementation the "&gt; 0" would be " = 2", and the answer would be "3, 5, and 7", which are the numbers that occurs twice in the set. The other elements are just me playing around with similar constructions that return related data.

In SQL, this would be something like
select * from ccc where (select count (*) from T/cSet as TC where tc.c = ccc.c ) >0

Any explanation for what the right syntax is, or, conceptually, why the idea doesn't fit in with xslt1.0 would be greatly appreciated. I've got looping code for the task almost implemented, so, mostly I'm asking for how the parser is reading the expression I wrote, and how far away is what I wrote from what I really wanted?

Another approach might be to use the parent axis to walk back up and then over to other T elements to find the related data.

thanks for any suggestions,

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:xs="http://www.w3.org/2001/XMLSchema";>

<xsl:output method="html"/>

<xsl:template match="/">
<xsl:call-template name="make-Choices">


<xsl:template name = "make-Choices">

<xsl:variable name = "output" >
<xsl:element name="root">
<xsl:element name = "ccc"><xsl:copy-of select="root/ccc/*"/> </xsl:element>

<xsl:element name = "Find_Cs_In_Ts"><xsl:copy-of select="
//root/ccc/c[ count( //root/t[ h =1 and . = cSet/c ] ) &gt; 0 ] "/> </xsl:element>

<xsl:element name = "Count7s_In_TH"><xsl:copy-of select="
count( //root/t[ h =1 and cSet/c = 7 ] ) "/> </xsl:element>

<xsl:element name = "Cs_In_Ts"><xsl:copy-of select="
//root/ccc/c[ . = //root/t[ h =1 ]/cSet/c ] "/> </xsl:element>

<xsl:element name = "Count_TCs_In_C"><xsl:copy-of select="count( root/t[h =1]/cSet/c[. = //root/ccc/c] ) "/> </xsl:element> </xsl:element> </xsl:variable>

<xsl:copy-of select="$output"/>



<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="ukbSmall.xslt"?>
</t> <t>
</t> </root>

Current Thread