Display by category...

Z

Zhou Lei

Hello I'm posting a new article about the category. The following XML
segment was posted by Li some days ago and I'm trying to do some more work:

<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>
<location name="Address">
<cdshelf>
<disc name="CCC" cg="pop"/>
<disc name="AAA" cg="pop"/>
<disc name="EEE" cg="rock"/>
<disc name="GGG" cg="pop"/>
</cdshelf>
</location>

<location name="AnotherAddress">
<cdshelf>
<disc name="BBB" cg="classical"/>
<disc name="DDD" cg="pop"/>
<disc name="FFF" cg="classical"/>
</cdshelf>
</location>

</content>
</shop>

The desired web output:

Welcome to myshop

Music CD

Location Address:

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

Rock:
1. EEE

Location AnotherAddress:

Classical music:
1. BBB
2. FFF

Pop music:
1. DDD


The XML file contains 2 locations, and each location has different kinds of CDs
available (some are unavailable, for example, classical CDs are absent at
location "Address"). And the correct CDs should appear according to
its location and the category should be listed in ascendent order. Is it
possible to do it? Thank you.
 
J

Joris Gillis

Tempore 09:16:52 said:
The XML file contains 2 locations, and each location has different kinds of CDs
available (some are unavailable, for example, classical CDs are absent at
location "Address"). And the correct CDs should appear according to
its location and the category should be listed in ascendent order. Is it
possible to do it? Thank you.
Once again, no problem:)

here's one example, using xpath rather than keys.

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

<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:variable name="shelf" select="."/>
<xsl:for-each select="ancestor::shop/category/cd">
<xsl:sort/>
<xsl:if test="$shelf/disc[@cg=current()/@type]">
<h4><xsl:apply-templates/>:</h4>
<ol>
<xsl:apply-templates select="$shelf/disc[@cg=current()/@type]"/>
</ol>
</xsl:if>
</xsl:for-each>
</xsl:template>

<xsl:template match="location">
<h3>Location <xsl:value-of select="@name"/>:</h3>
<xsl:apply-templates/>
</xsl:template>

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

</xsl:stylesheet>

regards,
 
Z

Zhou Lei

Okay I got it. Thank you very much for your help now I can use XSLT better than
before :) And please help me when I have problems again...
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top