This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Is this right for a key?
- From: jestoll at crtinc dot com
- To: XSL-List at lists dot mulberrytech dot com
- Date: Thu, 06 Jun 2002 16:52:21 -0400
- Subject: [xsl] Is this right for a key?
- Reply-to: xsl-list at lists dot mulberrytech dot com
I have something of the nature:
<Chassis>
<Inserts>
<Insert>
<PartNumber>aaa</PartNumber>
<PartRNumber>bbb</PartRNumber>
<Level>1</Level>
<XStart>10</XStart>
</Insert>
<Insert>
<PartNumber>ccc</PartNumber>
<PartRNumber>ddd</PartRNumber>
<Level>1</Level>
<XStart>20</XStart>
</Insert>
</Inserts>
</Chassis>
that I transform to: (simply splitting each Insert into two Inserts, one
with a Hand of Left and one with a Hand of Right, and using a common
element - PartNumber for the PartNumber and PartRNumber elements)
<Chassis>
<Inserts>
<Insert>
<PartNumber>aaa</PartNumber>
<Level>1</Level>
<XStart>10</XStart>
<Hand>Left</Hand>
</Insert>
<Insert>
<PartNumber>bbb</PartNumber>
<Level>1</Level>
<XStart>10</XStart>
<Hand>Right</Hand>
</Insert>
<Insert>
<PartNumber>ccc</PartNumber>
<Level>1</Level>
<XStart>20</XStart>
<Hand>Left</Hand>
</Insert>
<Insert>
<PartNumber>ddd</PartNumber>
<Level>1</Level>
<XStart>20</XStart>
<Hand>Right</Hand>
</Insert>
</Inserts>
</Chassis>
Note that there are multiple Chassis in a file and Inserts with other
<Level> values, as well, but I'm trying to keep the sample XML short...
I then need to sort Inserts with matching Level and Hand values by their
relative XStart values and assign them a <Position> ordinal, such that the
Insert, with a particular Level and Hand, with the smallest XStart is
assigned a Position of 1 for that Level and Hand, the next Insert with that
Level and Hand having the next larger XStart gets a position of 2, etc. I
don't have the <Hand> element until after the template is applied, however,
and can't really derive it, as its assigned via brute-force in the
template. (Also, note that the PartRNumber is optional - if it is not
present, I still create an Insert with a Hand = Right and with an empty
PartNumber, so I always end up with 2 Rails (one Left, one Right) for any
Rail and 2 Inserts (one Left, one Right) for any Insert, and I can assume
that a right-hand and left-hand insert at the same Level will have the same
XStart, and hence, will be assigned the same Position ordinal.) I think
that this would be relatively simple if I applied a second template to the
transformed XML (is it possible to apply a transform to a template w/in the
same XSLT or do you have to do it to the transformed XML afterward?)
Failing that (I'd really rather do it all in one XSLT if possible), my
thought is to build a key based on match=Chassis/Inserts/Insert with
use=concat(Chassis+Inserts+Insert+Level) (to have access to all Inserts for
a particular Level w/in the collection of Inserts for a Chassis), then
for-each through those, sorted by XStart, somehow assigning/storing a
relative Position ordinal to each of those Inserts (possibly in another
key, storing the value of the ordinal (match) and indexing by the
Chassis/Inserts/Insert (use) - is this even possible??) so that I can then
pull the Position ordinal for an Insert when I go to split an Insert into
Left and Right hands. Is this a decent way to go about this? Is there a
better way? How would I setup the second key for this, if this is an
acceptable approach?
I'm pretty new to XSLT - no doubt there's a good way to do this, but its
not currently apparent to me.
I appreciate any help!
Jim Stoll
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list