XML Tutorials
  Home arrow XML Tutorials arrow Page 3 - Solving Problems by Querying XML
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

Solving Problems by Querying XML
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-01-31

    Table of Contents:
  • Solving Problems by Querying XML
  • 9.4 Performing Structure-Preserving Queries
  • 9.5 Joins
  • Joins With Many Members

  • 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


    Solving Problems by Querying XML - 9.5 Joins


    (Page 3 of 4 )

    Problem

    You want to relate elements in a document to other elements in the same or different document.

    Solution

    A join is the process of considering all pairs of element as being related (i.e., a Cartesian product) and keeping only those pairs that meet the join relationship (usually equality).

    To demonstrate, I have adapted the supplier parts database found in 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 to be performed will answer the question, “Which suppliers and parts are in the same city (co-located)?”

    You can use two basic techniques to approach this problem in XSLT. The first uses nestedfor-eachloops:

      <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 usesapply-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>

    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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek