Performing Set Operations When Querying XML - Equality in Programming
(Page 4 of 4 )
Discussion
You might think that equality is a cut-and-dried issue; two things are either equal or they’re not. However, in programming (as in politics), equality is in the eye of the beholder. In a typical document, an element is associated with a uniquely identifiable object. For example, a paragraph element, <p>...</p>, is distinct from another paragraph element somewhere else in the document, even if they have the same content. Hence, set operations based on the unique identity of elements are the norm. However, when considering XSLT operations crossing multiple documents or acting on elements that result from applyingxsl:copy, we need to carefully consider what we want equality to be.
Here are some query examples in which value set semantics are required:
- You have two documents from different namespaces. Examples 9-5 to 9-8 help you find all the element (local) names these documents have in common and those that are unique to each namespace.
Example 9-5. doc1.xml
<doc xmlns:doc1="doc1" xmlns="doc1">
<chapter label="1">
<section label="1">
<p>
Once upon a time...
</p>
</section>
</chapter>
<chapter label="2">
<note to="editor">I am still waiting for my $100000 advance.</note>
<section label="1">
<p>
... and they lived happily ever after.
</p>
</section>
</chapter>
</doc>
Example 9-6. doc2.xml
<doc xmlns:doc1="doc2" xmlns="doc2">
<chapter label="1">
<section label="1">
<sub>
<p>
Once upon a time...
<ref type="footnote" label="1"/>
</p>
</sub>
<fig>Figure1</fig>
</section>
<footnote label="1">
Hey diddle diddle.
</footnote>
</chapter>
<chapter label="2">
<section label="1">
<p>
... and they lived happily ever after.
</p>
</section>
</chapter>
</doc>
Example 9-7. unique-element-names.xslt
<xsl:stylesheet version="1.0" xmlns:xsl=http://www.w3.org/1999/XSL/ Transform
xmlns:doc1="doc1" xmlns:doc2="doc2"
xmlns:vset="http:/www.ora.com/ XSLTCookbook/namespaces/vset"
extension-element-prefixes="vset">
<xsl:import href="set.ops.xslt"/>
<xsl:output method="text" />
<xsl:template match="/">
<xsl:text>
 The elements in common are: </xsl:text>
<xsl:call-template name="vset:intersection">
<xsl:with-param name="nodes1" select="//*"/>
<xsl:with-param name="nodes2" select="document('doc2.xml')//*"/>
</xsl:call-template>
<xsl:text>
 The elements only in doc1 are: </xsl:text>
<xsl:call-template name="vset:difference">
<xsl:with-param name="nodes1" select="//*"/>
<xsl:with-param name="nodes2" select="document('doc2.xml')//*"/>
</xsl:call-template>
<xsl:text>
 The elements only in doc2 are: </xsl:text>
<xsl:call-template name="vset:difference">
<xsl:with-param name="nodes1" select="document('doc2.xml')//*"/>
<xsl:with-param name="nodes2" select="//*"/>
</xsl:call-template>
<xsl:text>
 </xsl:text>
</xsl:template>
<xsl:template match="*" mode="vset:intersection">
<xsl:value-of select="local-name(.)"/>
<xsl:if test="position() != last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:template>
<xsl:template match="*" mode="vset:difference">
<xsl:value-of select="local-name(.)"/>
<xsl:if test="position() != last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:template>
<xsl:template match="doc1:* | doc2:*" mode="vset:element-equality">
<xsl:param name="other"/>
<xsl:if test="local-name(.) = local-name($other)">
<xsl:value-of select="true()"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Example 9-8. Output
The elements in common are: doc, chapter, section, p
The elements only in doc1 are: note
The elements only in doc2 are: sub, ref, fig, footnote
A Visio XML document consists of master shapes, master-shape instances, and user-defined shapes with no corresponding master. You would like to extract the data for all unique shapes. For purpose of this query, two shapes are equal if either of the following are true:
a. They both have master attributes,@Master, and these attribute values are equal.
b. At least one lacks a master attribute, but their geometry elements,Geom, are equal. Geometry elements are equal if all attributes of all descendants ofGeomare equal.
Otherwise, they are not equal.
This query can be implemented by taking the intersection of the set of all shapes with itself under the rules of equality stated earlier.*
You can also use thevset:uniontemplate with the nodes parameter:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"
xmlns:vxd="urn:schemas-microsoft-com:office:visio"
xmlns:vset="http:/www.ora.com/ XSLTCookbook/namespaces/vset"
extension-element-prefixes="vset">
<xsl:import href="set.ops.xslt"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<UniqueShapes>
<xsl:call-template name="vset:intersection">
<xsl:with-param name="nodes1" select="//vxd:Pages/*/*/vxd:Shape"/>
<xsl:with-param name="nodes2" select="//vxd:Pages/*/*/vxd:Shape"/>
</xsl:call-template>
</UniqueShapes>
</xsl:template>
<xsl:template match="vxd:Shape" mode="vset:intersection">
<xsl:copy-of select="." />
</xsl:template>
<xsl:template match="vxd:Shape" mode="vset:element-equality">
<xsl:param name="other"/>
<xsl:choose>
<xsl:when test="@Master and $other/@Master and @Master = $other/@Master">
<xsl:value-of select="true()"/>
</xsl:when>
<xsl:when test="not(@Master) or not($other/@Master)">
<xsl:variable name="geom1">
<xsl:for-each select="vxd:Geom//*/@*">
<xsl:sort select="name()"/>
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="geom2">
<xsl:for-each select="$other/vxd:Geom//*/@*">
<xsl:sort select="name()"/>
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:variable>
<xsl:if test="$geom1 = $geom2">
<xsl:value-of select="true()"/>
</xsl:if>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
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. |
|
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.
|
|