XML technologies / XSLT / XSLT Cookbook / Querying XML / Performing set operations on node sets

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

You need the union, intersection, difference or symmetric difference of two node sets. You may also need to test their equality or subset relationships.

Solution

XSLT 1.0

Union is trivial because XPath supports it directly:

<xsl:copy-of select="$knotenmenge1 | $knotenmenge2"/>

Finding the intersection of two node sets requires a more complex expression:

<xsl:copy-of select="$knotenmenge1[count(. | $knotenmenge2) = count($knotenmenge2)]"/>

This means all elements in knotenmenge1 that are also in knotenmenge2, since taking the union of knotenmenge2 and certain elements in knotenmenge1 yields the same number of elements.

Next is the set difference: elements in the first set but not the second.

<xsl:copy-of select="$knotenmenge1[count(. | $knotenmenge2) != count($knotenmenge2)]"/>

This means all elements in knotenmenge1 that are not also in knotenmenge2, since taking the union of knotenmenge2 and certain elements from knotenmenge1 yields a set with more elements.

Here is an example of symmetric difference: elements present in one set but not the other.

<xsl:copy-of select="$knotenmenge1[count(. | $knotenmenge2) != count($knotenmenge2)] | $knotenmenge2[count(. | $knotenmenge1) != count($knotenmenge1)] "/>

Symmetric difference is simply the union of the differences calculated in both directions.

Testing whether knotenmenge1 equals knotenmenge2:

<xsl:if test="count($km1|$km2) = count($km1) and count($km1) = count($km2)">

Two sets are equal if their union produces a set with the same number of elements as each of the original sets.

Testing whether knotenmenge2 is a subset of knotenmenge1:

<xsl:if test="count($knotenmenge1|$knotenmenge2) = count($knotenmenge1)">

Testing whether knotenmenge2 is a proper subset of knotenmenge1:

<xsl:if test="count($km1|$km2) = count($km1) and count($km1) &gt; count(km2)">

XSLT 2.0

Set operations on sequences are directly supported in XPath 2.0 (for details, see the recipe Using set operations).

Discussion

You may wonder what set operations have to do with XML queries. They provide ways to identify commonalities and differences between sets of elements extracted from a document. Many basic questions about data concern shared and distinguishing properties.

For example, imagine extracting person elements from people.xml:

<xsl:variable name="Männer" select="//person[@sex='m']"/>
<xsl:variable name="Frauen" select="//person[@sex='w']"/>
<xsl:variable name="Raucher" select="//person[@smoker='ja']"/>
<xsl:variable name="Nichtraucher" select="//person[@smoker='nein']"/>

If you worked in life insurance, you would assess each of the following groups differently:

<!-- Männliche Raucher -->
<xsl:variable name="sehr-großes-Risiko" select="$Männer[count(. | $Raucher) = count($Raucher)]"/>
<!-- Weibliche Raucher -->
<xsl:variable name="hohes-Risiko" select="$Frauen[count(. | $Raucher) = count($Raucher)]"/>
<!-- Männliche Nichtraucher -->
<xsl:variable name="mittleres-Risiko" select="$Männer[count(. | $Nichtraucher) = count($Nichtraucher)]"/>
<!-- Weibliche Nichtraucher -->
<xsl:variable name="geringes-Risiko" select="$Frauen[count(. | $Nichtraucher) = count($Nichtraucher)]"/>

You have probably noticed that logic instead of set theory could give you the same answers more directly:

<!-- Männliche Raucher -->
<xsl:variable name="sehr-großes-Risiko" select="//person[@sex='m' and @smoker='ja']"/>
<!-- Weibliche Raucher -->
<xsl:variable name="hohes-Risiko" select="//person[@sex='w' and @smoker='ja']"/>
<!-- Männliche Nichtraucher -->
<xsl:variable name="mittleres-Risiko" select="//person[@sex='m' and @smoker='nein']"/>
<!-- Weibliche Nichtraucher -->
<xsl:variable name="geringes-Risiko" select="//person[@sex='w' and @smoker='nein']"/>

Extracting the groups of men and women first would make the following even more efficient:

<!-- Männliche Raucher -->
<xsl:variable name="sehr-großes-Risiko" select="$Männer[@smoker='ja']"/>
<!-- Weibliche Raucher -->
<xsl:variable name="hohes-Risiko" select="$Frauen[@smoker='ja']"/>
<!-- Männliche Nichtraucher -->
<xsl:variable name="mittleres-Risiko" select="$Männer[@smoker='nein']"/>
<!-- Weibliche Nichtraucher -->
<xsl:variable name="geringes-Risiko" select="$Frauen[@smoker='nein']"/>

These observations do not diminish the usefulness of the set approach. Set operations work even when you do not know what the sets contain. They operate at a higher level of abstraction. Imagine a complex XML document in which you are interested in these four sets:

<!-- Alle Elemente, die die Elemente c1 oder c2 als Kinder haben-->
<xsl:variable name="menge1" select="//*[c1 or c2]"/>
<!-- Alle Elemente, die die Elemente c3 und c4 als Kinder haben-->
<xsl:variable name="menge2" select="//*[c3 and c4]"/>
<!-- Alle Elemente, deren Eltern-Element das Attribut a1 besitzt-->
<xsl:variable name="menge3" select="//*[../@a1]"/>
<!-- Alle Elemente, deren Eltern-Element das Attribut a2 besitzt-->
<xsl:variable name="menge4" select="//*[../@a2]"/>

In the original example, the sets of men and women, like those of smokers and nonsmokers, were clearly disjoint. Here, you do not know. The sets may be entirely disjoint, overlap completely, or share only some elements. There are only two ways to determine what, say, menge1 and menge3 have in common. The first is to intersect them. The second traverses the entire document again using the logical and of their predicates. Clearly, intersection is the practical approach here.

EXSLT defines a set module containing functions that perform the set operations discussed here. EXSLT uses an interesting technique to return the results of its set operations. Instead of returning a result directly, it applies templates to the result in a mode specifically suited to the type of set operation. For example, after EXSLT set:intersection computes the intersection, it executes <xsl:apply-templates mode="set:intersection"/>. EXSLT provides a defaulttemplate in this mode, returning a copy of the result as a result tree fragment. This indirect return mechanism lets users import the set module and override the default. It is useful but limited. It is useful because it can eliminate the need for the node-set extension function to convert the result back into a node set. Its limitation is that the user stylesheet can contain at most one such overriding template per matching pattern for each operation. You might, however, want very different postprocessing for intersections computed at different places in the same stylesheet.

See also

An explanation of EXSLT set operations is available at EXSLT-Sets.

  

 

 

back to top