tags:

views:

179

answers:

2

Can someone help me get the value of the columndefinition/column/cssclass from within my rows loop?

So, in my xsl, I want to pull in the cssclass for the same column position during my "rows" forloop, and put it into my <td class="PullItFromColumnDefition">

Hopefully this makes sense. Can anyone help me figure this out?

Thanks.

My XML looks something like this:

<report>
  <columndefinition>
    <column>
      <headertext>Test Column 1</headertext>
      <cssclass>test1</cssclass>
    </column>
    <column>
      <headertext>Test Column 2</headertext>
      <cssclass>test2</cssclass>
    </column>
  </columndefinition>
  <rows>
    <row>
      <column>3</column>
      <column>11/04/2002</column>
    </row>
    <row>
      <column>22</column>
      <column>04/15/2003</column>
    </row>
    <row>
      <column>134</column>
      <column>04/15/2003</column>
    </row>
    <row>
      <column>63</column>
      <column>11/03/2004</column>
    </row>
    <row>
      <column>65</column>
      <column>11/03/2004</column>
    </row>
    <row>
      <column>66</column>
      <column>11/03/2004</column>
    </row>
  </rows>
</report>

And here is what my xsl currently is:

<xsl:template match="/report">
  <html>
  <body>
    <h2>Report Sample</h2>
    <table border="1">
      <thead>
        <xsl:for-each select="columndefinition/column">
        <th><xsl:value-of select="headertext"/></th>
        </xsl:for-each>
      </thead>
      <tbody>
      <xsl:for-each select="rows/row">
      <tr>
        <xsl:for-each select="column">
        <td><xsl:value-of select="."/></td>
        </xsl:for-each>
      </tr>
      </xsl:for-each>
      </tbody>
    </table>
  </body>
  </html>
</xsl:template>
+2  A: 
...
<xsl:for-each select="column">
  <xsl:variable name="column-index" select="position()"/>
  <td class="{/report/columndefinition/column[$column-index]/cssclass}">
    <xsl:value-of select="."/>
  </td>
</xsl:for-each>
...
Pavel Minaev
Nice and simple, Thanks!
TehOne
+1  A: 

As an alternative to Pavel's solution you could make use of an XSL key:

<xsl:key 
  name="kCssClass" 
  match="cssclass" 
  use="count(../preceding-sibling::column) + 1" 
/>

<!-- later, in <column> context… -->
<td class="{key('kCssClass', position())}">

The key would index <cssclass> nodes by their parent <column> position. For large inputs, this has a chance to run faster.

Tomalak