Xml 合并XSLT中的列时出现问题
我有一个XML,我正在使用XSLT将其转换为HTML。当前表格结构如下所示Xml 合并XSLT中的列时出现问题,xml,xslt,Xml,Xslt,我有一个XML,我正在使用XSLT将其转换为HTML。当前表格结构如下所示 |-------------------------| |type |name |age | |data |john |28 | |comment |pass | | |-------------------------| 我正在尝试将col0='comment'所在行的单元格合并到两个TD中。第一个TD表示“评论”,第二个
|-------------------------|
|type |name |age |
|data |john |28 |
|comment |pass | |
|-------------------------|
我正在尝试将col0='comment'所在行的单元格合并到两个TD中。第一个TD表示“评论”,第二个TD表示“通过”。我将在第二个TD中添加colspan=2
我试图从下面的代码生成下表结构。通过的td将具有colsapn=2
|-------------------------|
|type |name |age |
|data |john |28 |
|comment |pass |
|-------------------------|
XML示例数据如下所示
<Form>
<Log>
<col0>type</col0>
<col1>name</col1>
<col2>age</col2>
</Log>
<Log>
<col0>data</col0>
<col1>john</col1>
<col2>28</col2>
</Log>
<Log>
<col0>comment</col0>
<col1>passed</col1>
<col2></col2>
</Log>
</Form>
我使用下面的XSLT代码进行转换。但这并没有产生预期的结果。这是更大的XML的一部分,所以我只放了所需的代码
代码如下。我已经删除了代码中与此问题无关的其他部分
<xsl:for-each select="Form">
-- Another code for Log position 1
<xsl:apply-templates select="Log[position() > 1]" mode="LogsData" />
</xsl:for-each>
<xsl:template match="Form/*" mode="LogsData">
<xsl:choose>
<xsl:when test="name()='col0' and text()='Comments'">
<tr>
<td>
<b>
<xsl:value-of select="name() = 'col0' and text()='comment'"/>
</b>
</td>
<td>
<xsl:for-each select="*[starts-with(name(), 'col')]">
<xsl:value-of select="." />
</xsl:for-each>
</td>
</tr>
</xsl:when>
<xsl:otherwise>
<tr>
<xsl:for-each select="*[starts-with(name(), 'col')]">
<xsl:choose>
<xsl:when test="name() = 'col0'">
<td>
<b>
<xsl:value-of select="." disable-output-escaping="yes"/>
</b>
</td>
</xsl:when>
<xsl:otherwise>
<td>
<xsl:value-of select="." disable-output-escaping="yes"/>
</td>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</tr>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
结果未按预期生成。这张桌子仍然像以前一样。每个注释数据都有单独的TD。
我只想在评论部分有两个TD。第一个TD包含注释,第二个TD包含col0以外的所有其他列值。
请帮我解决这个问题 如果我猜对了,您想制作第二个模板:
<xsl:template match="Log" mode="LogsData">
<tr>
<xsl:choose>
<xsl:when test="*[1]='comment'">
<th>Comments</th>
<td colspan="{count(*) -1}">
<xsl:for-each select="*[position() > 1]">
<xsl:value-of select="." />
</xsl:for-each>
</td>
</xsl:when>
<xsl:otherwise>
<th>
<xsl:value-of select="*[1]" />
</th>
<xsl:for-each select="*[position() > 1]">
<td>
<xsl:value-of select="." />
</td>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:template>
第二个是TD,列中除col0之外的所有其他值。您的意思是可以比示例中显示的三个列更多吗?@michael.hor257k是的,可以更多,因为它们本质上是动态的。但它们会编号,如col0、col1、col2、co3…..ColN。。在评论部分,我想把第一个TD中的所有数据合并在一起。你还想让我再加一些数据吗??我使用select=*[以名称“col”开头]而不是select=*[位置>1],因为还有更多的节点。如何从该选择中排除col0?我是用select=*[以name'col'和notname='col0'开头]完成的,非常感谢迈克尔。非常感谢您的帮助。您也可以使用*[以名称开头,'col'][位置>1]。谢谢。你太棒了。。!!