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">
  <ns1>
    <flag xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>A</flag>
    <flag xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>B</flag>
  </ns1>
  <ns2>
    <flag xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>A</flag>
    <flag xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>C</flag>
  </ns2>
  <int>
    <flag xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>A</flag>
  </int>
  <uni>
    <flag xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>B</flag>
    <flag xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>A</flag>
    <flag xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>C</flag>
  </uni>
</out>

$


<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:data="data"
>
<data:data>
  <flags>
    <flag>A</flag>
    <flag>B</flag>
  </flags>
  <flags>
    <flag>A</flag>
    <flag>C</flag>
  </flags>
</data:data>

  <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"/>

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

</xsl:stylesheet>


Mit besten Gruessen / Best wishes,

Hermann Stamm-Wilbrandt
Level 3 support for XML Compiler team and Fixpack team lead
WebSphere DataPower SOA Appliances
https://www.ibm.com/developerworks/mydeveloperworks/blogs/HermannSW/
----------------------------------------------------------------------
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:
>
http://www.xml.org//sites/www.xml.org/files/xslt_efficient_programming_techniques.pdf#page=3

>
>
> 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"
and
> "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
Saxonica

Current Thread