xsl-list
[Top] [All Lists]

RE: XSLT Problem - Random Order

2003-10-06 09:24:20
Hi Saurabh,
  From the XSL you have written, it seems to me you
are expecting the output to appear as c4 first, then
c6 and then c2. 

  You are trying to achieve this by *textual ordering
of xsl:when in your XSLT*. But xsl:when will not
evaluate in the order they are written in XSLT. But
instead, a particular *xsl:when will evaluate to true,
when the XML node will pass the test against that
xsl:when*. 

When you do xsl:for-each select="//book" , the XSLT
processor will read <book> tags in order c1, c2, c3,
c4, c5 and c6. When the processor encounters <book
category="c2"> , the 3rd xsl:when in your XSLT will
evaluate to true. Similarly, the processor will
process XML elements c4 and c6. 

  The question still is a bit ambiguous, and answers
till now seem to address the problem asked. If you can
explain more clearly, some body can provide the right
solution.

Regards,
Mukul


--- Saurabh Sinha <i_am_saurabhsinha(_at_)yahoo(_dot_)co(_dot_)in>
wrote:
Hi,

Thanks for your mail. What you have mentioned about
ascending/descending order that thing I know. If I
write the statement 

<xsl:sort select="@category" order="descending"/>
then
ouput will come as category 6, 5 , 4, 3......

For ascending it will come as category c1, c2,
c3.......

But I want to select the category at random order
not
as ascending or descending order. Suppose there are
6
categories in 

books.xml file. Now I want to select only 3
categories
in ouput file ( e.g in html) and their position will
be different - 

such as 4th (category="c4" in xml doc) will come at
1st position (in html doc), 6th (category="c6" in
xml
doc) will come at 

2nd position (in html doc) and 2nd (category="c2" in
xml doc) will come at 3rd position(in html doc).


books.xml
----------

<?xml version="1.0"?>
<books>
      <book category="c1">
              <title>1st Book</title>
              <auth>Author 1</auth>
      </book>

      <book category="c2">
              <title>2nd Book</title>
              <auth>Author 2</auth>
      </book>
      <book category="c3">
              <title>3rd Book</title>
              <auth>Author 3</auth>
      </book>
      <book category="c4">
              <title>4th Book</title>
              <auth>Author 4</auth>
      </book>
      <book category="c5">
              <title>5th Book</title>
              <auth>Author 5</auth>
      </book>
      <book category="c6">
              <title>6th Book</title>
              <auth>Author 6</auth>
      </book>
</books>



books.xsl
----------

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

      <xsl:template match="/">
              <html>
                      <head>
                      </head>
                      <body>
                              <table border="1" width="60%" align="center"
cellpadding="0">
                                      <xsl:apply-templates/>
                              </table>
                      </body>
              </html>
      </xsl:template>


      <xsl:template match="books">
              <xsl:for-each select="//book">


                      <xsl:choose>

                              <xsl:when test="@category='c4'">

                                      <tr>
                                              <td align="center">
                                                      <xsl:value-of 
select="title"/>
                                              </td>
                                              <td align="center">
                                                      <xsl:value-of 
select="auth"/>
                                              </td>
                                      </tr>
                              </xsl:when>


                              <xsl:when test="@category='c6'">

                                      <tr>
                                              <td align="center">
                                                      <xsl:value-of 
select="title"/>
                                              </td>
                                              <td align="center">
                                                      <xsl:value-of 
select="auth"/>
                                              </td>
                                      </tr>
                              </xsl:when>


                              <xsl:when test="@category='c2'">
                                      <tr>
                                              <td align="center">
                                                      <xsl:value-of 
select="title"/>
                                              </td>
                                              <td align="center">
                                                      <xsl:value-of 
select="auth"/>
                                              </td>
                                      </tr>
                              </xsl:when>
                      </xsl:choose>
              </xsl:for-each>
      </xsl:template>
</xsl:stylesheet>



books.html
----------

Current Output
--------------


Category              Author
--------              -------

2nd Book              Author 2

4th Book              Author 4

6th Book              Author 6


Expected Output 
---------------


Category              Author
--------              -------

4th Book              Author 4

6th Book              Author 6

2nd Book              Author 2



Regards,

Saurabh Sinha











 --- Michael Kay <mhk(_at_)mhk(_dot_)me(_dot_)uk> wrote: > You
haven't
said what criteria you want to use for
sorting - is it
descending order by category name, or what?

You could get the order c3, c2, c1, by adding

<xsl:sort select="@category" order="descending"/>

immediately after the <xsl:for-each>

You have an xsl:choose with three branches, but
they
are all the same.
This makes it very difficult to understand what
exactly you are trying
to achieve.

=== message truncated ===


__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

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



<Prev in Thread] Current Thread [Next in Thread>