xsl-list
[Top] [All Lists]

Re: Making sure table rows are filled?

2002-10-18 11:46:07

--- Vic Gar wrote:

Hello all,
With flat-ish xml (below), I need to
1) select the elements from a predetermined position (e.g, elems
d1-d9)
     <xsl:variable name="table_nodes" select="/a/b/c/*[position()
&lt; 
10]"/>
2) take those that have text node children
     ...and normalize-space{.}
3) make a table with rows of two name/value pairs (e.g., 
<tr><td>#9</td><td>foo</td><td>#10</td><td>bar</td></tr> )
     for-each select="$table_nodes[position() mod 2 = 1]
          <tr>
               <xsl:apply-templates select=". | ...
4) if there are an odd number of elements, only the last row of the
table 
can be missing a <td> - all the preceding rows should have the
requisite 2 
name/value pairs.


I tried:
<xsl:apply-templates select=". | $table_nodes[position() &gt; $here
and

position() &lt; $here+2]" mode="td"/>
The nodes that made it into $table_nodes, in doc order, are
d1 & d4-9.
but for some reason it created:
d1   (d1 value}  d4   (d4 value}
d5   (d5 value}
d6   (d6 value}  d7   (d7 value)
d7   (d7 value}  d9   (d9 value}

My understanding of what's going on here is a long way from where it
should 
be.  I get why d7 was selected twice, but as for the rest (why no d8,
why 
single <td> at d5, how to do this properly) can anyone explain it to
me?

xml:
<a>
   <b>
      <c>
         <d1>one</d1>
         <d2></d2>
         <d3></d3>
         <d4>four</d4>
         <d5>five</d5>
         <d6>six</d6>
         <d7>seven</d7>
         <d8>eight</d8>
         <d9>nine</d9>
         <d10>ten</d10>
      </c>
   </b>
</a>

Many thanks!


Hi Vic,

Here's one solution to your problem:

With your source xml document (just above), 
the following transformation:
============================
<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <xsl:output method="html" indent="yes"/>
  
  <xsl:template match="/">
    <table>
      <xsl:apply-templates 
       select="/*/*/*/*[normalize-space()][position() mod 2 = 1]"
       mode="row"/>
    </table>
  </xsl:template>
  
  <xsl:template match="*" mode="row">
    <tr>
      <xsl:apply-templates select=". 
                                 | 
                                   following-sibling::*
                                              [normalize-space()][1]"/>
    </tr>
  </xsl:template>
  
  <xsl:template match="*">
    <td><xsl:value-of select="name()"/></td>
    <td><xsl:value-of select="."/></td>
  </xsl:template>
  
</xsl:stylesheet>

when applied produces the desired result:
========================================
<table>
   <tr>
      <td>d1</td>
      <td>one</td>
      <td>d4</td>
      <td>four</td>
   </tr>
   <tr>
      <td>d5</td>
      <td>five</td>
      <td>d6</td>
      <td>six</td>
   </tr>
   <tr>
      <td>d7</td>
      <td>seven</td>
      <td>d8</td>
      <td>eight</td>
   </tr>
   <tr>
      <td>d9</td>
      <td>nine</td>
      <td>d10</td>
      <td>ten</td>
   </tr>
</table>


Hope this helped.



=====
Cheers,

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

__________________________________________________
Do you Yahoo!?
Faith Hill - Exclusive Performances, Videos & More
http://faith.yahoo.com

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



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