categorised product lists: is there a better way?

Subject: categorised product lists: is there a better way?
From: "James Tauber" <jtauber@xxxxxxxxxxx>
Date: Thu, 11 Feb 1999 15:00:22 +0800
I'd like to present a scenario and my current solution and ask if anyone has
    a) a better way of doing under XSL currently
    b) ideas for how XSL could be slightly altered to make this stuff easier

I have an XML file (it doesn't have a prolog so let's assume it's an
external parsed entity) called productlist.xml that lists product
information, along with one or more categories the product fits into:

    <ProductList>
        <Product>
            <Name>widget1</Name>
            <Category>foo</Category>
            <Price>10.00</Price>
        </Product>
        <Product>
            <Name>widget2</Name>
            <Category>bar</Category>
            <Price>15.00</Price>
        </Product>
        <Product>
            <Name>widget3</Name>
            <Category>foo</Category>
            <Category>bar</Category>
            <Price>5.00</Price>
        </Product>
    </ProductList>

I'd like to create multiple web pages, one for each category, with a list of
the products in that category.

I have an XML document for each category. The document for the 'foo'
category might look something like this:

<?xml version="1.0"?>
<!DOCTYPE CategoryPage [
<!ENTITY productlist SYSTEM "productlist.xml">
]>
<CategoryPage>
    <Title>Foos</Title>
    <Desc>Foos are widgets that are good for fooing.</Desc>

    <ProductSelect Type="foo">
        &productlist;
    </ProductSelect>
</CategoryPage>

I then have a stylesheet that gets applied to each category document. It
includes the following:

 <xsl:template match="ProductSelect">
    <xsl:choose>
        <xsl:when test='.[@Type="foo"]'>
            <xsl:apply-templates
select='ProductList/Product[Category="foo"]'/>
        </xsl:when>
        <xsl:when test='.[@Type="bar"]'>
            <xsl:apply-templates
select='ProductList/Product[Category="bar"]'/>
        </xsl:when>
    </xsl:choose>
</xsl:template>

Now, I don't like this approach for a number of reasons, one of which is
that I have to hardcode the categories in the stylesheet. I wasn't able to
parameterize the above template.

1. Is there a better way of approaching the whole scenario?
2. If this is an ok approach overall, can the above template be
parameterized? Can you say "when you encounter elements of type A with
attribute B=C, then apply templates selecting all D in A that have child
element E with content C" for arbitrary C?
3. If it can't be parameterized, should XSL allow it to be?


On a separate but related note, it would be nice if testing could be on
string sort order as well as equality. XSL already has the notion of
sorting, so it should be quite simple to implement. I like to be able to say
things like "select all A with attribute B > C". My particular application
is with dates. I'm using YYYY-MM-DD so that all that is need is string
sorting, no special knowledge of a date format.

James Tauber




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread