xsl-list
[Top] [All Lists]

Re: html table of sorted element names

2003-10-31 02:28:25
I cannot reproduce your problem at all:

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns:exsl="http://exslt.org/common";
 exclude-result-prefixes="exsl"

  <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="/db">
    <xsl:if test="1">
      <xsl:variable name="sortedelements">
        <xsl:for-each select="*">
          <xsl:sort data-type="text" select="name()"/>
          <xsl:copy-of select="."/>
        </xsl:for-each>
      </xsl:variable>
      <xsl:variable name="sortedelementsNodeSet"
                    select="exsl:node-set($sortedelements)/*"/>
      <table>
        <xsl:for-each select="$sortedelementsNodeSet[position() mod 4 = 1]">
          <tr>
            <xsl:for-each select=".|following-sibling::node()[position()
&lt; 4]">
              <td>
                <xsl:value-of select="name()"/>
              </td>
            </xsl:for-each>
          </tr>
        </xsl:for-each>
      </table>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

when applied on this source.xml:

<db>
  <a>aaa</a>
  <c>ccc</c>
  <b>bbb</b>
  <e>eee</e>
  <d>ddd</d>
  <i>iii</i>
  <f>fff</f>
  <k>kkk</k>
  <m>mmm</m>
  <h>hhh</h>
  <j>jjj</j>
</db>

produces the wanted result:

<table>
  <tr>
    <td>a</td>
    <td>b</td>
    <td>c</td>
    <td>d</td>
  </tr>
  <tr>
    <td>e</td>
    <td>f</td>
    <td>h</td>
    <td>i</td>
  </tr>
  <tr>
    <td>j</td>
    <td>k</td>
    <td>m</td>
  </tr>
</table>

I got this result with many different XSLT processors, including xsltProc:

"Using libxml 20504, libxslt 10027 and libexslt 718
xsltproc was compiled against libxml 20504, libxslt 10027 and libexslt 718
libxslt 10027 was compiled against libxml 20504
libexslt 718 was compiled against libxml 20504"


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL


"Saverio Perugini" <sperugin(_at_)csgrad(_dot_)cs(_dot_)vt(_dot_)edu> wrote in 
message
news:Pine(_dot_)LNX(_dot_)4(_dot_)50(_dot_)0310302330500(_dot_)20921-100000(_at_)ap2(_dot_)cs(_dot_)vt(_dot_)edu(_dot_)(_dot_)(_dot_)
Hello,

I'd like to produce a 4 column HTML table where the
text contained in each <td> element is alphabetically
sorted from cell (1,1) to cell (n,4),
where n is the required number of rows.

The following is an attempt:

<xsl:template match="/db">
   <xsl:if test="*[some condition]">
      <xsl:variable name="sortedelements">
         <xsl:for-each select="*">
            <xsl:sort data-type="text" select="name()"/>
            <xsl:copy-of select="."/>
         </xsl:for-each>
      </xsl:variable>

      <xsl:variable name="sortedelementsNodeSet"
                    select="exsl:node-set($sortedelements)/*"/>
      <table>
         <xsl:for-each select="$sortedelementsNodeSet[position() mod 4 =
1]">
            <tr>
               <xsl:for-each
select=".|following-sibling::node()[position() &lt; 4]">
                   <td><xsl:value-of select="name()"/></td>
               </xsl:for-each>
            </tr>
         </xsl:for-each>
      </table>
   </xsl:if>
</xsl:template>

While this produces a table, not all rows contain four columns, and even
worse, the text contained in the <td>'s is not sorted across all <td>'s.

I am using libxslt, which supports the node-set() exslt function.
Does anyone see what is going wrong here?

Thanks,
Saverio

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






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