0
votes

I'm a newbie and I'm trying to use test in an <xsl:when> element to see if the current node is a descendant of an earlier node. I then want to apply the appropriate html tag to the content. I am a novice with xpath expressions.

Specifically, I would like to apply <th> tags to the <tcell> elements that are descendants of the <thead> element. I would like to apply <td> tags to the <tcell> elements that are descendants of the <tbody> elements. My best guess is that I have to use an <xsl:choose> element in my <xsl:template match="tcell"> element. I have tried a few different xpath expressions in the test, but none of them have worked.

Question: Is <xsl:choose> the best option for this?

Here is my xml document, the applicable part. The document structure cannot be changed.

<table>
  <tgroup>
    <thead>
      <trow>
        <tcell>Column Head Text</tcell>
        <tcell>Column Head Text</tcell>
      </trow>
    </thead>
    <tbody>
      <trow>
        <tcell>Cell Text</tcell>
        <tcell>Cell Text</tcell>
      </trow>        
    </tbody>
  </tgroup>
 </table>

I want to use XSL/XPath to generate a table with a header row and body rows. My XSL stylesheet looks like this:

<xsl:template match="/">
  <html>
    <body>
    <xsl:apply templates />
    </body>
  </html>
</xsl:template>

<xsl:template match="table">
    <table>
        <xsl:apply-templates />
    </table>
</xsl:template>

<xsl:template match="tgroup">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="thead">
    <thead>
        <xsl:apply-templates />
    </thead>
</xsl:template>

<xsl:template match="tbody">
    <tbody>
        <xsl:apply-templates />
    </tbody>
</xsl:template>       

<xsl:template match="trow">
    <tr>
        <xsl:apply-templates />
    </tr>
</xsl:template>

<!-- MY TROUBLE STARTS HERE -->
<xsl:template match="tcell">
    <xsl:choose>
      <xsl:when test="current()!=descendant::tbody">
        <th>
          <xsl:value-of select="."/>
        </th>
      </xsl:when>
      <xsl:otherwise>
        <td>
          <xsl:value-of select="."/>
        </td>
      </xsl:otherwise>
    </xsl:choose>
</xsl:template>

Any help would be appreciated.

Sample html output

<table>
  <tgroup>
    <thead>
     <tr>
      <th>Column Head Text</th>
      <th>Column Head Text</th>
     <tr>
    </thead>
    <tbody>
      <tr>
       <td>Cell Text</td>
       <td>Cell Text</td>
      </tr>
    </tbody>
  </tgroup>
 </table>

Thanks, M_66

1
Show the desired output xml pleaseJim Garrison
The desired xml output would be:<br /><pre><code><table><br />M_66

1 Answers

0
votes
<xsl:template match="thead//tcell">
    <th>
        <xsl:value-of select="."/>
    </th>
</xsl:template>

<xsl:template match="tbody//tcell">
    <td>
        <xsl:value-of select="."/>
    </td>
</xsl:template>

or if you still want to use xsl:choose:

<xsl:template match="tcell">
    <xsl:choose>
        <xsl:when test="ancestor::thead">
            <th>
                <xsl:value-of select="."/>
            </th>
        </xsl:when>
        <xsl:otherwise>
            <td>
                <xsl:value-of select="."/>
            </td>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>