Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dinstinct Values

Status
Not open for further replies.

tokoh

Programmer
May 23, 2001
13
0
0
AU
I have the following XML....

<movie>
<title>Braveheart</title>
<star>Mel Gibson</star>
<studio>Universal</studio>
<ticketsales>$700,000,000</ticketsales>
</movie>
<movie>
<title>Payback</title>
<star>Mel Gibson</star>
<studio>Universal</studio>
<ticketsales>$122,000,000</ticketsales>
</movie>
<movie>
<title>Ransom</title>
<star>Mel Gibson</star>
<studio>Universal</studio>
<ticketsales>$270,000,000</ticketsales>
</movie>
<movie>
<title>Galdiator</title>
<star>Russell Crowe</star>
<studio>Universal</studio>
<ticketsales>$340,000,000</ticketsales>
</movie>
<movie>
<title>Proof</title>
<star>Russell Crowe</star>
<studio>Universal</studio>
<ticketsales>$550,000</ticketsales>
</movie>

I want to create a style sheet which does the following...
Mel Gibson
Braveheart Universal, $700,000,000
Ransom Universal, $122,000,000
Payback Universal, $270,000,000

Russell Crowe
Galdiator Universal, $340,000,000
Proof Universal, $550,000

Thus I want to know how to search elements and select the distinct values (Russell Crowe and Mel Gibson). Is this possible using XSL?
 
Hello,
Short answer: Yes, it is possible.

Long answer: The code and some explanations.
It will be long post, so please be patient.

First I would recommend reading an article of representing database in XML:

I suppose you are familiar with relational databases and SQL, because you are asking for distinct values.

If your task was to design a database for movies, there would be at least 2 tables - stars and movies (may be third one studios):

table stars
===========
starid name
1 Mel Gibson
2 Russel Crowe


table movies
============
movieid title starid studio ticketsales
1 Braveheart 1 Universal $700,000,000
2 Payback 1 Universal $122,000,000
3 Ransom 1 Universal $270,000,000
4 Gladiator 2 Universal $340,000,000
5 Proof 2 Universal $550,000

For that tables (or similar :) I'm sure you know how to make a desired query.

The problem is that these two tables with their relationships - primary key starid in table stars as foreign key in moves is presented in the following way in XML.
(I added root element movies and where needed attributes, also removed recurring info.)

<movies>
<movie movieid=&quot;1&quot; starid=&quot;1&quot;>
<title>Braveheart</title>
<studio>Universal</studio>
<ticketsales>$700,000,000</ticketsales>
</movie>
<movie movieid=&quot;2&quot; starid=&quot;1&quot;>
<title>Payback</title>
<studio>Universal</studio>
<ticketsales>$122,000,000</ticketsales>
</movie>
<movie movieid=&quot;3&quot; starid=&quot;1&quot;>
<title>Ransom</title>
<studio>Universal</studio>
<ticketsales>$270,000,000</ticketsales>
</movie>
<movie movieid=&quot;4&quot; starid=&quot;2&quot;>
<title>Gladiator</title>
<studio>Universal</studio>
<ticketsales>$340,000,000</ticketsales>
</movie>
<movie movieid=&quot;5&quot; starid=&quot;2&quot;>
<title>Proof</title>
<studio>Universal</studio>
<ticketsales>$550,000</ticketsales>
</movie>
<stars>
<star>
<id>1</id>
<name>Mel Gibson</name>
</star>
<star>
<id>2</id>
<name>Russel Crowe</name>
</star>
</stars>
</movies>

In order to do your task in XSL you have to know what xsl:key do.
&quot;The <xsl:key> element declares a named key for use with the key() function in XPath expressions for enabling easier access into complex XML documents. Keys are top-level elements, which means they cannot appear within a template.
The <xsl:key> element provides an alternative mechanism for addressing elements within an XML document. In effect, keys create a directory of specific elements from an XML source document, with the values generated from the use expressions referring to those elements. The primary difference between a key and an id is that an id must be a fixed qualified name, while a key can be created from either one or several concatenated expressions.&quot; (definition)

I'm sure you can find more resources on xsl:key and key().

So, the proposed XSL is:

<xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot; <xsl:eek:utput method=&quot;html&quot;/>
<xsl:key name=&quot;star-movie&quot; match=&quot;movie&quot; use=&quot;@starid&quot;/>
<xsl:template match=&quot;/&quot;>
<html>
<body>
<xsl:for-each select=&quot;//star&quot;>
<p>
<xsl:value-of select=&quot;name&quot;/>
<br/>
<xsl:for-each select=&quot;key('star-movie', id)&quot;>
<xsl:value-of select=&quot;title&quot;/>, <xsl:value-of select=&quot;studio&quot;/> - <xsl:value-of select=&quot;ticketsales&quot;/>
<br/>
</xsl:for-each>
</p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

So
<xsl:key name=&quot;star-movie&quot; match=&quot;movie&quot; use=&quot;@starid&quot;/>
creates key on attribute starid, which has values 1 and 2.
Then you could figure out that for each star we display name, then with function key() we extract all info we need.

Hope this helps.
D.
 
Love your work, dianal.
Thank you!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top