XML Tutorials
  Home arrow XML Tutorials arrow Performing Set Operations When Queryin...
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML TUTORIALS

Performing Set Operations When Querying XML
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-01-24

    Table of Contents:
  • Performing Set Operations When Querying XML
  • Implementing the Recursive Templates
  • XSLT 2.0 Enhancements
  • Equality in Programming

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Performing Set Operations When Querying XML


    (Page 1 of 4 )

    In this second part to a five-part series on using XSLT as an XML query language, we pick up where we left off last week, with how to perform set operations on node sets using value semantics. It is excerpted from chapter nine of the XSLT Cookbook, Second Edition, written by Sal Mangano (O'Reilly; ISBN: 0596009747). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    Performing Set Operations on Node Sets Using Value Semantics continued

    The following stylesheet provides a reusable implementation of union, intersection, and set difference based on value semantics. The idea is that a stylesheet importing this one will override the template whosemode="vset:element-equality". This allows the importing stylesheet to define whatever equality semantics make sense for the given input:

      <xsl:stylesheet version="1.0"  
    xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"
       xmlns:vset="http:/www.ora.com/ XSLTCookbook/namespaces/vset">

      <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

      <!-- The default implementation of element equality. Override in the importing
      stylesheet as necessary. -->

      <xsl:template match="node() | @*" mode="vset:element-equality">
        <xsl:param name="other"/>
        <xsl:if test=". = $other">
         
    <xsl:value-of select="true()"/>
        </xsl:if>
      </xsl:template>

      <!-- The default set membership test uses element equality. You will rarely need to
      override this in the importing stylesheet. -->
      <xsl:template match="node() | @*" mode="vset:member-of">
       
    <xsl:param name="elem"/>
        <xsl:variable name="member-of">
          <xsl:for-each select=".">
            <xsl:apply-templates select="." mode="vset:element-equality">
              <xsl:with-param name="other" select="$elem"/>
            </xsl:apply-templates>
          </xsl:for-each>
        </xsl:variable>
        <xsl:value-of select="string($member-of)"/>
     
    </xsl:template>

      <!-- Compute the union of two sets using "by value" equality. -->
      <xsl:template name="vset:union">
        <xsl:param name="nodes1" select="/.." />
        <xsl:param name="nodes2" select="/.." />
        <!-- for internal use -->
        <xsl:param name="nodes" select="$nodes1 | $nodes2" />
        <xsl:param name="union" select="/.." />
        <xsl:choose>
         
    <xsl:when test="$nodes">
            <xsl:variable name="test">
              <xsl:apply-templates select="$union" mode="vset:member-of">
                <xsl:with-param name="elem" select="$nodes[1]" />
             
    </xsl:apply-templates>
            </xsl:variable>
            <xsl:call-template name="vset:union">
              <xsl:with-param name="nodes" select="$nodes[position() > 1]" />
              <xsl:with-param name="union"
                              select="$union | $nodes[1][not(string($test))]" />
           
    </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="$union" mode="vset:union" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>

      <!-- Return a copy of union by default. Override in importing stylesheet to receive
      reults as a "callback"-->
      <xsl:template match="/ | node() | @*" mode="vset:union">
       
    <xsl:copy-of select="."/>
      </xsl:template>

      <!-- Compute the intersection of two sets using "by value" equality. -->
      <xsl:template name="vset:intersection">
        <xsl:param name="nodes1" select="/.."/>
        <xsl:param name="nodes2" select="/.."/>
        <!-- For internal use -->
        <xsl:param name="intersect" select="/.."/>

        <xsl:choose>
          <xsl:when test="not($nodes1)">
            <xsl:apply-templates select="$intersect" mode="vset:intersection"/>
          </xsl:when>
          <xsl:when test="not($nodes2)">
            <xsl:apply-templates select="$intersect" mode="vset:intersection"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:variable name="test1">
              <xsl:apply-templates select="$nodes2" mode="vset:member-of">
                <xsl:with-param name="elem" select="$nodes1[1]"/>
              </xsl:apply-templates>
            </xsl:variable>
            <xsl:variable name="test2">
              <xsl:apply-templates select="$intersect" mode="vset:member-of">
                <xsl:with-param name="elem" select="$nodes1[1]"/>
             
    </xsl:apply-templates> 
            </xsl:variable>
            <xsl:choose>
              <xsl:when test="string($test1) and not(string($test2))">
                <xsl:call-template name="vset:intersection">
                  <xsl:with-param name="nodes1"
                          select="$nodes1[position() > 1]"/>
                  <xsl:with-param name="nodes2" select="$nodes2"/>
                  <xsl:with-param name="intersect"
                         
    select="$intersect | $nodes1[1]"/>
               
    </xsl:call-template>
              </xsl:when>
              <xsl:otherwise>
                <xsl:call-template name="vset:intersection">
                  <xsl:with-param name="nodes1"
                          select="$nodes1[position() > 1]"/>
                  <xsl:with-param name="nodes2" select="$nodes2"/>
                  <xsl:with-param name="intersect" select="$intersect"/>
               
    </xsl:call-template>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>

      <!-- Return a copy of intersection by default. Override in importing stylesheet to
      receive results as a "callback"-->
      <xsl:template match="/ | node() | @*" mode="vset:intersection">
        <xsl:copy-of select="."/>
      </xsl:template>

      <!-- Compute the differnce between two sets (node1 - nodes2) using "by value"
      equality. -->
      <xsl:template name="vset:difference">
        <xsl:param name="nodes1" select="/.."/>
        <xsl:param name="nodes2" select="/.."/>
        <!-- For internal use -->
        <xsl:param name="difference" select="/.."/>

        <xsl:choose>
          <xsl:when test="not($nodes1)">

            <xsl:apply-templates select="$difference" mode="vset:difference"/>
          </xsl:when>
          <xsl:when test="not($nodes2)">
            <xsl:apply-templates select="$nodes1" mode="vset:difference"/>
          </xsl:when>
          <xsl:otherwise>
           
    <xsl:variable name="test1">
              <xsl:apply-templates select="$nodes2" mode="vset:member-of">
                <xsl:with-param name="elem" select="$nodes1[1]"/>
             
    </xsl:apply-templates>
            </xsl:variable>
            <xsl:variable name="test2">
              <xsl:apply-templates select="$difference" mode="vset:member-of">
                <xsl:with-param name="elem" select="$nodes1[1]"/>
             
    </xsl:apply-templates>
            </xsl:variable>
            <xsl:choose>
             
    <xsl:when test="string($test1) or string($test2)">
                <xsl:call-template name="vset:difference">
                  <xsl:with-param name="nodes1"
                          select="$nodes1[position() > 1]"/>
                  <xsl:with-param name="nodes2" select="$nodes2"/>
                  <xsl:with-param name="difference" select="$difference"/>
               
    </xsl:call-template>
              </xsl:when>
              <xsl:otherwise>
                <xsl:call-template name="vset:difference">
                  <xsl:with-param name="nodes1"
                          select="$nodes1[position() > 1]"/>
                  <xsl:with-param name="nodes2" select="$nodes2"/>
                  <xsl:with-param name="difference"
                         
    select="$difference | $nodes1[1]"/>
                </xsl:call-template>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>

      <!-- Return a copy of difference by default. Override in importing stylesheet to
      receive results as a "callback"-->
      <xsl:template match="/ | node() | @*" mode="vset:difference">
        <xsl:copy-of select="."/>
      </xsl:template>

    More XML Tutorials Articles
    More By O'Reilly Media


       · This article is an excerpt from the "XSLT Cookbook, Second Edition," published by...
     

    Buy this book now. This article is excerpted from chapter nine of the XSLT Cookbook, Second Edition, written by Sal Mangano (O'Reilly; ISBN: 0596009747). Check it out today at your favorite bookstore. Buy this book now.

    XML TUTORIALS ARTICLES

    - Validation with Document Type Definitions (D...
    - Creating a Well-Formed XML Document
    - Creating XML Taxonomies
    - Getting to Know XML
    - A Friendly Approach to XML
    - Creating RSS 2.0 Feeds
    - Using Modules in Your RSS Feed
    - RSS 2.0
    - Querying XML: Use Cases
    - Joins and Query Use with XML
    - Solving Problems by Querying XML
    - Performing Set Operations When Querying XML
    - Querying XML
    - Handling Data for Ajax with JSON
    - Handling XML Data for Ajax





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek