RE: RE: [xsl] How to implement an array

Subject: RE: RE: [xsl] How to implement an array
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 13 Feb 2003 18:34:41 -0500
Imrran,

As you surmised, your problem is in your second template:

<xsl:template match="Table1/Item" mode="celldata">
  <xsl:variable name="Table1" select="."/>
  <tr>
  <xsl:for-each select="./*">
  <xsl:variable name="tval" select="text()"/>
  <xsl:for-each select="$Table1Struct">
    <xsl:variable name="ItemName" select="string(name(.))"/>
    <xsl:if test="count($Table1//*[name(.)=$ItemName]) &gt; 0">
      <td><xsl:value-of select="$tval"/></td>
    </xsl:if>
  </xsl:for-each>
  </xsl:for-each>
  </tr>
</xsl:template>

Essentially what's messing you up is that you are iterating over your lookup table nodes ($Table1Struct) for *each* of the child nodes of your Item, and every time, you're creating <td> output if *any* child (descendant actually, as you have it) of your Item has the name you're looking for. Usually it will, and you are performing this test (count(Item/*) * count($TableStruct)) times.


It's tricky and confusing. In order to get the logic right, you actually have to invert it. First iterate over the lookup nodes in $Table1Struct, creating a <td> for each one. Then its value: you want the value of that child of your Item that passes your name test; if there is none, presumably (this being HTML) you want a placeholding non-breaking space.

This template will do this:

<xsl:template match="Table1/Item" mode="celldata">
  <xsl:variable name="thisitem" select="."/>
    <tr>
      <xsl:for-each select="$Table1Struct">
        <td>
          <xsl:value-of select="$thisitem/*[name()=name(current())]"/>
          <!-- selects that child of the Item whose name is the name of
               the $Table1Struct node we're looking at -->
          <xsl:if
             test="not($thisitem/*[name()=name(current())])">&#160;</xsl:if>
          <!-- if there is none, gives us a space -->
        </td>
      </xsl:for-each>
  </tr>
</xsl:template>

Please ask about anything mysterious here.

Cheers,
Wendell



======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


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



Current Thread