XML Tutorials

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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    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

    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 8 - Follow our Sitemap