2012-04-13 8 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», и он работал отлично. Я предположил, что после применения первой строки я не мог ее использовать ag но я просто не понимаю, как работают шаблоны. Еще раз спасибо! –

Смежные вопросы