0

我有一个如下的 XML 结构:

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

1 回答 1

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>
于 2012-04-13T15:41:07.733 回答