[xsl] Newbie: Help with counting and removing duplicate elements

Subject: [xsl] Newbie: Help with counting and removing duplicate elements
From: "Wayne Posner" <wayne.posner@xxxxxxxxx>
Date: Thu, 1 Mar 2007 19:41:55 -0500
Hi all.

I'm a definite newbie when it comes to XSLT. I've got an XSL file that
takes an XML of one schema and maps it to another. As a result of the
mapping I end up with duplicate sibling nodes in my tree. I need to
remove the duplicates but count how many there were and set that value
to a qty attribute in the remaining node. It is possible that there
would be other nodes of the same value elsewhere in the tree, but I'm
only concerned when they're immediate siblings.

I've done searches and read up on grouping and counting using the
Muenchian Method, but I'm not sure how to modify my existing XSL to
include the code and even just trying to do a simple count in a
completely new XSL has been resulting in nothing being returned. Below
is my current XSL code and an blurb of XML I am translating from.  The
goal would be
to have my new XML output have only one instance of "spoke_nipple" and
a new attribute "qty" with a value of 2.....any help would be greatly
appreciated!

XML:
<assembly xmlns:xsd="http://www.w3.org/2001/XMLSchema";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
uid="-1" name="_MOTO_X.asm.1" source="NG-PRO/ENGINEER" version="1.0"
xmlns="righthemisphere.deepserver.bom" path="absolute">
<position p1="1" p2="0" p3="0" p4="0" p5="1" p6="0" p7="0" p8="0"
p9="1" p10="0" p11="0" p12="0" />
<instance uid="0" name="_MOTO_X.asm" >
<instance uid="277" name="SPOKE_NIPPLE" >
<position p1="3.319013E-001" p2="-9.433141E-001" p3="0.000000E+000"
p4="9.190716E-001" p5="3.233716E-001" p6="2.252517E-001"
p7="-2.124831E-001" p8="-7.476132E-002" p9="9.743006E-001" p10="-1.337976E+002"
p11="5.225291E+000" p12="-6.200934E+001" />
<part uid="278" name="SPOKE_NIPPLE" filename="spoke_nipple.prt.1" />
</instance>
<instance uid="277" name="SPOKE_NIPPLE" >
<position p1="-4.121733E-001" p2="0.000000E+000" p3="9.111055E-001"
p4="8.897386E-001" p5="-2.152981E-001" p6="4.025072E-001"
p7="1.961592E-001" p8="9.765484E-001" p9="8.874012E-002" p10="-1.178813E+002"
p11="-3.580657E+000" p12="-8.712816E+001" />
<part uid="278" name="SPOKE_NIPPLE" filename="spoke_nipple.prt.1" />
</instance>
</instance>
</assembly>

XSLT:


<?xml version='1.0' ?> <xsl:stylesheet version="2.0" xmlns:pc_function="uri://none" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:a="righthemisphere.deepserver.bom">


<msxsl:script language="JavaScript" implements-prefix="pc_function"> <![CDATA[ function rand() { return "" + Math.random()*100000; } ]]> </msxsl:script>

<xsl:variable
name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
<xsl:variable
name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>

<xsl:template match="/">
<catalog>
<xsl:attribute name="name">
<xsl:value-of select="a:assembly/@name"/>
</xsl:attribute>

<item>
<xsl:attribute name="uid">
<xsl:value-of select="substring-after(pc_function:rand(),'.')"/>
</xsl:attribute>

<name>
<xsl:value-of
select="translate(a:assembly/@name,$ucletters,$lcletters)"/>
</name>

<xsl:element name="number"/>
<xsl:element name="description"/>
<xsl:element name="qty"/>
<xsl:element name="uom"/>
<xsl:element name="unit_cost"/>
<xsl:element name="hasChild"/>

<xsl:apply-templates select="a:assembly/a:instance"/>
</item>
</catalog>
</xsl:template>

<xsl:template match="a:instance">
<item>
<xsl:attribute name="uid">
<xsl:value-of select="substring-after(pc_function:rand(),'.')"/>
</xsl:attribute>

<name>
<xsl:value-of select="translate(@name,$ucletters,$lcletters)"/>
</name>

<xsl:element name="number"/>
<xsl:element name="description"/>
<xsl:element name="qty"/>
<xsl:element name="uom"/>
<xsl:element name="unit_cost"/>
<xsl:element name="hasChild"/>
<xsl:apply-templates select="a:instance"/>
</item>
</xsl:template>
</xsl:stylesheet>

Current Thread