2012-04-13 24 views
0

我有一个XML结构如下所示:如何在XSL中“预览”以获取表格列标题?

<Root> 
    <Node Name="File System"> 
    <Node Name="C:\Windows\System32\drivers\etc\hosts"> 
     <Table> 
     <Row> 
      <Column Name="Name" Value="localhost" /> 
      <Column Name="IP" Value="127.0.0.1" /> 
     </Row> 
     <Row> 
     ..... 
    </Node> 
    </Node> 
</Root> 

我有XSLT代码遍历节点和表,但我会被卡住时,我想以某种方式抢列名头。

这里是工作的代码(除抓住了列标题:

<xsl:template match="Root"> 
    <ul> 
    <xsl:apply-templates select="Node" /> 
    </ul> 
</xsl:template> 

<xsl:template match="Node"> 
    <li> 
    <xsl:value-of select="@Name" /> 
    <xsl:if test="Node"> 
     <ul> 
     <xsl:apply-templates select="Node" /> 
     </ul> 
    </xsl:if> 
    <xsl:if test="Attributes"> 
     <xsl:apply-templates select="Attributes" /> 
    </xsl:if> 
    <xsl:if test="Table"> 
     <xsl:apply-templates select="Table" /> 
    </xsl:if> 
    </li> 
</xsl:template> 

<xsl:template match="Attributes"> 
    <ul> 
    <xsl:apply-templates select="Attribute" /> 
    </ul> 
</xsl:template> 

<xsl:template match="Attribute"> 
    <li> 
    <b><xsl:value-of select="@Name" />:</b> <xsl:value-of select="@Value" /> 
    </li> 
</xsl:template> 

<xsl:template match="Table"> 
    <table border="1"> 
    <tbody> 
     <xsl:apply-templates select="Row" /> 
    </tbody> 
    </table> 
</xsl:template> 

<xsl:template match="Row"> 
    <tr> 
    <xsl:apply-templates select="Column" /> 
    </tr> 
</xsl:template> 

<xsl:template match="Column"> 
    <td> 
    <xsl:value-of select="@Value" /> 
    </td> 
</xsl:template> 

回答

0

如何:

<xsl:template match="Row" mode="thead"> 
    <th> 
    <xsl:apply-templates select="Column" mode="thead"/> 
    </th> 
</xsl:template> 

<xsl:template match="Column" mode="thead"> 
    <td> 
    <xsl:value-of select="@Name" /> 
    </td> 
</xsl:template> 

,然后改变你的match="Table"

<xsl:template match="Table"> 
    <table border="1"> 
    <thead> 
     <xsl:apply-templates select="Row[1]" mode="thead"/> 
    </thead> 
    <tbody> 
     <xsl:apply-templates select="Row" /> 
    </tbody> 
    </table> 
</xsl:template> 
+0

太谢谢你了!我只是将“th”标记更改为“tr”,并且工作完美。我假设一旦第一行被应用,我就无法使用它但这只是我误解了模板的工作原理。再次感谢! – 2012-04-13 16:18:14

相关问题