0

I have a XML structure like the following:

<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>

I have xslt code to traverse the nodes and tables but I get stuck when I want to somehow grab the column names as headers.

Here is the working code (other than grabbing the column headers:

<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>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Rick Parker
  • 15
  • 2
  • 6

1 Answers1

0

How about:

<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>

and then change your match="Table" to

<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>
hroptatyr
  • 4,702
  • 1
  • 35
  • 38
  • Thank you so much! I just changed the "th" tag to "tr" and it worked perfectly. I assumed once the first row was applied I couldn't use it again but that's just me misunderstanding how the templates work. Thanks again! – Rick Parker Apr 13 '12 at 16:18