XML Tutorials

  Home arrow XML Tutorials arrow Joins and Query Use with XML
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.

  •  
     

    SEARCH CODEWALKERS

    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

    blog comments powered by Disqus

    XML TUTORIALS ARTICLES

    - Validation with Document Type Definitions (D...
    - Creating a Well-Formed XML Document
    - 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
    - XML and JSON for Ajax

    Developer Shed Affiliates

     



    © 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap