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

Subject: Re: [xsl] counting elements with values that match other element values
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 18 May 2006 09:32:12 -0400
Sean,

It is tempting to try running your code and knowing for sure, but even without doing that, insofar as I understand your requirements you are almost there. (I can't say for sure because I'm not sure I understand your requirements fully. For example, what should happen if you have c elements with the same value inside the same cSet?)

But a performant version of this would probably use keys to speed up the lookup. Doing it using brute-force XPath, like this, is going to bog down on sizable documents.

Since it doesn't sound like this is super-critical, maybe I'll leave it at that -- if you can get the key() arrow into your XSLT quiver, you'll be doing great.

Cheers,
Wendell

At 07:28 AM 5/18/2006, you wrote:

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:call-template>

</xsl:template>
<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"/>
</xsl:template>

</xsl:stylesheet>

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

<c>1</c><c>2</c><c>3</c><c>4</c><c>5</c><c>6</c><c>7</c><c>8</c><c>9</c>
  </ccc>
  <t>
     <h>1</h><v>3</v>
     <cSet><c>3</c><c>5</c><c>8</c></cSet>
  </t>
  <t>
     <h>1</h><v>4</v>
     <cSet><c>3</c><c>5</c></cSet>
  </t>
  <t>
     <h>1</h><v>7</v>
     <cSet><c>7</c><c>8</c></cSet>
  </t>
  <t>
     <h>1</h><v>9</v>
     <cSet><c>7</c><c>8</c></cSet>
  </t>
</root>


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread