Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing <xsl:choose> to format an element depending on its ancestor element
    text
    copied!<p>I'm a newbie and I'm trying to use test in an &lt;xsl:when&gt; 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.</p> <p>Specifically, I would like to apply &lt;th&gt; tags to the &lt;tcell&gt; elements that are descendants of the &lt;thead&gt; element. I would like to apply &lt;td&gt; tags to the &lt;tcell&gt; elements that are descendants of the &lt;tbody&gt; elements. My best guess is that I have to use an &lt;xsl:choose&gt; element in my &lt;xsl:template match="tcell"&gt; element. I have tried a few different xpath expressions in the test, but none of them have worked.</p> <p><strong>Question:</strong> Is &lt;xsl:choose&gt; the best option for this?</p> <p>Here is my xml document, the applicable part. The document structure cannot be changed.</p> <pre><code>&lt;table&gt; &lt;tgroup&gt; &lt;thead&gt; &lt;trow&gt; &lt;tcell&gt;Column Head Text&lt;/tcell&gt; &lt;tcell&gt;Column Head Text&lt;/tcell&gt; &lt;/trow&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;trow&gt; &lt;tcell&gt;Cell Text&lt;/tcell&gt; &lt;tcell&gt;Cell Text&lt;/tcell&gt; &lt;/trow&gt; &lt;/tbody&gt; &lt;/tgroup&gt; &lt;/table&gt; </code></pre> <p>I want to use XSL/XPath to generate a table with a header row and body rows. My XSL stylesheet looks like this:</p> <pre><code>&lt;xsl:template match="/"&gt; &lt;html&gt; &lt;body&gt; &lt;xsl:apply templates /&gt; &lt;/body&gt; &lt;/html&gt; &lt;/xsl:template&gt; &lt;xsl:template match="table"&gt; &lt;table&gt; &lt;xsl:apply-templates /&gt; &lt;/table&gt; &lt;/xsl:template&gt; &lt;xsl:template match="tgroup"&gt; &lt;xsl:apply-templates/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="thead"&gt; &lt;thead&gt; &lt;xsl:apply-templates /&gt; &lt;/thead&gt; &lt;/xsl:template&gt; &lt;xsl:template match="tbody"&gt; &lt;tbody&gt; &lt;xsl:apply-templates /&gt; &lt;/tbody&gt; &lt;/xsl:template&gt; &lt;xsl:template match="trow"&gt; &lt;tr&gt; &lt;xsl:apply-templates /&gt; &lt;/tr&gt; &lt;/xsl:template&gt; &lt;!-- MY TROUBLE STARTS HERE --&gt; &lt;xsl:template match="tcell"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="current()!=descendant::tbody"&gt; &lt;th&gt; &lt;xsl:value-of select="."/&gt; &lt;/th&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;td&gt; &lt;xsl:value-of select="."/&gt; &lt;/td&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:template&gt; </code></pre> <p>Any help would be appreciated.</p> <p><strong>Sample html output</strong></p> <pre><code>&lt;table&gt; &lt;tgroup&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;Column Head Text&lt;/th&gt; &lt;th&gt;Column Head Text&lt;/th&gt; &lt;tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;Cell Text&lt;/td&gt; &lt;td&gt;Cell Text&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/tgroup&gt; &lt;/table&gt; </code></pre> <p>Thanks, M_66</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload