473,785 Members | 2,794 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help working with distinct records in an xml doc

Good Morning,
Sorry for xposting. Just need a liitle help.
I have an xml file that's generated from a database. How do I select
distinct values from a field in xslt and then loop through the records and
produce output. Example

<registration s>
<registration >
<company>Awesom e Printers</company>
<first>John</first>
<last>Smith</last>
</registration>
<registration >
<company>Awesom e Printers</company>
<first>Bob</first>
<last>Roberts </last>
</registration>
<registration >
<company>Awesom e Printers</company>
<first>John</first>
<last>Johnson </last>
</registration>
<registration >
<company>Othe r Company</company>
<first>Tom</first>
<last>Thomas</last>
</registration>
<registration >
<company>Othe r Company</company>
<first>Dan</first>
<last>Daniels </last>
</registration>
</registrations>
+++++++++++++++ +++++++++++++++ +++++++++++++++ +++
I want the out put to be something like this

<table>
<tr>
<td>Company Name</td>
<td>Employees </td>
</tr>
<tr>
<td>Awesome Printers</td>
<td>John Smith<br/>
Bob Roberts<br/>
John Johnson<br/></td>
</tr>
<tr>
<td>Other Company</td>
<td>Tom Thomas<br/>
Dan Daniels<br/></td>
</tr>
</table>

Effectively writing unique company names once and then putting only the
employees from that company into the employee table cell.

Thanks for any advise.
Regards,
Chris

Aug 24 '05 #1
5 1538
Chris Kettenbach wrote:
Good Morning,
Sorry for xposting. Just need a liitle help.
I have an xml file that's generated from a database. How do I select
distinct values from a field
XML doesn't have fields -- the database did but this isn't a database any
more. In XML they're called elements (they've got a lot in common with
fields but they ain't the same).
in xslt and then loop through the records and
produce output.
It looks like you need to process the information grouped by company name,
and there's a technique in XSLT 1.0 for doing this called Muenchian grouping
(it won't be needed in XSLT 2 because that has a built-in group-processing
command, but XSLT 2 is not a Recommendation yet).

Check the XSLT FAQ for "Muenchian" to find examples.

///Peter

[Group and followup corrected to comp.text.xml]
Example

<registration s>
<registration >
<company>Awesom e Printers</company>
<first>John</first>
<last>Smith</last>
</registration>
<registration >
<company>Awesom e Printers</company>
<first>Bob</first>
<last>Roberts </last>
</registration>
<registration >
<company>Awesom e Printers</company>
<first>John</first>
<last>Johnson </last>
</registration>
<registration >
<company>Othe r Company</company>
<first>Tom</first>
<last>Thomas</last>
</registration>
<registration >
<company>Othe r Company</company>
<first>Dan</first>
<last>Daniels </last>
</registration>
</registrations>
+++++++++++++++ +++++++++++++++ +++++++++++++++ +++
I want the out put to be something like this

<table>
<tr>
<td>Company Name</td>
<td>Employees </td>
</tr>
<tr>
<td>Awesome Printers</td>
<td>John Smith<br/>
Bob Roberts<br/>
John Johnson<br/></td>
</tr>
<tr>
<td>Other Company</td>
<td>Tom Thomas<br/>
Dan Daniels<br/></td>
</tr>
</table>

Effectively writing unique company names once and then putting only the
employees from that company into the employee table cell.


If you scrolled down this far then you've been rewarded :-)

The trick is to use lookup keys to test the selected group of elements
against, in order to pick only the first ocurrence in each group; then
output the group header at that point; and only then go and process all the
elements in the group to extract the detail from each of them.

The syntax is tricky because it's very compact. It selects all registration
elements, but subjects each of them to the test that [the number of nodes in
the union of (the current node and the first node with the current company
value returned by the lookup) is equal to 1] -- in effect meaning they are
one and the same node, ie the first in their company name grouping. You'll
probably need to read that 3 or 4 times -- it took me a week to grok it.

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="html"/>

<xsl:key name="entry" match="registra tion" use="company"/>

<xsl:template match="/">
<html>
<head>
<title>Registra tions</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="registra tions">
<table>
<xsl:for-each select="registr ation[count(.|key('en try',company
[1])=1]">
<xsl:sort select="company "/>
<tr>
<th valign="top">
<xsl:value-of select="company "/>
</th>
<td valign="top">
<xsl:for-each select="key('en try',current()/company)">
<xsl:sort select="last"/>
<xsl:value-of select="first"/>
<xsl:text> </xsl:text>
<xsl:value-of select="last"/>
<xsl:if test="position( )!=last()">
<br/>
</xsl:if>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>

</xsl:stylesheet>

///Peter

Aug 24 '05 #2
Peter Flynn <pe********@m.s ilmaril.ie> writes:
XML doesn't have fields -- the database did but this isn't a
database any more. In XML they're called elements


Sometimes it might be appropriate to use attributes.

Aug 24 '05 #3
Hi Peter,
I get error when processing the stylesheet. It errors here.

<xsl:for-each select="registr ation[count(.|key('en try',company
[1])=1]">

specifically:

Expression does not return a DOM node.
registration[-->count(.|key('e ntry',company[1])=1]<--

Ideas?
Thanks,
Chris

"Peter Flynn" <pe********@m.s ilmaril.ie> wrote in message
news:3n******** *****@individua l.net...
Chris Kettenbach wrote:
Good Morning,
Sorry for xposting. Just need a liitle help.
I have an xml file that's generated from a database. How do I select
distinct values from a field


XML doesn't have fields -- the database did but this isn't a database any
more. In XML they're called elements (they've got a lot in common with
fields but they ain't the same).
in xslt and then loop through the records and
produce output.


It looks like you need to process the information grouped by company name,
and there's a technique in XSLT 1.0 for doing this called Muenchian
grouping
(it won't be needed in XSLT 2 because that has a built-in group-processing
command, but XSLT 2 is not a Recommendation yet).

Check the XSLT FAQ for "Muenchian" to find examples.

///Peter

[Group and followup corrected to comp.text.xml]
Example

<registration s>
<registration >
<company>Awesom e Printers</company>
<first>John</first>
<last>Smith</last>
</registration>
<registration >
<company>Awesom e Printers</company>
<first>Bob</first>
<last>Roberts </last>
</registration>
<registration >
<company>Awesom e Printers</company>
<first>John</first>
<last>Johnson </last>
</registration>
<registration >
<company>Othe r Company</company>
<first>Tom</first>
<last>Thomas</last>
</registration>
<registration >
<company>Othe r Company</company>
<first>Dan</first>
<last>Daniels </last>
</registration>
</registrations>
+++++++++++++++ +++++++++++++++ +++++++++++++++ +++
I want the out put to be something like this

<table>
<tr>
<td>Company Name</td>
<td>Employees </td>
</tr>
<tr>
<td>Awesome Printers</td>
<td>John Smith<br/>
Bob Roberts<br/>
John Johnson<br/></td>
</tr>
<tr>
<td>Other Company</td>
<td>Tom Thomas<br/>
Dan Daniels<br/></td>
</tr>
</table>

Effectively writing unique company names once and then putting only the
employees from that company into the employee table cell.


If you scrolled down this far then you've been rewarded :-)

The trick is to use lookup keys to test the selected group of elements
against, in order to pick only the first ocurrence in each group; then
output the group header at that point; and only then go and process all
the
elements in the group to extract the detail from each of them.

The syntax is tricky because it's very compact. It selects all
registration
elements, but subjects each of them to the test that [the number of nodes
in
the union of (the current node and the first node with the current company
value returned by the lookup) is equal to 1] -- in effect meaning they are
one and the same node, ie the first in their company name grouping. You'll
probably need to read that 3 or 4 times -- it took me a week to grok it.

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="html"/>

<xsl:key name="entry" match="registra tion" use="company"/>

<xsl:template match="/">
<html>
<head>
<title>Registra tions</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="registra tions">
<table>
<xsl:for-each select="registr ation[count(.|key('en try',company
[1])=1]">
<xsl:sort select="company "/>
<tr>
<th valign="top">
<xsl:value-of select="company "/>
</th>
<td valign="top">
<xsl:for-each select="key('en try',current()/company)">
<xsl:sort select="last"/>
<xsl:value-of select="first"/>
<xsl:text> </xsl:text>
<xsl:value-of select="last"/>
<xsl:if test="position( )!=last()">
<br/>
</xsl:if>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>

</xsl:stylesheet>

///Peter

Aug 24 '05 #4
hint: from performed elements you have to create attributtes in yor new
element, because there isn't posibility to being few the same attributtes in
one xml element

td
Aug 25 '05 #5
Chris Kettenbach wrote:
Hi Peter,
I get error when processing the stylesheet. It errors here.

<xsl:for-each select="registr ation[count(.|key('en try',company
[1])=1]">

specifically:

Expression does not return a DOM node.
registration[-->count(.|key('e ntry',company[1])=1]<--

Ideas?
What processor are you using? I ran it through Saxon without errors, and the
output was:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Registra tions</title>
</head>
<body>
<table>
<tr>
<th valign="top">Aw esome Printers</th>
<td valign="top">Jo hn Johnson<br>Bob Roberts<br>John Smith
</td>
</tr>
<tr>
<th valign="top">Ot her Company</th>
<td valign="top">Da n Daniels<br>Tom Thomas
</td>
</tr>
</table>
</body>
</html>

///Peter
"Peter Flynn" <pe********@m.s ilmaril.ie> wrote in message
news:3n******** *****@individua l.net...
Chris Kettenbach wrote:
Good Morning,
Sorry for xposting. Just need a liitle help.
I have an xml file that's generated from a database. How do I select
distinct values from a field


XML doesn't have fields -- the database did but this isn't a database any
more. In XML they're called elements (they've got a lot in common with
fields but they ain't the same).
in xslt and then loop through the records and
produce output.


It looks like you need to process the information grouped by company
name, and there's a technique in XSLT 1.0 for doing this called Muenchian
grouping
(it won't be needed in XSLT 2 because that has a built-in
group-processing command, but XSLT 2 is not a Recommendation yet).

Check the XSLT FAQ for "Muenchian" to find examples.

///Peter

[Group and followup corrected to comp.text.xml]
Example

<registration s>
<registration >
<company>Awesom e Printers</company>
<first>John</first>
<last>Smith</last>
</registration>
<registration >
<company>Awesom e Printers</company>
<first>Bob</first>
<last>Roberts </last>
</registration>
<registration >
<company>Awesom e Printers</company>
<first>John</first>
<last>Johnson </last>
</registration>
<registration >
<company>Othe r Company</company>
<first>Tom</first>
<last>Thomas</last>
</registration>
<registration >
<company>Othe r Company</company>
<first>Dan</first>
<last>Daniels </last>
</registration>
</registrations>
+++++++++++++++ +++++++++++++++ +++++++++++++++ +++
I want the out put to be something like this

<table>
<tr>
<td>Company Name</td>
<td>Employees </td>
</tr>
<tr>
<td>Awesome Printers</td>
<td>John Smith<br/>
Bob Roberts<br/>
John Johnson<br/></td>
</tr>
<tr>
<td>Other Company</td>
<td>Tom Thomas<br/>
Dan Daniels<br/></td>
</tr>
</table>

Effectively writing unique company names once and then putting only the
employees from that company into the employee table cell.


If you scrolled down this far then you've been rewarded :-)

The trick is to use lookup keys to test the selected group of elements
against, in order to pick only the first ocurrence in each group; then
output the group header at that point; and only then go and process all
the
elements in the group to extract the detail from each of them.

The syntax is tricky because it's very compact. It selects all
registration
elements, but subjects each of them to the test that [the number of nodes
in
the union of (the current node and the first node with the current
company value returned by the lookup) is equal to 1] -- in effect meaning
they are one and the same node, ie the first in their company name
grouping. You'll probably need to read that 3 or 4 times -- it took me a
week to grok it.

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="html"/>

<xsl:key name="entry" match="registra tion" use="company"/>

<xsl:template match="/">
<html>
<head>
<title>Registra tions</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="registra tions">
<table>
<xsl:for-each select="registr ation[count(.|key('en try',company
[1])=1]">
<xsl:sort select="company "/>
<tr>
<th valign="top">
<xsl:value-of select="company "/>
</th>
<td valign="top">
<xsl:for-each select="key('en try',current()/company)">
<xsl:sort select="last"/>
<xsl:value-of select="first"/>
<xsl:text> </xsl:text>
<xsl:value-of select="last"/>
<xsl:if test="position( )!=last()">
<br/>
</xsl:if>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>

</xsl:stylesheet>

///Peter


Aug 25 '05 #6

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

Similar topics

5
6265
by: Ralph Freshour | last post by:
I have a question about the following PHP script - I got it off a web site tutorial on how to count users logged into your site - my question is the $PHP_SELF variable - it writes the name of the web page to the 'file' field in the table - I don't understand why it is doing that - I mean, isn't the SELECT DISTINCT statement only pulling those records from that one web page? I guess I just don't follow what it is doing with that SELECT...
2
4706
by: berthelot samuel | last post by:
Hi everyone, I am currently trying to write a report based on a View of SQL Server. Basically, I have 3 tables : Hardware, SoftwareInstalled and Software with SoftwareInstalled that keeps track of all the software installed on each piece of hardware by referencing the primary keys of each table. So now, I have a request that retrieve information from those 3 tables giving a list of all the hardware with their details + the software...
4
10237
by: Surendra | last post by:
I have this query that I need to use in an Update statement to populate a field in the table by the value of Sq ---------------------------------------------------------------------------- Inline View Query: Select Sq from ( Select substr(to_date(End_Date,"DD-MON-YYYY"),4), End_Date, Rank() Over (Partition by substr(to_date(End_Date,"DD-MON-YYYY"),4) Order by End_Date) As Sq
6
4354
by: Toucan | last post by:
i need to retrieve the most recent timestamped records with unique names (see working query below) what i'm having trouble with is returning the next-most-recent records (records w/ id 1 and 3 in this example) i also need to return the 3rd most recent, 4th, 5th and 6th most recent - i figure if i can get the 2nd working, 3rd, 4th, etc will be cake thanks,
48
3880
by: phillip.s.powell | last post by:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't ask) I have database Spring with table Students I have database Summer with table Students I am tasked to produce a query of all students in both tables with no duplicates. No clue whatsoever.
0
3182
by: hahahardididi | last post by:
Hi Forums, I have a frustrating problem on my Stored Procedure. It can only proccess about 100 records in 10 minutes. I have 2 million initial records that need to processed. Meaning that with this speed i will around 200 days to finish all of them. To make it worse, the data itself grows at least another 100 records per hour. Really appreciated if anybody can help to speed this up. Rgds/Hardi
15
2581
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to determine who needs to receive the text message then send the message to the address. Only problem is, the employee may receive up to 4 of the same messages because each thread gets the recors then sends the message. I need somehow to prevent...
1
2799
by: Webstorm | last post by:
Hi, I hope someone can help me sort this out a bit, Im completely lost. Here is the page I am working on: http://www.knzbusinessbrokers.com/default.asp I have 3 search critera that I need to use when querying the database. Right now it is only looking for a match on one of those dropdowns and not all 3. can anyone help? Here is the code: <form BOTID="0" METHOD="POST" action="businessforsale_interface/Results/test3.asp">
1
3736
by: RussCRM | 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 statistical purposes. I've been using Here’s the situation: I have two main tables: Guest (stores data such as GuestID, First Name, Last Name, etc.) and Services (stores data such as the type of service the guest used (Shelter Bed, Lunch, Dinner,...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10152
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8974
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.