XML Tutorials
  Home arrow XML Tutorials arrow Page 2 - Solving Problems by Querying XML
Web Buyers Guide
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 
IBM Rational Software Development Conference
 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
     
     
    Web Buyers Guide
     
    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.

    Solving Problems by Querying XML - 9.4 Performing Structure-Preserving Queries
    (Page 2 of 4 )

    Problem

    You need to query an XML document so that the response has a structure that is identical to the original.

    Solution

    Structure-preserving queries filter out irrelevant information while preserving most of the document structure. The degree by which the output structure resembles the structure of the input is the metric that determines the applicability of this example. The more similar it is, the more this example applies.

    The example has two components—one reusable and the other custom. The reusable component is a stylesheet that copies all nodes to the output (identity transform). We used this stylesheet, shown in Example 9-9, extensively in Chapter 6.

    Example 9-9. copy.xslt

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="node() | @*">
      <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
    </xsl:template>

    </xsl:stylesheet>

    The custom component is a stylesheet that imports copy.xslt and creates rules to override its default behavior. For example, the following stylesheet results in output identical to people.xml, but with only female smokers:

      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

         <!-- Collapse space left by removing person elements -->
         <xsl:strip-space elements="people"/>

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

          <xsl:template match="person[@sex = 'f' and @smoker='yes']">
            <!-- Apply default behavior, which is to copy -->
            <xsl:apply-imports/>
          </xsl:template>

          <!-- Ignore other people --> 
        <xsl:template match="person"/>

      </xsl:stylesheet>

    Alternatively, a single template can match the things that you want to exclude and do nothing with them:

      <xsl:template match="person[@sex != 'f' or @smoker != 'yes']" />

    Discussion

    This example is extremely useful because it lets you preserve the structure of an XML document without necessarily knowing what its structure is. You only need to know what elements should be filtered out and that you create templates that do so.

    This example is applicable in contexts that most people would not describe as queries. For example, suppose you wanted to clone an XML document, but remove all attributes namedsex and replace them with an attribute calledgender:

      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

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

          <xsl:template match="@sex">
            <xsl:attribute name="gender">
              <xsl:value-of select="."/>
            </xsl:attribute>
          </xsl:template>

      </xsl:stylesheet>

    The beauty of this example is that it works on any XML document, regardless of its schema. If the document has elements with an attribute namedsex, they will becomegender:

    Can you guess what the following variation does?*

      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

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

          <xsl:template match="@sex">
            <xsl:attribute name="gender">
              <xsl:value-of select="."/>
            </xsl:attribute>
         
    <xsl:apply-imports/>
          </xsl:template>  

      </xsl:stylesheet>

    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


     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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