Unique Records from Unions

G

Gadrin77

is it possible to get a unique ID or key for a union of the following XML

<Bonus>
<BonusItem Name="Category One" Value="1"/>
<BonusItem Name="Category One" Value="1"/>
<BonusItem Name="Category Two" Value="2"/>
<BonusItem Name="Category One" Value="1"/>
<BonusItem Name="Category Two" Value="2"/>
<BonusItem Name="Category Six" Value="6"/>

<OtherItem Name="Category One" Value="1"/>
<OtherItem Name="Category Two" Value="2"/>
<OtherItem Name="Category Six" Value="6"/>
</Bonus>


I want to get a summary that looks like:

Category One: 4
Category Two: 6
Category Six: 12

I can select both using select="/Bonus/BonusItem | /Bonus/OtherItem"

<xsl:key match="BonusItem|OtherItem" use="@Name">

but the match= attribute in the <xsl:key> didn't seem to care about the
union operator inside it and still listed the OtherItem @Names separately.

Is it possible to combine them into a single, unique list, or do I have to
create a new set of XML from the above and then make a unique list from
that?

I'm using MS XML 4 SP2
 
B

Ben Edgington

is it possible to get a unique ID or key for a union of the following XML

<Bonus>
<BonusItem Name="Category One" Value="1"/>
<BonusItem Name="Category One" Value="1"/>
<BonusItem Name="Category Two" Value="2"/>
<BonusItem Name="Category One" Value="1"/>
<BonusItem Name="Category Two" Value="2"/>
<BonusItem Name="Category Six" Value="6"/>

<OtherItem Name="Category One" Value="1"/>
<OtherItem Name="Category Two" Value="2"/>
<OtherItem Name="Category Six" Value="6"/>
</Bonus>


I want to get a summary that looks like:

Category One: 4
Category Two: 6
Category Six: 12

I can select both using select="/Bonus/BonusItem | /Bonus/OtherItem"

<xsl:key match="BonusItem|OtherItem" use="@Name">

but the match= attribute in the <xsl:key> didn't seem to care about the
union operator inside it and still listed the OtherItem @Names separately.

Perhaps I haven't understood the problem, but this works for me:

This transformation
- - -
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
<xsl:eek:utput omit-xml-declaration="yes"/>

<xsl:key match="BonusItem|OtherItem" use="@Name" name="item-key"/>

<xsl:template match="/">

<xsl:for-each select="//@Name">
<!-- Only process the item if it's
the first time we've seen the Name -->
<xsl:if test="generate-id() = generate-id(key('item-key',.)[1]/@Name)">
<xsl:value-of select="."/>
<xsl:text>: </xsl:text>
<xsl:value-of select="sum(key('item-key',.)/@Value)"/>
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>
- - -

with your input XML gives

- - -
Category One: 4
Category Two: 6
Category Six: 12

- - -

Is that what you wanted?

Ben
 
G

Gadrin77

schnip...
with your input XML gives

- - -
Category One: 4
Category Two: 6
Category Six: 12

- - -

Is that what you wanted?

Ben

Yes, thanks again. I'm still learning all these functions and using your
example as a starting point I was able to convert my code.

Thanks a bunch.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top