XML Tutorials

  Home arrow XML Tutorials arrow Page 2 - 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

    TOOLS YOU CAN USE

    advertisement

    Joins and Query Use with XML - 9.6 Implementing the W3C XML Query-Use Cases in XSLT


    (Page 2 of 4 )

    Problem

    You need to perform a query operation similar to one of the use cases in http://www.w3.org/TR/2001/WD-xmlquery-use-cases-20011220, but you want to use XSLT rather than XQuery (http://www.w3.org/TR/xquery/).

    Solution

    The following examples are XSLT solutions to most of the XML query-use cases presented in the W3C document. The descriptions of each use case are taken almost verbatim from the W3C document.

    Use case “XMP”: experiences and exemplars.

    This use case contains several example queries that illustrate requirements gathered by the W3C from the database and document communities. The data use by these queries follows in Examples 9-10 to Example 9-13.

    Example 9-10. bib.xml

    <bib>
        <book year="1994">
            <title>TCP/IP Illustrated</title>             
            <author><last>Stevens</last> 
    <first>W. </first></author> 
            <publisher>Addison-Wesley</publisher>
            <price> 65.95</price>
         </book>

         <book year="1992">
            <title>Advanced Programming in the Unix environment</title> 
            <author><last>Stevens</last><first>W. </first></author>
            <publisher>Addison-Wesley</publisher>
            <price>65.95</price>
         </book>

         <book year="2000">
            <title>Data on the Web</title>
            <author><last>Abiteboul </last><first> Serge</first></author> 
            <author><last>Buneman</last><first> Peter</first></author>
            <author><last>Suciu </last><first>Dan </first></author>
            <publisher>Morgan Kaufmann Publishers</publisher>
            <price> 39.95</price>
         </book>

         <book year="1999">
            <title>The Economics of Technology and Content for Digital TV</title> 
            <editor>
                   <last>Gerbarg</last> <first>Darcy</first>          
                     <affiliation>CITI </affiliation>
            </editor>
               <publisher>Kluwer Academic Publishers</publisher>
            <price>129.95</price>
         </book>

    </bib>


    Example 9-11. reviews.xml

    reviews>
        <entry>
            <title>Data on the Web</title>
            <price>34.95</price>
            <review>
                   A very good discussion of semi-structured database
                   systems and XML.
            </review>
        </entry>
        <entry>
            <title>Advanced Programming in the Unix environment</title>
            <price>65.95</price>
            <review>
                   A clear and detailed discussion of UNIX programming.
            </review>
        </entry>
        <entry>
            <title>TCP/IP Illustrated</title>
            <price>65.95</price>
            <review>
                   One of the best books on TCP/IP.
            </review>
        </entry>
    </reviews>

    Example 9-12. books.xml

    <chapter>
        <title>Data Model</title>
        <section>
            <title>Syntax For Data Model</title>
        </section>
        <section>
            <title>XML</title>
            <section>
                <title>Basic Syntax</title>
            </section>
            <section>
                <title>XML and Semistructured Data</title>
            </section>
        </section>
    </chapter>

    Example 9-13. prices.xml

    <prices>
        <book>
            <title>Advanced Programming in the Unix environment</title>
            <source>www.amazon.com</source>
            <price>65.95</price>
        </book>
        <book>
            <title>Advanced Programming in the Unix environment</title>
            <source>www.bn.com</source>
            <price>65.95</price>
        </book>
        <book>
            <title> TCP/IP Illustrated </title>
            <source>www.amazon.com</source>
            <price>65.95</price>
        </book>
        <book>
            <title> TCP/IP Illustrated </title>
            <source>www.bn.com</source>
            <price>65.95</price>
        </book>
        <book>
            <title>Data on the Web</title>
            <source>www.amazon.com</source>
            <price>34.95</price>
        </book>
        <book>
            <title>Data on the Web</title>
            <source>www.bn.com</source>
            <price>39.95</price>
        </book>
    </prices>
    Question 1. List books in bib.xml published by Addison-Wesley after 1991, including
    their year and title:
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:import href="copy.xslt"/>

    <xsl:template match="book[publisher = 'Addison-Wesley' and @year > 1991]">
      <xsl:copy-of select="."/> </xsl:template>

    <xsl:template match="book"/>

    </xsl:stylesheet>
    Question 2. Create a flat list of all the title-author pairs from bib.xml, with each
    pair enclosed in a "result" element:
    <xsl:template match="/">
    <results>
      <xsl:apply-templates select="bib/book/author"/>
    </results>
    </xsl:template>

    <xsl:template match="author">
      <result>
        <xsl:copy-of select="preceding-sibling::title"/>
        <xsl:copy-of select="."/>
      </result>
    </xsl:template>
    Question 3. For each book in bib.xml, list the title and authors, grouped inside a
    "result" element:
    <xsl:template match="bib">
      <results>
        <xsl:for-each select="book">
        <result>
          <xsl:copy-of select="title"/>
          <xsl:copy-of select="author"/>
        </result>
      </xsl:for-each>
     </results>
    </xsl:template>
    Question 4. For each author in bib.xml, list the author's name and the titles of all
    books by that author, grouped inside a "result" element:
    <xsl:template match="/">
    <results>
      <xsl:for-each select="//author[not(.=preceding::author)]">
        <result>
          <xsl:copy-of select="."/>
          <xsl:for-each select="/bib/book[author=current()]">
            <xsl:copy-of select="title"/>
          </xsl:for-each>
        </result>
      </xsl:for-each>
      </results>
    Question 5. For each book found on both http://www.bn.com (bib.xml) and http://www.
    amazon.com (reviews.xml), list the title of the book and its price from each source:
    <xsl:variable name="bn" select="document('bib.xml')"/> <xsl:variable name="amazon" select="document('reviews.xml')"/>

    <!--Solution 1 -->
    <xsl:template match="/">
      <books-with-prices>
      <xsl:for-each select="$bn//book[title = $amazon//entry/title]">
        <book-with-prices>
          <xsl:copy-of select="title"/>
          <price-amazon><xsl:value-of
          select="$amazon//entry[title=current()/title]/price"/></price-amazon>
          <price-bn><xsl:value-of select="price"/></price-bn>
        </book-with-prices>
      </xsl:for-each>
      </books-with-prices>
    </xsl:template>

    <!--Solution 2-->
    <xsl:template match="/">
      <books-with-prices>
      <xsl:for-each select="$bn/book">
        <xsl:variable name="bn-book" select="."/>
        <xsl:for-each select="$amazon//entry[title=$bn-book/title]">
          <book-with-prices>
            <xsl:copy-of select="title"/>
            <price-amazon><xsl:value-of select="price"/></price-amazon>
            <price-bn><xsl:value-of select="$bn-book/price"/></price-bn>
          </book-with-prices>
        </xsl:for-each>
      </xsl:for-each>
      </books-with-prices>
    </xsl:template>
    Question 6. For each book that has at least one author, list the title and first two
    authors, as well as an empty
    "et-al" element if the book has additional authors:
    <xsl:template match="bib">
      <xsl:copy>
        <xsl:for-each select="book[author]">
          <xsl:copy>
            <xsl:copy-of select="title"/>
            <xsl:copy-of select="author[position() &lt;= 2]"/>
            <xsl:if test="author[3]">
            <et-al/>
            </xsl:if>
          </xsl:copy>
        </xsl:for-each>
      </xsl:copy>
    </xsl:template>
    Question 7. List the titles and years of all books published by Addison-Wesley after
    1991, in alphabetic order:
    <xsl:template match="bib">
      <xsl:copy>
        <xsl:for-each select="book[publisher = 'Addison-Wesley'
              and @year > 1991]">
          <xsl:sort select="title"/>
          <xsl:copy>
            <xsl:copy-of select="@year"/>
            <xsl:copy-of select="title"/>
          </xsl:copy>
        </xsl:for-each>
      </xsl:copy>
    </xsl:template>
    Question 8. In the document books.xml, find all section or chapter titles that
    contain the word "XML", regardless of the nesting level:
     
    <xsl:template match="/">
    <results>
      <xsl:copy-of select="(//chapter/title |
      //section/title)[contains(.,'XML')]"/>
    </results>
    </xsl:template>
    Question 9. In the document prices.xml, find the minimum price for each book in the
    form of a "minprice" element with the book title as its title attribute:
    <xsl:include href="../math/math.min.xslt"/>

    <xsl:template match="/">
    <results>
      <xsl:for-each select="//book/title[not(. = ./preceding::title)]">
        <xsl:variable name="min-price">
          <xsl:call-template name="math:min">
            <xsl:with-param name="nodes" select="//book[title =
                        current()]/price"/>
          </xsl:call-template>
        </xsl:variable>
        <minprice title="{.}">
          <price><xsl:value-of select="$min-price"/></prices>
        </minprice>
      </xsl:for-each>
    </results>
    </xsl:template>
    Question 10. For each book with an author, return the book with its title and
    authors. For each book with an editor, return a reference with the book title and the
    editor's affiliation:
    <xsl:template match="bib"> <xsl:copy>
      <xsl:for-each select="book[author]">
        <xsl:copy>
          <xsl:copy-of select="title"/>
          <xsl:copy-of select="author"/>
        </xsl:copy>
      </xsl:for-each>

      <xsl:for-each select="book[editor]">
        <reference>
          <xsl:copy-of select="title"/>
          <org><xsl:value-of select="editor/affiliation"/></org>
        </reference>
      </xsl:for-each>
      </xsl:copy>
    </xsl:template>
    Question 11. Find pairs of books that have different titles but the same set of
    authors (possibly in a different order):
    <xsl:include href="query.equal-values.xslt"/>

    <xsl:template match="bib">
      <xsl:copy>
        <xsl:for-each select="book[author]">
          <xsl:variable name="book1" select="."/>
          <xsl:for-each select="./following-sibling::book[author]">
            <xsl:variable name="same-authors">
              <xsl:call-template name="query:equal-values">
                <xsl:with-param name="nodes1" select="$book1/author"/>
                <xsl:with-param name="nodes2" select="author"/> 
              </xsl:call-template>
            </xsl:variable>
            <xsl:if test="string($same-authors)">
              <book-pair>
                <xsl:copy-of select="$book1/title"/>
                <xsl:copy-of select="title"/>
              </book-pair>
            </xsl:if>
          </xsl:for-each>
        </xsl:for-each>
      </xsl:copy>
    </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


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