XML Tutorials
  Home arrow XML Tutorials arrow Joins and Query Use with XML
eWeek
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  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
IBM® developerWorks
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
eWeek
 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

Joins and Query Use with XML
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-02-11

    Table of Contents:
  • Joins and Query Use with XML
  • 9.6 Implementing the W3C XML Query-Use Cases in XSLT
  • Use case TREE: queries that preserve hierarchy
  • Use case SEQ: queries based on sequence.

  • 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

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    Joins and Query Use with XML
    (Page 1 of 4 )

    In this fourth part of a five-part series on using XSLT as an XML query language, you'll learn how to implement the W3C XML query-use cases in XSLT, and more. This article 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.

    Further Discussion of Joins 

    XSLT 1.0

    The join you performed is called an equi-join because the elements are related by equality. More generally, joins can be formed using other relations. For example, consider the query, “Select all combinations of supplier and part information for which the supplier city follows the part city in alphabetical order.”

    It would be nice if you could simply write the following stylesheet, but XSLT 1.0 does not define relational operations on string types:

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

    Instead, you must create a table usingxsl:sortthat can map city names onto integers that reflect the ordering. Here you rely on Saxon’s ability to treat variables containing result-tree fragments as node sets when the version is set to 1.1. However, you can also use the node-set function of your particular XSLT 1.0 processor or use 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 results in 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

    Comparison operators work correctly on string values in XSLT 2.0, so the simpler form will work:

      <xsl:template match="/">
        <result>
          <xsl:for-each select="database/suppliers/*">
            <xsl:variable name="supplier" select="."/>
            <!-- This is okay in 2.0 -->
           <xsl:for-each select="/database/parts/*[current( )/@city > @city]">
          
    <colocated>
              <xsl:copy-of select="$supplier"/>
              <xsl:copy-of select="."/>
            </colocated>
            </xsl:for-each>
          </xsl:for-each>
        </result>
      </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

    - 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
    - XML and JSON for Ajax





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway