RE: [xsl] pairing up similar tags based on an attribute

Subject: RE: [xsl] pairing up similar tags based on an attribute
From: cknell@xxxxxxxxxx
Date: Thu, 12 Oct 2006 14:52:19 -0400
I can't recall if you got an answer on this, but if not, I'll see how I can help.

Let me re-state the situation and you can tell me if I got it right or how I got it wrong.

o) You have a set of <spec> elements (not tags).
o) Each <spec> element has two attributes: "partnumber" and "parentpn".
o) For every distinct value of "spec/@parentpn", there are exactly two <spec> elements.
o) For each distinct value of "parentpn", you want your stylesheet to emit the text node that is a child of the <value> element that, in turn, is a child of the <spec> element.

i.e., spec[@parentpn="9310079"]/value

The values of the "partnumber" attributes are a red herring, and don't fit into the XPath construction.

Are you using XSLT 1.0 or XSLT 2.0?

Here is an XST 2.0 construct that will do what you describe (as far as I can understand what you want.)

<xsl:for-each-group select="spec" group-by="@parentpn">
  <xsl:variable name="ppn" select="@parentpn" />
  <xsl:value-of select="value" />:<xsl:value-of select="following-sibling::spec[@parentpn=$ppn]/value" />
Charles Knell
cknell@xxxxxxxxxx - email

-----Original Message-----
From:     Webmaster <Webmaster@xxxxxxxxxxx>
Sent:     Wed, 11 Oct 2006 20:37:05 -0500
To:       "'xsl-list@xxxxxxxxxxxxxxxxxxxxxx'" <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject:  [xsl] pairing up similar tags based on an attribute


My data (snippet) looks like this:

<spec partnumber="9300033" parentpn="9310079">

<spec partnumber="9300133" parentpn="9310079">

<spec partnumber="9300033" parentpn="9310023">

<spec partnumber="9300133" parentpn="9310023">

I'm trying to write a template that prints out each pair of 9300033 and
9300133 with the same @parentpn, but without luck:

<xsl:variable name="connectortype" >
<xsl:for-each select="spec[@partnumber=9300033]">
  <xsl:for-each select="./@parentpn">
		<xsl:value-of select="ancestor::spec/value" />
		<xsl:value-of select="ancestor::spec[@partnumber=9300133 and
@parentpn=.]/value" />

With this code, I'm getting the correct value for the 9300033, but I can't
seem to grab the corresponding 9300133 value.  I'm not quite sure if my
xpath is right. 

Any help would be very much appreciated!!!


Current Thread