This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Standard XPath expression for the intersection of two node se ts (Was: RE: How can I test if an node included in a nodeset)


At 00/08/04 14:23 -0600, Mike Brown wrote:
>For those of us who can't wait for the 8th edition,

Ninth. :{)}

>and who somehow missed
>the original post, could someone recap what the Kaysian Method is? I'd go
>look it up in the archives, but they're hosed.

Here is the text as it stands today for the next edition:

========8<-------
Node-set intersection and difference
Module 8:  XPath and XSLT expressions and advanced techniques
Lesson 8-1:  Expression Functions


Sometimes it is necessary to determine the intersection or difference of 
two node-sets selected from the source tree.  The following expression, 
colloquially referred to as the "Kaysian Method" after Mike Kay who first 
proposed this use of XPath, will select only those nodes that are in both 
of two node-set variables named set1 and set2:

         ·       $set1[count(.|$set2)=count($set2)]

The above expression takes advantage of the XPath union operator to 
determine that a given node doesn't impact on the count of nodes of the 
union of the node and the second node-set.  The predicate returns true when 
the count isn't affected, thus including the member being tested.  It is 
not necessary to test the members of the second set because the 
intersection would have to include members of the first set, all of which 
are being tested in the above expression.

The symmetric difference requires an expression involving the union of the 
determination of those nodes in each set that are not in the other set:

         ·       (   $set1[count(.|$set2)!=count($set2)]
                   | $set2[count(.|$set1)!=count($set1)] )

========8<-------
Node-set intersection and difference (cont.)
Module 8:  XPath and XSLT expressions and advanced techniques
Lesson 8-1:  Expression Functions


The following script illustrates the assignment of two node-set variables 
from a common area of the source tree (in this case the stylesheet is also 
the source tree) and the intersection and symmetric difference of those two 
variables:

<?xml version="1.0"?><!--diff.xsl-->
<!--XSLT 1.0 - http://www.CraneSoftwrights.com/training -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 xmlns:data="crane"
                 version="1.0">

<xsl:output method="text"/>

<data:data>               <!--data source for testing purposes-->
   <item>1</item><item>2</item><item>3</item>
   <item>4</item><item>5</item><item>6</item>
</data:data>

<xsl:template match="/">                         <!--root rule-->
   <xsl:variable name="ns1" select="//item[position()>1]"/>
   <xsl:variable name="ns2" select="//item[position()&lt;5]"/>

   <xsl:for-each select="$ns1[count(.|$ns2)=count($ns2)]">
     Intersection: <xsl:value-of select="."/>
   </xsl:for-each>
   <xsl:for-each select="(   $ns1[count(.|$ns2)!=count($ns2)]
                           | $ns2[count(.|$ns1)!=count($ns1)] )">
     Difference: <xsl:value-of select="."/>
   </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

When run with itself as input, the following is the result:

     Intersection: 2
     Intersection: 3
     Intersection: 4
     Difference: 1
     Difference: 5
     Difference: 6
========8<-------


I hope this helps.

............. Ken

--
G. Ken Holman                    mailto:gkholman@CraneSoftwrights.com
Crane Softwrights Ltd.             http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999   (Fax:-0995)
Web site: XSL/XML/DSSSL/SGML services, training, libraries, products.
Book: Practical Transformation Using XSLT and XPath ISBN1-894049-05-5
Next public instructor-led training:        2000-09-19/20,2000-10-03,
.2000-10-04,2000-10-05,2000-10-19,2000-11-12,2000-12-03/04,2001-01-27


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]