XML / XSL table for HTML

Can someone help me get the columndefinition / column / cssclass value from my row loop?

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

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

Thank.

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 this is what my xsl is currently:

<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


source to share


2 answers


...
<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>
...

      



+3


source


As an alternative to Pavel's solution, you can use 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 will index the nodes <cssclass>

by their parent position <column>

. For large entrances, this allows for faster work.

+2


source







All Articles