Re: [xsl] Question on Kaysian method for set intersection

Subject: Re: [xsl] Question on Kaysian method for set intersection
From: Hermann Stamm-Wilbrandt <STAMMW@xxxxxxxxxx>
Date: Wed, 28 Mar 2012 14:36:50 +0200
Thanks Michael, that really helped to get it right.

$ xsltproc i.xsl some.xml  | tidy -q -xml
<?xml version="1.0"?>
<out xmlns:data="data">
    <flag xmlns:xsl="";>A</flag>
    <flag xmlns:xsl="";>B</flag>
    <flag xmlns:xsl="";>A</flag>
    <flag xmlns:xsl="";>C</flag>
    <flag xmlns:xsl="";>A</flag>
    <flag xmlns:xsl="";>B</flag>
    <flag xmlns:xsl="";>A</flag>
    <flag xmlns:xsl="";>C</flag>


<xsl:stylesheet version="1.0"

  <xsl:template match="/">
    <xsl:variable name="ns1" select="document('')//flags[1]/flag"/>
    <xsl:variable name="ns2" select="document('')//flags[2]/flag"/>

    <xsl:variable name="intersection" select="$ns1[. = $ns2]"/>
    <xsl:variable name="union"        select="$ns1[not(. = $ns2)] | $ns2"/>

      <ns1><xsl:copy-of select="$ns1"/></ns1>
      <ns2><xsl:copy-of select="$ns2"/></ns2>
      <int><xsl:copy-of select="$intersection"/></int>
      <uni><xsl:copy-of select="$union"/></uni>


Mit besten Gruessen / Best wishes,

Hermann Stamm-Wilbrandt
Level 3 support for XML Compiler team and Fixpack team lead
WebSphere DataPower SOA Appliances
IBM Deutschland Research & Development GmbH
Vorsitzende des Aufsichtsrats: Martina Koederitz
Geschaeftsfuehrung: Dirk Wittkopp
Sitz der Gesellschaft: Boeblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294

  From:       Michael Kay <mike@xxxxxxxxxxxx>                                                                                      
  To:         xsl-list@xxxxxxxxxxxxxxxxxxxxxx,                                                                                     
  Date:       03/28/2012 01:39 PM                                                                                                  
  Subject:    Re: [xsl] Question on Kaysian method for set intersection                                                            

On 28/03/2012 12:16, Hermann Stamm-Wilbrandt wrote:
> I used Kaysian method as described in this document (section 1c), sofar:

> Now I wanted to use it similarly for nodesets from different parts of
> document.
> That does not work because /flags[1]/flag[.='A'] is not the same node
> as /flags[2]/flag[.='A'].
> Is it possible to get the flag with "A" for $int and flags for "A", "B"
> "C" for $uni?
> If so, with keys?
The so-called Kaysian method is a way of doing identity-based node-set
intersection and difference in XSLT 1.0, which does not offer the
"intersect" and "except" operators.

You seem to be wanting to do equality-based intersection and difference,
which is actually much simpler, for example

$x/ns1/flags/flag[not(. = $x/ns2/flags/flag)]

selects the flags that are in ns1 but not in ns2.

Michael Kay

Current Thread