xsl-list
[Top] [All Lists]

RE: Create table with optional elements

2003-06-02 07:27:56
Hello,

Your code works ok, but I have another problem related to the same
table.
I can have more then one element on each column/row.
My xml can be:

        <wn1:Term i:type="wn1:Term">
        <wn1:data i:type="d:string">Client (of a shop)</wn1:data>
            <wn1:lang i:type="d:string">en</wn1:lang>
      </wn1:Term>
      <wn1:Term i:type="wn1:Term">
             <wn1:data i:type="d:string">Clase de objeto</wn1:data>
             <wn1:lang i:type="d:string">es</wn1:lang>
       </wn1:Term>
      <wn1:Term i:type="wn1:Term">
             <wn1:data i:type="d:string">Chaise longue 3</wn1:data>
             <wn1:lang i:type="d:string">fr</wn1:lang>
      </wn1:Term>
      <wn1:Term i:type="wn1:Term">
             <wn1:data i:type="d:string">Transat</wn1:data>
             <wn1:lang i:type="d:string">fr</wn1:lang>
      </wn1:Term>
                    
the table row would be
<tr>
        <td>
                Client (of a shop)
        </td>
        <td>
                Clase de objeto
        </td>
        <td>
                Chaise longue 3, Transat
        </td>

</tr>

How can I do this? 
Thanks in advance,

Ricardo Saraiva.

-----Original Message-----
From: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com] On Behalf Of 
Martin
Rowlinson (MarrowSoft)
Sent: terça-feira, 27 de Maio de 2003 12:15
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] Create table with optional elements

Hi Ricardo,

Assuming that you have your namespaces declared, e.g.

<wn2:ArrayOfToken_Response i:type="wn1:ArrayOfToken"
  xmlns:wn2="namespace-wn2" xmlns:wn1="namespace-wn1"
xmlns:i="namespace-i">
  ....

then try something like...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns:wn2="namespace-wn2" xmlns:wn1="namespace-wn1"
xmlns:i="namespace-i">
<xsl:output method="html" indent="yes"/>
<!-- key for finding distinct columns -->
<xsl:key name="kDistinctCols" match="wn1:lang" use="."/>
<!-- find distinct columns here so that they can be re-used in different
templates -->
<xsl:variable name="DistinctCols"
select="/wn2:ArrayOfToken_Response/wn1:Token/wn1:term/wn1:Term/wn1:lang[
generate-id() = generate-id(key('kDistinctCols',.))]"/>
<xsl:template match="wn2:ArrayOfToken_Response">
  <html>
    <body>
      <table border="1">
        <!-- header row -->
        <tr>
          <xsl:for-each select="$DistinctCols">
            <xsl:sort/>
            <th>
              <xsl:value-of select="."/>
            </th>
          </xsl:for-each>
        </tr>
        <!-- data rows -->
        <xsl:apply-templates select="wn1:Token"/>
      </table>
    </body>
  </html>
</xsl:template>

<xsl:template match="wn1:Token">
  <xsl:variable name="this-terms" select="wn1:term/wn1:Term"/>
  <tr>
    <xsl:for-each select="$DistinctCols">
      <xsl:sort/>
      <xsl:variable name="this-cell" select="$this-terms[wn1:lang =
current()]"/>
      <td>
        <xsl:choose>
          <xsl:when test="$this-cell">
            <xsl:value-of select="$this-cell/wn1:data"/>
          </xsl:when>
          <xsl:otherwise>
            <!-- do whatever you want to indicate no cell content -->
            <xsl:text>[none!]</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </td>
    </xsl:for-each>
  </tr>
</xsl:template>
</xsl:stylesheet>


Hope this helps
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator




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





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



<Prev in Thread] Current Thread [Next in Thread>
  • RE: Create table with optional elements, Ricardo Saraiva <=