XML Tutorials

  Home arrow XML Tutorials arrow Page 4 - Querying XML
XML TUTORIALS

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

    Table of Contents:
  • Querying XML
  • 9.1 Performing Set Operations on Node Sets
  • Performing Set Operations on Node Sets continued
  • 9.2 Performing Set Operations on Node Sets Using Value Semantics

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Querying XML - 9.2 Performing Set Operations on Node Sets Using Value Semantics


    (Page 4 of 4 )

    Problem

    You need to find the union, intersection, set difference, or symmetrical set difference between elements in two node sets; however, in your problem, equality is not defined as node-set identity. In other words, equality is a function of a node’s value.

    Solution

    XSLT 1.0

    The need for this solution may arise when working with multiple documents. Consider two documents with the same DTD but content that may not contain duplicate element values. XSLT elements coming from distinct documents are distinct even if they contain elements with the same namespace, attribute, and text values. See Examples 9-1 to 9-4.

    Example 9-1. people1.xslt

    <people>
      <person name="Brad York" age="38" sex="m" smoker="yes"/>
      <person name="Charles Xavier" age="32" sex="m" smoker="no"/>
      <person name="David Williams" age="33" sex="m" smoker="no"/>
    </people>

    Example 9-2. people2.xslt

    <people>
      <person name="Al Zehtooney" age="33" sex="m" smoker="no"/>
      <person name="Brad York" age="38" sex="m" smoker="yes"/>
      <person name="Charles Xavier" age="32" sex="m" smoker="no"/>
    </people>

    Example 9-3. Failed attempt to use XSLT union to select unique people

    <xsl:template match="/">
      <people>
        <xsl:copy-of select="//person | document('people2.xml')//person"/>
      </people>
    </xsl:template>

    Example 9-4. Output when run with people1.xml as input

    <people>
       <person name="Brad York" age="38" sex="m" smoker="yes"/>
       <person name="Charles Xavier" age="32" sex="m" smoker="no"/>
       <person name="David Williams" age="33" sex="m" smoker="no"/>
       <person name="Al Zehtooney" age="33" sex="m" smoker="no"/>
       <person name="Brad York" age="38" sex="m" smoker="yes"/>
       <person name="Charles Xavier" age="32" sex="m" smoker="no"/>
    </people>

    Relying on node identity can also break down in single document cases when you want equality of nodes to be a function of their text or attribute values.

    Please check back next week for the continuation of this article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.
    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 1 - Follow our Sitemap