[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] grouping, sorting and selection by language(s)


Subject: Re: [xsl] grouping, sorting and selection by language(s)
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 27 Jan 2005 11:08:39 -0500

Hi,

At 03:45 PM 1/25/2005, you wrote:
I use ASP to transform the following XML (an article
index) and XSL to HTML. The XSL groups the articles by
category, sorts them by date within each category and
only shows two articles per category. That works OK,
but I want to show on one page only the articles in
English, on another page the ones in French, Dutch and
German, and on a third page only the ones in French
and German. How do I do that ?

There's more than one answer to this; which approach is best depends on factors you haven't mentioned. It raises architectural questions about how you want the transform to run.

As you probably know, the usual scenario for XSLT is XML + XSLT => output
(in your case, XML + XSLT => HTML), with a single file in each case. That
is, a single input file with a single stylesheet results in a single output
file.

But you are suggesting you want multiple pages created for the input. This
could be done in either of two ways:

1. Run the stylesheet repeatedly, each time specifying which language you
want. So you'd run once for English, once for French, etc. Each time you'd
create a single page.

2. You'd use an XSLT 1.0 extension, or an XSLT 2.0 feature, to override the
usual behavior and write multiple files on a single run of the stylesheet.
So running the stylesheet will create all the files, one each for every
language you have.

Approach #1 can be achieved by passing in a runtime parameter to identify
your language, and using it in an XPath select expression to filter your
nodes. Approach 2 can be achieved by extending your grouping logic to
accommodate grouping by language before the nodes are subgrouped by category.

Maybe this gives you enough hints to help you forward. If not, let us know
which approach you'd rather take, and someone can be more specific about
how it would done.

Cheers,
Wendell

<?xml version="1.0" encoding="ISO-8859-1"?>
<articles>
<article lang="fr">
<category>Catigorie 2</category>
<source>Publication in French</source>
<date pubdate="20050124">24/01/2004</date>
<art_title>Title of the first article in
French</art_title>
<firstpara>Text of the first article in
French.</firstpara>
<link>http://www.anyurl.com/articles/20050124-fr.asp</link>
</article>
<article lang="en">
<category>Category 3</category>
<source>English-language publication</source>
<date pubdate="20041221">21/12/2004</date>
<art_title>Title of the first article in
English</art_title>
<firstpara>Text of the first article in
English.</firstpara>
<link>http://www.anyurl.com/articles/20041221-en.asp</link>
</article>
<article lang="de">
<category>Kategorie 1</category>
<source>German-language publication</source>
<date pubdate="20040724">24/07/2004</date>
<art_title>Title of the first article in
German</art_title>
<firstpara>Text of the first article in
German.</firstpara>
<link>http://www.anyurl.com/articles/20040724-de.asp</link>
</article>
<article lang="fr">
<category>Catigorie 2</category>
<source>Publication in French</source>
<date pubdate="20040302">02/03/2004</date>
<art_title>Title of the second article in
French</art_title>
<firstpara>Text of the second article in
French.</firstpara>
<link>http://www.anyurl.com/articles/20040302-fr.asp</link>
</article>
<article lang="nl">
<category>Categorie 1</category>
<source>Dutch-language publication</source>
<date pubdate="20041119">19/11/2004</date>
<art_title>Title of the first article in
Dutch</art_title>
<firstpara>Text of the first article in
Dutch.</firstpara>
<link>http://www.anyurl.com/articles/20041119-nl.asp</link>
</article>
<article lang="de">
<category>Kategorie 3</category>
<source>German-language publication</source>
<date pubdate="20031224">24/12/2003</date>
<art_title>Title of the second article in
German</art_title>
<firstpara>Text of the second article in
German.</firstpara>
<link>http://www.anyurl.com/articles/20031224-de.asp</link>
</article>
<article lang="nl">
<category>Categorie 2</category>
<source>Dutch-language publication</source>
<date pubdate="20040919">19/09/2004</date>
<art_title>Title of the second article in
Dutch</art_title>
<firstpara>Text of the second article in
Dutch.</firstpara>
<link>http://www.anyurl.com/articles/20040919-nl.asp</link>
</article>
<article lang="en">
<category>Category 1</category>
<source>English-language publication</source>
<date pubdate="20040621">21/06/2004</date>
<art_title>Title of the second article in
English</art_title>
<firstpara>Text of the second article in
English.</firstpara>
<link>http://www.anyurl.com/articles/20040621-en.asp</link>
</article>
<article lang="de">
<category>Kategorie 3</category>
<source>German-language publication</source>
<date pubdate="20041029">29/10/2004</date>
<art_title>Title of the third article in
German</art_title>
<firstpara>Text of the third article in
German.</firstpara>
<link>http://www.anyurl.com/articles/20041029-de.asp</link>
</article>
<article lang="fr">
<category>Catigorie 3</category>
<source>Publication in French</source>
<date pubdate="20030320">20/03/2003</date>
<art_title>Title of the third article in
French</art_title>
<firstpara>Text of the third article in
French.</firstpara>
<link>http://www.anyurl.com/articles/20030320-fr.asp</link>
</article>
<article lang="en">
<category>Category 2</category>
<source>English-language publication</source>
<date pubdate="20030512">12/05/2003</date>
<art_title>Title of the third article in
English</art_title>
<firstpara>Text of the third article in
English.</firstpara>
<link>http://www.anyurl.com/articles/20030512-en.asp</link>
</article>
<article lang="nl">
<category>Categorie 3</category>
<source>Dutch-language publication</source>
<date pubdate="20031017">17/10/2003</date>
<art_title>Title of the third article in
Dutch</art_title>
<firstpara>Text of the third article in
Dutch.</firstpara>
<link>http://www.anyurl.com/articles/20031017-nl.asp</link>
</article>
</articles>

The XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method ="html" encoding="ISO-8859-1" />
<xsl:key name ="articles-by-category" match ="article"
use ="category" />
<xsl:template match="articles">
<xsl:for-each select
="article[count(.|key('articles-by-category',
category)[1])=1]">
<xsl:sort select="category" />
<h2><xsl:value-of select="category" /></h2><br/>
<xsl:for-each select="key('articles-by-category',
category)">
<xsl:sort select="date/@pubdate" order="descending"/>
<xsl:if test="position() &lt;3">
<i><xsl:value-of select="source"/> - <xsl:value-of
select="date" /></i><br/>
<h4><xsl:value-of select="art_title"/></h4>
<xsl:value-of select="firstpara"/> ..
<a><xsl:attribute name="href">
<xsl:value-of select="link"/>
</xsl:attribute>read full article</a><br/><br/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>



======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


Current Thread
Keywords