我在编写一个xslt时面临一个问题:
的xml:
<students>
<studentDetails tag="to" id="1" fname="AA"/>
<studentDetails tag="mo" id="2" fname="BB"/>
</students>
编写xslt我必须将其转换为HTML:
<table>
<tr>
<th>to</th>
<th>mo</th>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>AA</td>
<td>BB</td>
</tr>
</table>
现在该怎么写这个xslt?
我试过了
<xsl:template match="students">
<table>
<tr>
<xsl:apply-templates select="studentDetails"/>
</tr>
</table>
</xsl:template>
<xsl:template match="studentDetails">
<th>
<xsl:call-template name="zz">
<xsl:with-param name="child-name" select="'tag'"/>
</xsl:call-template>
</th>
<td></td>
</xsl:template>
<xsl:template name="zz">
<xsl:param name="child-name"/>
<xsl:value-of select="@*[name() = $child-name]"/>
</xsl:template>
它的工作但我的逻辑失败了。有人可以建议如何编码这个。
答案 0 :(得分:3)
这将提供您需要的输出:
<xsl:template match="students">
<table>
<tr>
<xsl:for-each select="studentDetails">
<th><xsl:value-of select="@tag"/></th>
</xsl:for-each>
</tr>
<tr>
<xsl:for-each select="studentDetails">
<td><xsl:value-of select="@id"/></td>
</xsl:for-each>
</tr>
<tr>
<xsl:for-each select="studentDetails">
<td><xsl:value-of select="@fname"/></td>
</xsl:for-each>
</tr>
</table>
</xsl:template>
答案 1 :(得分:1)
这不起作用吗?从你所写的内容来看,这就像你追求的那样?
<xsl:template match="/">
<table>
<tr>
<th>to</th>
<th>mo</th>
</tr>
<xsl:for-each select="/students/studentDetails">
<tr>
<td><xsl:value-of select="./@to" /></td>
<td><xsl:value-of select="./@mo" /></td>
<td><xsl:value-of select="./@fname" /></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
P.S。写在我的头顶,所以可能不是完美的语法...