XML technologies / XSLT / XSLT Cookbook / Query XML / Joins

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

You want to associate elements with other elements in the same document or a different one.

Solution

A join considers all pairs of elements together, a Cartesian product, and retains only pairs satisfying the join relation, usually equality.

To demonstrate, I converted the supplier database from Date’s An Introduction to Database Systems (Addison-Wesley, 1986) to XML:

<database>
  <suppliers>
    <supplier id="S1" name="Smith" status="20" city="London"/>
    <supplier id="S2" name="Jones" status="10" city="Paris"/>
    <supplier id="S3" name="Blake" status="30" city="Paris"/>
    <supplier id="S4" name="Clark" status="20" city="London"/>
    <supplier id="S5" name="Adams" status="30" city="Athens"/>
  </suppliers>
  <parts>
    <part id="P1" name="Nut" color="Red" weight="12" city="London"/>
    <part id="P2" name="Bult" color="Green" weight="17" city="Paris"/>
    <part id="P3" name="Screw" color="Blue" weight="17" city="Rome"/>
    <part id="P4" name="Screw" color="Red" weight="14" city="London"/>
    <part id="P5" name="Cam" color="Blue" weight="12" city="Paris"/>
    <part id="P6" name="Cog" color="Red" weight="19" city="London"/>
  </parts>
  <inventory>
    <invrec sid="S1" pid="P1" qty="300"/>
    <invrec sid="S1" pid="P2" qty="200"/>
    <invrec sid="S1" pid="P3" qty="400"/>
    <invrec sid="S1" pid="P4" qty="200"/>
    <invrec sid="S1" pid="P5" qty="100"/>
    <invrec sid="S1" pid="P6" qty="100"/>
    <invrec sid="S2" pid="P1" qty="300"/>
    <invrec sid="S2" pid="P2" qty="400"/>
    <invrec sid="S3" pid="P2" qty="200"/>
    <invrec sid="S4" pid="P2" qty="200"/>
    <invrec sid="S4" pid="P4" qty="300"/>
    <invrec sid="S4" pid="P5" qty="400"/>
  </inventory>
</database>

The join answers: “Which suppliers and parts are located in the same city?”

Two simple XSLT approaches are available. The first uses nested for-each loops:

<xsl:template match="/">
  <result>
    <xsl:for-each select="database/suppliers/*">
      <xsl:variable name="supplier" select="."/>
      <xsl:for-each select="/database/parts/*[@city=current( )/@city]">
        <colocated>
          <xsl:copy-of select="$supplier"/>
          <xsl:copy-of select="."/>
        </colocated>
      </xsl:for-each>
    </xsl:for-each>
  </result>
</xsl:template>

The second approach uses apply-templates:

<xsl:template match="/">
  <result>
    <xsl:apply-templates select="database/suppliers/supplier" />
  </result>
</xsl:template>
<xsl:template match="supplier">
  <xsl:apply-templates select="/database/parts/part[@city = current( )/@city]">
    <xsl:with-param name="supplier" select="." />
  </xsl:apply-templates>
</xsl:template>
<xsl:template match="part">
  <xsl:param name="supplier" select="/.." />
  <colocated>
    <xsl:copy-of select="$supplier" />
    <xsl:copy-of select="." />
  </colocated>
</xsl:template>

If one of the element sets has many members, consider using xsl:key to improve performance:

<xsl:key name="part-city" match="part" use="@city"/>
<xsl:template match="/">
  <result>
    <xsl:for-each select="database/suppliers/*">
      <xsl:variable name="supplier" select="."/>
      <xsl:for-each select="key('part-city',$supplier/@city)">
        <colocated>
          <xsl:copy-of select="$supplier"/>
          <xsl:copy-of select="."/>
        </colocated>
      </xsl:for-each>
    </xsl:for-each>
  </result>
</xsl:template>

Each stylesheet produces the same result:

<result>
  <colocated>
    <supplier id="S1" name="Smith" status="20" city="London"/>
    <part id="P1" name="Nut" color="Red" weight="12" city="London"/>
  </colocated>
  <colocated>
    <supplier id="S1" name="Smith" status="20" city="London"/>
    <part id="P4" name="Screw" color="Red" weight="14" city="London"/>
  </colocated>
  <colocated>
    <supplier id="S1" name="Smith" status="20" city="London"/>
    <part id="P6" name="Cog" color="Red" weight="19" city="London"/>
  </colocated>
  <colocated>
    <supplier id="S2" name="Jones" status="10" city="Paris"/>
    <part id="P2" name="Bult" color="Green" weight="17" city="Paris"/>
  </colocated>
  <colocated>
    <supplier id="S2" name="Jones" status="10" city="Paris"/>
    <part id="P5" name="Cam" color="Blue" weight="12" city="Paris"/>
  </colocated>
  <colocated>
    <supplier id="S3" name="Blake" status="30" city="Paris"/>
    <part id="P2" name="Bult" color="Green" weight="17" city="Paris"/>
  </colocated>
  <colocated>
    <supplier id="S3" name="Blake" status="30" city="Paris"/>
    <part id="P5" name="Cam" color="Blue" weight="12" city="Paris"/>
  </colocated>
  <colocated>
    <supplier id="S4" name="Clark" status="20" city="London"/>
    <part id="P1" name="Nut" color="Red" weight="12" city="London"/>
  </colocated>
  <colocated>
    <supplier id="S4" name="Clark" status="20" city="London"/>
    <part id="P4" name="Screw" color="Red" weight="14" city="London"/>
  </colocated>
  <colocated>
    <supplier id="S4" name="Clark" status="20" city="London"/>
    <part id="P6" name="Cog" color="Red" weight="19" city="London"/>
  </colocated>
</result>

Discussion

XSLT 1.0

This join is called an equijoin because it links elements by equality. More generally, joins can use other relations. For example: “Select every combination of supplier and part information where the supplier city follows the part city alphabetically.”

It would be nice to write the following stylesheet directly, but XSLT 1.0 defines no relational operations on string types:

<xsl:template match="/">
  <result>
    <xsl:for-each select="database/suppliers/*">
      <xsl:variable name="supplier" select="."/>
      <!-- Das funktioniert nicht! -->
      <xsl:for-each select="/database/parts/*[current( )/@city &gt; @city]">
        <colocated>
          <xsl:copy-of select="$supplier"/>
          <xsl:copy-of select="."/>
        </colocated>
      </xsl:for-each>
    </xsl:for-each>
  </result>
</xsl:template>

Instead, use xsl:sort to build a table mapping city names to integers reflecting their order. This relies on Saxon treating variables containing result tree fragments as node sets when the version is 1.1. Alternatively, use your XSLT 1.0 processor’s node-set function or an XSLT 2.0 processor:

<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:variable name="unique-cities" select="//@city[not(. = ../preceding::*/@city)]"/>
  <xsl:variable name="city-ordering">
    <xsl:for-each select="$unique-cities">
      <xsl:sort select="."/>
      <city name="{.}" order="{position( )}"/>
    </xsl:for-each>
  </xsl:variable>
  <xsl:template match="/">
    <result>
      <xsl:for-each select="database/suppliers/*">
        <xsl:variable name="s" select="."/>
        <xsl:for-each select="/database/parts/*">
          <xsl:variable name="p" select="."/>
          <xsl:if test="$city-ordering/*[@name = $s/@city]/@order &gt; $city-ordering/*[@name = $p/@city]/@order">
            <supplier-city-follows-part-city>
              <xsl:copy-of select="$s"/>
              <xsl:copy-of select="$p"/>
            </supplier-city-follows-part-city>
          </xsl:if>
        </xsl:for-each>
      </xsl:for-each>
    </result>
  </xsl:template>
</xsl:stylesheet>

This query produces the following output:

<result>
  <supplier-city-follows-part-city>
    <supplier id="S2" name="Jones" status="10" city="Paris"/>
    <part id="P1" name="Nut" color="Red" weight="12" city="London"/>
  </supplier-city-follows-part-city>
  <supplier-city-follows-part-city>
    <supplier id="S2" name="Jones" status="10" city="Paris"/>
    <part id="P4" name="Screw" color="Red" weight="14" city="London"/>
  </supplier-city-follows-part-city>
  <supplier-city-follows-part-city>
    <supplier id="S2" name="Jones" status="10" city="Paris"/>
    <part id="P6" name="Cog" color="Red" weight="19" city="London"/>
  </supplier-city-follows-part-city>
  <supplier-city-follows-part-city>
    <supplier id="S3" name="Blake" status="30" city="Paris"/>
    <part id="P1" name="Nut" color="Red" weight="12" city="London"/>
  </supplier-city-follows-part-city>
  <supplier-city-follows-part-city>
    <supplier id="S3" name="Blake" status="30" city="Paris"/>
    <part id="P4" name="Screw" color="Red" weight="14" city="London"/>
  </supplier-city-follows-part-city>
  <supplier-city-follows-part-city>
    <supplier id="S3" name="Blake" status="30" city="Paris"/>
    <part id="P6" name="Cog" color="Red" weight="19" city="London"/>
  </supplier-city-follows-part-city>
</result>

XSLT 2.0

In XSLT 2.0, comparison operators work correctly on strings, allowing the simpler form:

<xsl:template match="/">
  <result>
    <xsl:for-each select="database/suppliers/*">
      <xsl:variable name="supplier" select="."/>
      <!-- In 2.0 ist das in Ordnung -->
      <xsl:for-each select="/database/parts/*[current( )/@city &gt; @city]">
        <colocated>
          <xsl:copy-of select="$supplier"/>
          <xsl:copy-of select="."/>
        </colocated>
      </xsl:for-each>
    </xsl:for-each>
  </result>
</xsl:template>

  

 

 

 

back to top