Identify position of context node within another nodeset

nach

New Member
I have an XML document that looks like this:\[code\]<Root> <Cover> <Cover_Ref Val="1"/> <Cover_Code Val="Food in transit"/> <AdditionalCover> <AdditionalCover_Area Val="Sussex"/> </AdditionalCover> </Cover> <Cover> <Cover_Ref Val="2"/> <Cover_Code Val="Frozen goods"/> </Cover> <Cover> <Cover_Ref Val="3"/> <Cover_Code Val="Business equipment"/> <AdditionalCover> <AdditionalCover_Area Val="Sussex"/> </AdditionalCover> </Cover> <AdditionalCoverResult> <CoverArea Val="Sussex"/> <CoverExcluded Val="N"/> </AdditionalCoverResult> <AdditionalCoverResult> <CoverArea Val="Sussex"/> <CoverExcluded Val="Y"/> </AdditionalCoverResult></Root>\[/code\]I need to transform it into something like this:\[code\]<Insurances> <Insurance> <reference>1</reference> <CoverType>Food in transit</CoverType> <Region>Sussex</Region> <Excluded>N</Excluded> </Insurance> <Insurance> <reference>2</reference> <CoverType>Frozen goods</CoverType> </Insurance> <Insurance> <reference>3</reference> <CoverType>Business equipment</CoverType> <Region>Sussex</Region> <Excluded>Y</Excluded> </Insurance></Insurances>\[/code\]The AdditionalCoverResult elements refer to Cover elements that contain AdditionalCover elements - so the first Cover element has its cover not excluded, while the third Cover element has its cover excluded. The second cover element does not have AdditionalCover, so there is no AdditionalCoverResult element that refers to it.My problem is that within an \[code\]<xsl:for-each select="//Cover">\[/code\] loop I can't think of a way of identifying which AdditionalCoverResult element I should be looking for. Using \[code\]position()\[/code\] within the for-each ends up with me incorrectly outputting AdditionalCoverResult information for a Cover element that doesn't have AdditionalCover.I have to preserve the original ordering of Cover, so I can't do two separate loops, one for Cover with AdditionalCover and one for Cover without AdditionalCover.Within the nodeset returned by \[code\]//Cover\[/code\] is there a way I can identify the context node's position within the nodeset returned by \[code\]//Cover[AdditionalCover]\[/code\]?I'm sort of guessing that for the Cover element I need to think "What index am I within \[code\]//Cover[AdditionalCover]\[/code\] so that I can use that index to identify my corresponding AdditionalCoverResult element?"
 
Back
Top