XML technologies / XSLT / XSLT Cookbook / Querying XML / Determining set equality by value

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

You need to determine whether two node sets contain equal node values, ignoring order.

Solution

This is subtler than it first appears. Consider an obvious solution that works in many cases:

<xsl:template name="vset:equal-text-values">
  <xsl:param name="nodes1" select="/.."/>
  <xsl:param name="nodes2" select="/.."/>
  <xsl:choose>
    <!--Leere Knotenmengen haben gleiche Werte -->
    <xsl:when test="not($nodes1) and not($nodes2)">
      <xsl:value-of select="true( )"/>
    </xsl:when>
    <!--Knotenmengen ungleicher Größen können keine gleichen Werte haben -->
    <xsl:when test="count($nodes1) != count($nodes2)"/>
    <!-- Wenn ein Element von nodes1 in nodes2 vorhanden ist, dann haben die Knotenmengen gleiche Werte, falls die Knotenmengen ohne das gemeinsame Element gleiche Werte haben -->
    <xsl:when test="$nodes1[1] = $nodes2">
      <xsl:call-template name="vset:equal-text-values">
        <xsl:with-param name="nodes1" select="$nodes1[position( ) &gt; 1]"/>
        <xsl:with-param name="nodes2" select="$nodes2[not(. = $nodes1[1])]"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise/>
  </xsl:choose>
</xsl:template>

The test’s name emphasizes its intended context: equality means string-value equality. This template is incorrect if equality depends on attributes or more complex criteria. It also assumes the sets are genuine sets under string equality, without duplicates. That is not always true. Consider this XML describing people borrowing library books:

<?xml version="1.0" encoding="UTF-8"?>
<library>
  <book>
    <name>High performance Java programming.</name>
    <borrowers>
      <borrower>James Straub</borower>
    </borrowers>
  </book>
  <book>
    <name>Exceptional C++</name>
    <borrowers>
      <borrower>Steven Levitt</borower>
    </borrowers>
  </book>
  <book>
    <name>Design Patterns</name>
    <borrowers>
      <borrower>Steven Levitt</borower>
      <borrower>James Straub</borower>
      <borrower>Steven Levitt</borower>
    </borrowers>
  </book>
  <book>
    <name>The C++ Programming Language</name>
    <borrowers>
      <borrower>James Straub</borower>
      <borrower>James Straub</borower>
      <borrower>Steven Levitt</borower>
    </borrowers>
  </book>
</library>

Repeated names simply mean someone borrowed a book more than once. A query for books borrowed by the same people should recognize Design Patterns and The C++ Programming Language as such a pair. But using vset:equal-text-values would miss this result because it assumes no duplicates. Modify vset:equal-text-values as follows to tolerate duplicates:

<xsl:template name="vset:equal-text-values-ignore-dups">
  <xsl:param name="nodes1" select="/.."/>
  <xsl:param name="nodes2" select="/.."/>
  <xsl:choose>
    <!--Leere Knotenmengen haben gleiche Werte -->
    <xsl:when test="not($nodes1) and not($nodes2)">
      <xsl:value-of select="true( )"/>
    </xsl:when>
    <!-- Wenn ein Element von nodes1 in nodes2 vorhanden ist, dann haben die Knotenmengen gleiche Werte, falls die Knotenmengen ohne das gemeinsame Element gleiche Werte haben -->
    <!--Diese Zeile löschen<xsl:when test="count($nodes1) != count($nodes2)"/> -->
    <xsl:when test="$nodes1[1] = $nodes2">
      <xsl:call-template name="vset:equal-text-values">
        <xsl:with-param name="nodes1"select="$nodes1[not(. = $nodes1[1])]"/>
        <xsl:with-param name="nodes2" select="$nodes2[not(. = $nodes1[1])]"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise/>
  </xsl:choose>
</xsl:template>

The unequal-size test is commented out because duplicates invalidate it. One set might contain three elements with string value foo and the other only one foo. Ignoring duplicates, these sets should be equal. Also remove all elements equal to the first during recursion, not just the first element, as already done for the second set. This handles duplicates at every recursive pass. All text-value equality tests then work, but clearly unequal sets require extra work.

These tests are less general than the value-set operations in recipe Performing node-set operations with value semantics because they assume only text-value equality matters. Generalize with the same membership-testing technique: an element-equality test overridable by an importing stylesheet.

<xsl:template name="vset:equal">
  <xsl:param name="nodes1" select="/.."/>
  <xsl:param name="nodes2" select="/.."/>
  <xsl:if test="count($nodes1) = count($nodes2)">
    <xsl:call-template name="vset:equal-impl">
      <xsl:with-param name="nodes1" select="$nodes1"/>
      <xsl:with-param name="nodes2" select="$nodes2"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>
<!-- Sobald wir wissen, dass die Mengen die gleiche Anzahl an Elementen haben, müssen wir nur noch testen, dass jedes Mitglied der ersten Menge auch ein Mitglied der zweiten ist -->
<xsl:template name="vset:equal-impl">
  <xsl:param name="nodes1" select="/.."/>
  <xsl:param name="nodes2" select="/.."/>
  <xsl:choose>
    <xsl:when test="not($nodes1)">
      <xsl:value-of select="true( )"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="test">
        <xsl:apply-templates select="$nodes2" mode="vset:member-of">
          <xsl:with-param name="elem" select="$nodes1[1]"/>
        </xsl:apply-templates>
      </xsl:variable>
      <xsl:if test="string($test)">
        <xsl:call-template name="vset:equal-impl">
          <xsl:with-param name="nodes1" select="$nodes1[position( ) &gt; 1]"/>
          <xsl:with-param name="nodes2" select="$nodes2"/>
        </xsl:call-template>
      </xsl:if>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

Generalized equality tolerant of duplicates requires a more brute-force approach, making two passes over the sets:

<xsl:template name="vset:equal-ignore-dups">
   <xsl:param name="nodes1" select="/.."/>
   <xsl:param name="nodes2" select="/.."/>
   <xsl:variable name="mismatch1">
     <xsl:for-each select="$nodes1">
       <xsl:variable name="test-elem">
         <xsl:apply-templates select="$nodes2" mode="vset:member-of">
           <xsl:with-param name="elem" select="."/>
         </xsl:apply-templates>
       </xsl:variable>
       <xsl:if test="not(string($test-elem))">
         <xsl:value-of select=" 'false' "/>
       </xsl:if>
     </xsl:for-each>
   </xsl:variable>
   <xsl:if test="not($mismatch1)">
     <xsl:variable name="mismatch2">
       <xsl:for-each select="$nodes2">
         <xsl:variable name="test-elem">
           <xsl:apply-templates select="$nodes1" mode="vset:member-of">
             <xsl:with-param name="elem" select="."/>
           </xsl:apply-templates>
         </xsl:variable>
         <xsl:if test="not(string($test-elem))">
           <xsl:value-of select=" 'false' "/>
         </xsl:if>
      </xsl:for-each>
    </xsl:variable>
    <xsl:if test="not($mismatch2)">
      <xsl:value-of select="true( )"/>
    </xsl:if>
  </xsl:if>
</xsl:template>

The template iterates over the first set, looking for elements absent from the second. If none is found, variable $mismatch1 is null. Then repeat in the opposite direction by iterating over the second set.

Discussion

Queries often need set-equality tests. Consider these tasks:

  • Find books with the same authors.

  • Find suppliers stocking the same parts.

  • Find families with children of the same ages.

Whenever a one-to-many relationship is involved and you want items sharing the same set of related items, test set equality.

  

 

 

 

back to top