Anyone could help me with this category problem?

Discussion in 'XML' started by Li Ming, Feb 28, 2005.

  1. Li Ming

    Li Ming Guest

    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.
    Li Ming, Feb 28, 2005
    #1
    1. Advertising

  2. Li Ming

    Joris Gillis Guest

    Tempore 20:24:12, die Monday 28 February 2005 AD, hinc in foro {comp.text.xml} scripsit Li Ming <>:

    > <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:eek:utput 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-view.cgi?userid=38041)
    Fiat W3C in tenebris
    Joris Gillis, Feb 28, 2005
    #2
    1. Advertising

  3. Li Ming

    Li Ming Guest

    "Joris Gillis" <> дÈëÓʼþ
    news:eek:...
    > Tempore 20:24:12, die Monday 28 February 2005 AD, hinc in foro

    {comp.text.xml} scripsit Li Ming <>:
    >
    > > <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.
    Li Ming, Feb 28, 2005
    #3
  4. Li Ming

    Joris Gillis Guest

    Tempore 23:03:36, die Monday 28 February 2005 AD, hinc in foro {comp.text.xml} scripsit Li Ming <>:

    > 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:eek:utput 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-view.cgi?userid=38041)
    "Et ipsa scientia potestas est" - Francis Bacon , Meditationes sacrae
    Joris Gillis, Mar 1, 2005
    #4
  5. Li Ming

    Li Ming Guest

    > Here's one sample:
    >
    > <xsl:stylesheet version="1.0"

    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    > <xsl:eek:utput 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-view.cgi?userid=38041)
    > Fiat W3C in tenebris


    Thank you again for your help, that helped me a lot. All the problems are
    history now.
    Li Ming, Mar 1, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Hendryx

    Infinite Product Category Display

    Paul Hendryx, Apr 9, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    354
    Paul Hendryx
    Apr 9, 2004
  2. Replies:
    4
    Views:
    389
  3. Zhou Lei

    Display by category...

    Zhou Lei, Mar 2, 2005, in forum: XML
    Replies:
    2
    Views:
    399
    Zhou Lei
    Mar 3, 2005
  4. Steve H
    Replies:
    1
    Views:
    355
    Howard Hinnant
    Apr 19, 2005
  5. lavey
    Replies:
    3
    Views:
    269
Loading...

Share This Page