Performing Set Operations When Querying XML - Implementing the Recursive Templates
(Page 2 of 4 )
These recursive templates are implemented in terms of the following definitions:
Union(nodes1,nodes2)
The union includes everything innodes2plus
everything innodes1not already a member of
nodes2.
Intersection(nodes1,nodes2)
The intersection includes everything innodes1that
is also a member ofnodes2.
Difference(nodes1,nodes2)
The difference includes everything innodes1that is
not also a member ofnodes2.
In all cases, membership defaults to equality of string values, but the importing stylesheet can override this default.
Given these value-oriented set operations, you can achieve the desired effect on people1.xml and people2.xml using the following stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"
xmlns:vset="http:/www.ora.com/ XSLTCookbook/namespaces/vset">
<xsl:import href="set.ops.xslt"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<people>
<xsl:call-template name="vset:union">
<xsl:with-param name="nodes1" select="//person"/>
<xsl:with-param name="nodes2" select="document('people2.xml')//person"/>
</xsl:call-template>
</people>
</xsl:template>
<!--Define person equality as having the same name -->
<xsl:template match="person" mode="vset:element-equality">
<xsl:param name="other"/>
<xsl:if test="@name = $other/@name">
<xsl:value-of select="true()"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Next: XSLT 2.0 Enhancements >>
More XML Tutorials Articles
More By O'Reilly Media
|
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.
|
|