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 10:59 +0200, Oliver Becker wrote:
>I don't know if anybody has mentioned this before:
>
>We had already
>    $nodes1[count(.|$nodes2)=count($nodes2)]
>as intersection expression.
>
>If we add a single exclamation mark
>    $nodes1[count(.|$nodes2)!=count($nodes2)]
>it will become the difference of the two node-sets $nodes1 and $nodes2.

I disagree, though I thought the same at first when saw Mike's great post 
... what you have will be those in $nodes1 not in $nodes2, but not those in 
$nodes2 not in $nodes1.

I figured you would need:

    (   $ns1[count(.|$ns2)!=count($ns2)]
      | $ns2[count(.|$ns1)!=count($ns1)] )

to get the true difference as I understand it, that being all nodes in both 
sets not in the other set.  I have an example below.

I hope this helps.

.......... Ken

T:\ftemp>type diff.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 xmlns:data="ken"
                 version="1.0">

<xsl:output method="text"/>

<data:data>
   <item>1</item>
   <item>2</item>
   <item>3</item>
   <item>4</item>
   <item>5</item>
</data:data>

<xsl:template match="/">                         <!--root rule-->
   <xsl:variable name="ns1"
                 select="document('')//data:data/item[position()>1]"/>
   <xsl:variable name="ns2"
                 select="document('')//data:data/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>


T:\ftemp>xt diff.xsl diff.xsl

     Intersection: 2
     Intersection: 3
     Intersection: 4
     Difference: 1
     Difference: 5
T:\ftemp>

--
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 instructor-led training:    2000-09-19/20,2000-10-03,2000-10-04,
-              2000-10-05,2000-10-19,2000-11-12,2000-11-13,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]