473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Unique Records from Unions

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
Jul 20 '05 #1
2 1412
ga*****@aol.com (Gadrin77) writes:
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:output 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>&#x0a;</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

--
Ben Edgington
Mail to the address above is discarded.
Mail to ben at that address might be read.
http://www.edginet.org/
Jul 20 '05 #2
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.
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Beat Scheidiger | last post by:
I do not quite understand this property. Everything is seems clear to me, when I read the corresponding help text. But in practice I have a question: Why are there 3 identical records in the...
3
by: Phil | last post by:
I am looking to set up a hyperlink control on a form to retrieve letters that correspond to a record on a form. That is, there may be 100 form records, and I would like each of those form records...
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
23
by: rohit | last post by:
Hi, In my couple of years of experience, I have never found a single instance where I needed to use unions and bitfields(though I have used structures).I was just imagining where would these find...
2
by: D. Dante Lorenso | last post by:
I'm trying to build a table that will store a history of records by enumerating the records. I want the newest record to always be number ZERO, so I created a trigger on my table to handle the...
2
by: ET | last post by:
Hi! I'll need help on what todo with this information in table... There has to be a unique on one column (Item ID, for inventory purpose) but in many cases that ID is not known... thus, many...
2
by: banderson | last post by:
Hello, I have a data entry form for a table with information about buildings and am having a problem making a combo box do what I want. I would like to make the combo box show a list of unique bldg...
2
by: srusskinyon | last post by:
I need some help getting unique records from our database! I work for a small non-profit homeless shelter. We keep track of guest information as well as what services we have offered for...
11
by: pereges | last post by:
Hello, can some one please guide me a little into using unions. I read about unions in K & R but I am finding it difficult to apply to my problem at hand. I want to save up some space by using...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.