473,396 Members | 2,037 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.

Anyone could help me with this category problem?

Now I'm learning how to write xsl to transform an xml document. I am trapped
by a problem, and here's the sample xml document:

<category>
<cd type="pop">Pop music</cd>
<cd type="rock">Rock</cd>
<cd type="classical">Classical music</cd>
</category>

<cdshelf>
<disc name="CCC" cg="pop"/>
<disc name="BBB" cg="pop"/>
<disc name="AAA" cg="pop"/>
<disc name="DDD" cg="classical"/>
<disc name="EEE" cg="rock"/>
<disc name="FFF" cg="classcial"/>
<disc name="GGG" cg="pop"/>
</cdshelf>

I want the target html page like this:

Pop music:
1. CCC
2. BBB
3. AAA
4. GGG

Rock:
1. EEE

Classical music:
1. DDD
2. FFF
Can I achieve it using xslt? Thank you.
Jul 20 '05 #1
4 1125
Tempore 20:24:12, die Monday 28 February 2005 AD, hinc in foro {comp.text.xml} scripsit Li Ming <ra*******@eyou.com>:
<category>
<cd type="pop">Pop music</cd>
<cd type="rock">Rock</cd>
<cd type="classical">Classical music</cd>
</category>

<cdshelf>
<disc name="CCC" cg="pop"/>
<disc name="BBB" cg="pop"/>
<disc name="AAA" cg="pop"/>
<disc name="DDD" cg="classical"/>
<disc name="EEE" cg="rock"/>
<disc name="FFF" cg="classcial"/>
<disc name="GGG" cg="pop"/>
</cdshelf>

I want the target html page like this:

Pop music:
1. CCC
2. BBB
3. AAA
4. GGG

Rock:
1. EEE

Classical music:
1. DDD
2. FFF
Can I achieve it using xslt?

absolutely, and in numerous ways.

Here's one sample:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />

<xsl:key name="discByCat" match="cdshelf/disc" use="@cg"/>

<xsl:template match="category">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="cd">
<p>
<h1><xsl:apply-templates/>:</h1>
<ol>
<xsl:apply-templates select="key('discByCat',@type)"/>
</ol>
</p>
</xsl:template>

<xsl:template match="cdshelf"/>

<xsl:template match="disc">
<li>
<xsl:value-of select="@name"/>
</li>
</xsl:template>

</xsl:stylesheet>
regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Fiat W3C in tenebris
Jul 20 '05 #2

"Joris Gillis" <ro**@pandora.be> дÈëÓʼþ
news:op**************@news.pandora.be...
Tempore 20:24:12, die Monday 28 February 2005 AD, hinc in foro {comp.text.xml} scripsit Li Ming <ra*******@eyou.com>:
<category>
<cd type="pop">Pop music</cd>
<cd type="rock">Rock</cd>
<cd type="classical">Classical music</cd>
</category>


Yes it's perfect and thanks for your reply. But if the element of <category>
is the first element in the xml document, and I want it appear in the last
in my html file, what should I do? I mean, the xml is:

<shop name="myshop">
<category>
<cd type="pop">Pop music</cd>
<cd type="rock">Rock</cd>
<cd type="classical">Classical music</cd>
</category>

<welcome>Welcome to myshop</welcome>

<content>
<headline>Music CD</headline>
<cdshelf>
<disc name="CCC" cg="pop"/>
<disc name="BBB" cg="pop"/>
<disc name="AAA" cg="pop"/>
<disc name="DDD" cg="classical"/>
<disc name="EEE" cg="rock"/>
<disc name="FFF" cg="classcial"/>
<disc name="GGG" cg="pop"/>
</cdshelf>
</content>
</shop>

And I want the desired html web page to be:

Welcome to myshop

Music CD

Pop music:
1. CCC
2. BBB
3. AAA
4. GGG

Rock:
1. EEE

Classical music:
1. DDD
2. FFF

Because <category> element appears before <welcome> element, so it is
matched first and displayed before <welcome>. Can I display it in the order
that I like, just as the output above, without modifying the layout of the
xml document (even the element order)? Thank you.
Jul 20 '05 #3
Tempore 23:03:36, die Monday 28 February 2005 AD, hinc in foro {comp.text.xml} scripsit Li Ming <ra*******@eyou.com>:
Because <category> element appears before <welcome> element, so it is
matched first and displayed before <welcome>. Can I display it in the order
that I like, just as the output above, without modifying the layout of the
xml document (even the element order)? Thank you.


Yes, no problem at all.

Give this a try:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />

<xsl:key name="discByCat" match="cdshelf/disc" use="@cg"/>

<xsl:template match="category"/>

<xsl:template match="welcome | headline">
<xsl:element name="h{count(ancestor::*)}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>

<xsl:template match="cdshelf">
<xsl:for-each select="ancestor::shop/category/cd">
<h3><xsl:apply-templates/>:</h3>
<ol>
<xsl:apply-templates select="key('discByCat',@type)"/>
</ol>
</xsl:for-each>
</xsl:template>

<xsl:template match="disc">
<li><xsl:value-of select="@name"/></li>
</xsl:template>

</xsl:stylesheet>
regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
"Et ipsa scientia potestas est" - Francis Bacon , Meditationes sacrae
Jul 20 '05 #4
> Here's one sample:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output indent="yes" />

<xsl:key name="discByCat" match="cdshelf/disc" use="@cg"/>

<xsl:template match="category">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="cd">
<p>
<h1><xsl:apply-templates/>:</h1>
<ol>
<xsl:apply-templates select="key('discByCat',@type)"/>
</ol>
</p>
</xsl:template>

<xsl:template match="cdshelf"/>

<xsl:template match="disc">
<li>
<xsl:value-of select="@name"/>
</li>
</xsl:template>

</xsl:stylesheet>
regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Fiat W3C in tenebris


Thank you again for your help, that helped me a lot. All the problems are
history now.
Jul 20 '05 #5

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

Similar topics

2
by: Gert v O | last post by:
I've posted the following question once: I have a column in a table The first 1, 2 or 3 alphabetic characters of gives the category of the example: ms123 --> category MS f-2345 -->...
14
by: vic | last post by:
My manager wants me to develop a search program, that would work like they have it at edorado.com. She made up her requirements after having compared how search works at different websites, like...
9
by: Peter | last post by:
My problem is the last bit of coding below, the like statement does not work. what I have is a product options field and in it is stored characters i.e. "avcy" etc what the query does is...
3
by: ºa¤Ö | last post by:
multi-level category in database and object design with good performance? current my design like this category { id category name }
0
by: joebob8000 | last post by:
This seems like a simple task, but my 6 year old roots in classic ASP must be causing me trouble with my current problem. I am looking to provide a search for users in which they can select...
10
by: 60325 | last post by:
This is the page where I collect the data in drop-down boxes with values of 1-10 and send it to a submitted page to do calculations. Example: Employee1 TeamScore(1-10) Employee2 ...
3
by: Strasser | last post by:
In a nested subform in datasheet view, an interviewer of homeless people picks a descriptive CATEGORY from 20 descriptive categories. The 20 categories are displayed via a combo box. (Categories...
2
by: milecimm | last post by:
Hello, I need some help to solve the following problem (if it is possible, that's it): I'm using a xpath expression to programatically get data from my xml file. I want to transform ONLY the...
16
by: gnawz | last post by:
I have a pagination function I am using in a file called functions.php as below<? //Pagination functions function getPagingQuery($sql, $itemPerPage = 10) { if (isset($_GET) && (int)$_GET > 0) ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.