[xsl] Identifying duplicate nodes within a branch

Subject: [xsl] Identifying duplicate nodes within a branch
From: "Mark Peters" <flickrmeister@xxxxxxxxx>
Date: Thu, 13 Nov 2008 08:59:34 -0500
Hi Everyone,

I've merged two documents on a set of <view> nodes using the Meunchian method.

<view_doc>
	<view name="view1">
		<column file="dev" id="view1.NAME"></column>
		<column file="dev" id="view1.DESCRIPTION"></column>
	</view>
	<view name="view2">
		<column file="doc" id="view2.ID">text</column>
		<column file="doc" id="view2.TIME_STAMP">text</column>
		<column file="doc" id="view2.CUSTOM0">text</column>
		<column file="dev" id="view2.CUSTOM1"></column>
	</view>
	<view name="view3">
		<column file="doc" id="view3.ID">text</column>
		<column file="doc" id="view3.TIME_STAMP">text</column>
		<column file="doc" id="view3.CREATED_ON">text</column>
		<column file="dev" id="view3.ID"></column>
		<column file="dev" id="view3.TIME_STAMP"></column>
		<column file="dev" id="view3.CREATED_ON"></column>
	</view>
</view_doc>


Now I'm trying to identify the <column> nodes where duplicate @id
values exist. Where there are duplicates, I'd like to include the
nodes where the @file value is "doc." But the output should also
include the unique column nodes, all of which have a @file value of
"dev."


Desired output:

<?xml version="1.0" encoding="UTF-8"?>
<view_doc>
	<view name="view1">
		<column file="dev" id="view1.NAME"></column>
		<column file="dev" id="view1.DESCRIPTION"></column>
	</view>
	<view name="view2">
		<column file="doc" id="view2.ID">text</column>
		<column file="doc" id="view2.TIME_STAMP">text</column>
		<column file="doc" id="view2.CUSTOM0">text</column>
		<column file="dev" id="view2.CUSTOM1"></column>
	</view>
	<view name="view3">
		<column file="doc" id="view3.ID">text</column>
		<column file="doc" id="view3.TIME_STAMP">text</column>
		<column file="doc" id="view3.CREATED_ON">text</column>
	</view>
</view_doc>

In the past few days, I've tried every bit of XSL logic I could find
online, including those in the archives of this list. Some have gotten
me closer than others. Here is my current XSL. It isn't the best
example. Unfortunately, I haven't saved every version Ive tried.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:xalan="http://xml.apache.org/xalan";
exclude-result-prefixes="xalan">
	<xsl:output indent="yes"/>
	<xsl:template match="node()|@*">
		<xsl:copy>
			<xsl:apply-templates select="@*"/>
			<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>
	<xsl:template match="view">
		<xsl:copy>
			<xsl:for-each select="column">
				<xsl:choose>
					<xsl:when test="not(preceding-sibling::column[@id =
current()/@id] or following-sibling::column[@id = current()/@id])">
						<xsl:apply-templates select="self::node()[@file='dev']"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:apply-templates select="self::node()[@file='doc']"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:for-each>
		</xsl:copy>
	</xsl:template>
</xsl:stylesheet>


I'd appreciate any advice.

Thanks,
Mark

-- 

Mark Peters
Senior Technical Writer
Saba Software

Current Thread