Is it possible to merge attributes from multiple elements?

Subject: Is it possible to merge attributes from multiple elements?
From: andy@xxxxxxxxxxxx
Date: Sat, 17 Jun 2000 22:29:58 -0700
I'm trying to write a template that combines attributes from two elements into a 
single element. The goal is to use XSLT to translate something like this:

<matrix>
 <column c="1">
 <column c="2">
 <row r="1">
 <row r="2">
</matrix>

into something like this:

<table>
 <tr><td c="1" r="1"/><td c="2" r="1"/></tr>
 <tr><td c="1" r="2"/><td c="2" r="2"/></tr>
</table>

I thought this would be a simple thing to do, but I haven't succeeded yet. Here was 
my latest attempt to generate the <tr> elements, which is the hard part:

 <xsl:template match="row">
  <xsl:variable name="attrs"><xsl:value-of select="@$"/><xsl:variable>
  <tr>
   <xsl:for-each select="../column">
    <td>
     <xsl:for-each select="@*">
      <xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
     </xsl:for-each>
     <xsl:for-each select="$attrs">
      <xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
     </xsl:for-each>
    </td>
   </xsl:for-each>
  </tr>
  </xsl:template>

Given a <row> of the table, this code is supposed to iterate over the column 
definitions, creating a <td> for each column. The column attributes will be copied 
into the <td> with <xsl:attribute>. After the column attributes are copied, the 
attributes on the <row> are then copied. The idea being that attributes specified 
in the row will override the defaults in the column.

Unfortunately, this code crashes IE5 and causes Xalan to generate a "Can not 
convert #UNKNOWN to a NodeList!" message and then exit.

The problem appears to be with my attempt to use a variable to store the set of 
attributes from the <row> element. The reason I'm using a variable is that I need 
to get at those attributes from within the <xsl:for-each select="../column"> loop. 
Unfortunately, once the context is set to a <column>, there's no XPath that gets 
back to the <row> context (I think). So, I thought I'd save the <row>'s attributes 
in a variable and call them up later.

As far as I've been able to tell, variables, can't hold anything other than simple 
text--much less a collection of attribute nodes.

Has anyone else got working code that merges attribute sets from multiple elements? 
Also, why are variables restricted to simple text, or am I misunderstanding 
something?

Any and all suggestions appreciated.

Andy Turk
andy@xxxxxxxxxxxx



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


Current Thread