Typical sorting and grouping

Subject: Typical sorting and grouping
From: Mike Brown <mike@xxxxxxxx>
Date: Fri, 3 Nov 2000 14:24:26 -0700 (MST)
taherkordy@xxxxxxxxxxxxxxx wrote on xml-dev:
> [...]
>     Suppose we have the following XML file(book.xml)
> [...]
>        <?xml version="1.0"?>
>        <book>
>                <title> Computer Networks </title>
>                <category_id> 2 </category_id>
>                ...
>        </book>

I'm going to interrupt here because you are apparently modeling many book
titles within a single 'book' element. A better schema would be:

  <?xml version="1.0"?>
      <title> Computer Networks </title>
      <category_id> 2 </category_id>

(i.e., multiple 'book' elements in the 'books' document)   

>     I also have a Parameters table and have extracted values
>     for "category" parameter as follows(parameter.xml):
>        <?xml version="1.0"?>
>        <parameter>
>                <category> 
>                        <category_item id="1">Art</category_item>
>                        <category_item id="2">Computer</category_item>
>                        <category_item id="3">Physics</category_item>
>                        ..
>                </category>
>        </parameter>
>     I want to have the following Html file as the result of
>     merging two XML files and an XSL file(what that I want to help me):
>     <html>
>     <body>
>     Title :          
>        <INPUT TYPE='text' NAME='txtTitle' VALUE="Computer Networks" >
>     Category: 
>        <SELECT NAME="category" SIZE=1> 
>                <OPTION value='1'>Art
>                <OPTION value='2' SELECTED>Computer
>                <OPTION value='3'>Physics
>        </SELECT>
>     </body>
>     </html>
>     I have two quietions :
>  1. I think that when reading from Book and Parameter
>     table, I should make an XML file that have information about
>     books and categrory parameters togother.
> [example XML deleted]
>       I want to know that Is it right this idea ?

This is not necessary. You can access multiple source documents by using
the document() function.

>  2. I want to  write an XSL stylesheet for producing this HTML file
>     that automatically selects "computer" option in category Combobox
>     based on value of "category_id" in book.xml and "id"
>     attribute in parameter.xml  

The following stylesheet should illustrate the solution.
I am assuming the following things:
  - primary source tree is coming from books.xml
  - books.xml has each book in its own 'book' element

The complex for-each selection is one way to get the unique categories.  
Another way that is more efficient is to use keys and the Muenchian
method, described in the FAQ.

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

  <xsl:template match="/">
        <form action="formDataProcessorURI">
          <xsl:apply-templates select="books/book"/>

  <xsl:template match="book">
      <xsl:text>Title :</xsl:text>
      <input type="text" name="txtTitle" value="{normalize-space(title)}"/>
      <select name="category" size="1">
        <xsl:variable name="category_id" select="normalize-space(category_id)"/>
        <!-- iterate thru unique categories -->
        <xsl:for-each select="document('parameter.xml')/parameter/category/category_item[not(. = preceding-sibling::category_item)]">
          <option value="{position()}">
            <xsl:if test="@id = $category_id">
              <xsl:attribute name="selected">selected</xsl:attribute>
            <xsl:value-of select="."/>

   - Mike
Mike J. Brown, software engineer at         My XML/XSL resources:
webb.net in Denver, Colorado, USA           http://www.skew.org/xml/

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

Current Thread