使用根据元素的祖先元素来格式化元素
我是新手,我正在尝试在
具体来说,我想申请
问题:是
这是我的 xml 文档,适用的部分。文档结构无法更改。
<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>
我想使用 XSL/XPath 生成一个包含标题行和正文行的表。我的 XSL 样式表如下所示:
<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>
任何帮助将不胜感激。
示例 html 输出
<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>
谢谢, M_66
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
或者如果您仍想使用
xsl:choose
:or if you still want to use
xsl:choose
: